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

1.1       albertel    1: # The LearningOnline Network
                      2: # TCP networking package
1.12      www         3: #
1.1448  ! raeburn     4: # $Id: lonnet.pm,v 1.1447 2021/04/18 02:08:47 raeburn Exp $
1.178     www         5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.169     harris41   28: ###
                     29: 
1.971     jms        30: =pod
                     31: 
1.972     jms        32: =head1 NAME
                     33: 
                     34: Apache::lonnet.pm
                     35: 
                     36: =head1 SYNOPSIS
                     37: 
                     38: This file is an interface to the lonc processes of
                     39: the LON-CAPA network as well as set of elaborated functions for handling information
                     40: necessary for navigating through a given cluster of LON-CAPA machines within a
                     41: domain. There are over 40 specialized functions in this module which handle the
                     42: reading and transmission of metadata, user information (ids, names, environments, roles,
                     43: logs), file information (storage, reading, directories, extensions, replication, embedded
                     44: styles and descriptors), educational resources (course descriptions, section names and
                     45: numbers), url hashing (to assign roles on a url basis), and translating abbreviated symbols to
                     46: and from more descriptive phrases or explanations.
                     47: 
                     48: This is part of the LearningOnline Network with CAPA project
                     49: described at http://www.lon-capa.org.
                     50: 
1.971     jms        51: =head1 Package Variables
                     52: 
                     53: These are largely undocumented, so if you decipher one please note it here.
                     54: 
                     55: =over 4
                     56: 
                     57: =item $processmarker
                     58: 
                     59: Contains the time this process was started and this servers host id.
                     60: 
                     61: =item $dumpcount
                     62: 
                     63: Counts the number of times a message log flush has been attempted (regardless
                     64: of success) by this process.  Used as part of the filename when messages are
                     65: delayed.
                     66: 
                     67: =back
                     68: 
                     69: =cut
                     70: 
1.1       albertel   71: package Apache::lonnet;
                     72: 
                     73: use strict;
1.486     www        74: use HTTP::Date;
1.977     amueller   75: use Image::Magick;
1.1389    raeburn    76: use CGI::Cookie;
1.1182    foxr       77: 
1.1173    foxr       78: use Encode;
                     79: 
1.1405    raeburn    80: use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir $deftex
1.1138    raeburn    81:             $_64bit %env %protocol %loncaparevs %serverhomeIDs %needsrelease
1.1416    raeburn    82:             %managerstab $passwdmin);
1.871     albertel   83: 
                     84: my (%badServerCache, $memcache, %courselogs, %accesshash, %domainrolehash,
                     85:     %userrolehash, $processmarker, $dumpcount, %coursedombuf,
                     86:     %coursenumbuf, %coursehombuf, %coursedescrbuf, %courseinstcodebuf,
1.958     www        87:     %courseownerbuf, %coursetypebuf,$locknum);
1.403     www        88: 
1.1       albertel   89: use IO::Socket;
1.31      www        90: use GDBM_File;
1.208     albertel   91: use HTML::LCParser;
1.88      www        92: use Fcntl qw(:flock);
1.870     albertel   93: use Storable qw(thaw nfreeze);
1.1289    damieng    94: use Time::HiRes qw( sleep gettimeofday tv_interval );
1.599     albertel   95: use Cache::Memcached;
1.676     albertel   96: use Digest::MD5;
1.790     albertel   97: use Math::Random;
1.1024    raeburn    98: use File::MMagic;
1.1434    raeburn    99: use Net::CIDR;
1.807     albertel  100: use LONCAPA qw(:DEFAULT :match);
1.740     www       101: use LONCAPA::Configuration;
1.1160    www       102: use LONCAPA::lonmetadata;
1.1167    droeschl  103: use LONCAPA::Lond;
1.1345    raeburn   104: use LONCAPA::LWPReq;
1.1406    raeburn   105: use LONCAPA::transliterate;
1.1117    foxr      106: 
1.1090    raeburn   107: use File::Copy;
1.676     albertel  108: 
1.195     www       109: my $readit;
1.1288    damieng   110: my $max_connection_retries = 20;     # Or some such value.
1.1       albertel  111: 
1.619     albertel  112: require Exporter;
                    113: 
                    114: our @ISA = qw (Exporter);
                    115: our @EXPORT = qw(%env);
                    116: 
1.449     matthew   117: 
1.1187    raeburn   118: # ------------------------------------ Logging (parameters, docs, slots, roles)
1.729     www       119: {
                    120:     my $logid;
1.1187    raeburn   121:     sub write_log {
1.1188    raeburn   122: 	my ($context,$hash_name,$storehash,$delflag,$uname,$udom,$cnum,$cdom)=@_;
1.1184    raeburn   123:         if ($context eq 'course') {
                    124:             if (($cnum eq '') || ($cdom eq '')) {
                    125:                 $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                    126:                 $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                    127:             }
1.957     raeburn   128:         }
1.1184    raeburn   129: 	$logid ++;
1.957     raeburn   130:         my $now = time();
                    131: 	my $id=$now.'00000'.$$.'00000'.$logid;
1.1434    raeburn   132:         my $ip = &get_requestor_ip();
1.1184    raeburn   133:         my $logentry = { 
                    134:                           $id => {
                    135:                                    'exe_uname' => $env{'user.name'},
                    136:                                    'exe_udom'  => $env{'user.domain'},
                    137:                                    'exe_time'  => $now,
1.1434    raeburn   138:                                    'exe_ip'    => $ip,
1.1184    raeburn   139:                                    'delflag'   => $delflag,
                    140:                                    'logentry'  => $storehash,
                    141:                                    'uname'     => $uname,
                    142:                                    'udom'      => $udom,
                    143:                                   }
                    144:                        };
                    145: 	return &put('nohist_'.$hash_name,$logentry,$cdom,$cnum);
1.729     www       146:     }
                    147: }
1.1       albertel  148: 
1.163     harris41  149: sub logtouch {
                    150:     my $execdir=$perlvar{'lonDaemons'};
1.448     albertel  151:     unless (-e "$execdir/logs/lonnet.log") {	
1.1359    raeburn   152: 	open(my $fh,">>","$execdir/logs/lonnet.log");
1.163     harris41  153: 	close $fh;
                    154:     }
                    155:     my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
                    156:     chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
                    157: }
                    158: 
1.1       albertel  159: sub logthis {
                    160:     my $message=shift;
                    161:     my $execdir=$perlvar{'lonDaemons'};
                    162:     my $now=time;
                    163:     my $local=localtime($now);
1.1359    raeburn   164:     if (open(my $fh,">>","$execdir/logs/lonnet.log")) {
1.986     foxr      165: 	my $logstring = $local. " ($$): ".$message."\n"; # Keep any \'s in string.
                    166: 	print $fh $logstring;
1.448     albertel  167: 	close($fh);
                    168:     }
1.1       albertel  169:     return 1;
                    170: }
                    171: 
                    172: sub logperm {
                    173:     my $message=shift;
                    174:     my $execdir=$perlvar{'lonDaemons'};
                    175:     my $now=time;
                    176:     my $local=localtime($now);
1.1359    raeburn   177:     if (open(my $fh,">>","$execdir/logs/lonnet.perm.log")) {
1.448     albertel  178: 	print $fh "$now:$message:$local\n";
                    179: 	close($fh);
                    180:     }
1.1       albertel  181:     return 1;
                    182: }
                    183: 
1.850     albertel  184: sub create_connection {
1.853     albertel  185:     my ($hostname,$lonid) = @_;
1.851     albertel  186:     my $client=IO::Socket::UNIX->new(Peer    => $perlvar{'lonSockCreate'},
1.850     albertel  187: 				     Type    => SOCK_STREAM,
                    188: 				     Timeout => 10);
                    189:     return 0 if (!$client);
1.1399    raeburn   190:     print $client (join(':',$hostname,$lonid,&machine_ids($hostname),$loncaparevs{$lonid})."\n");
1.850     albertel  191:     my $result = <$client>;
                    192:     chomp($result);
                    193:     return 1 if ($result eq 'done');
                    194:     return 0;
                    195: }
                    196: 
1.983     raeburn   197: sub get_server_timezone {
                    198:     my ($cnum,$cdom) = @_;
                    199:     my $home=&homeserver($cnum,$cdom);
                    200:     if ($home ne 'no_host') {
                    201:         my $cachetime = 24*3600;
                    202:         my ($timezone,$cached)=&is_cached_new('servertimezone',$home);
                    203:         if (defined($cached)) {
                    204:             return $timezone;
                    205:         } else {
                    206:             my $timezone = &reply('servertimezone',$home);
                    207:             return &do_cache_new('servertimezone',$home,$timezone,$cachetime);
                    208:         }
                    209:     }
                    210: }
1.850     albertel  211: 
1.1106    raeburn   212: sub get_server_distarch {
                    213:     my ($lonhost,$ignore_cache) = @_;
                    214:     if (defined($lonhost)) {
                    215:         if (!defined(&hostname($lonhost))) {
                    216:             return;
                    217:         }
                    218:         my $cachetime = 12*3600;
                    219:         if (!$ignore_cache) {
                    220:             my ($distarch,$cached)=&is_cached_new('serverdistarch',$lonhost);
                    221:             if (defined($cached)) {
                    222:                 return $distarch;
                    223:             }
                    224:         }
                    225:         my $rep = &reply('serverdistarch',$lonhost);
                    226:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' ||
                    227:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
                    228:                 $rep eq '') {
                    229:             return &do_cache_new('serverdistarch',$lonhost,$rep,$cachetime);
                    230:         }
                    231:     }
                    232:     return;
                    233: }
                    234: 
1.1315    raeburn   235: sub get_servercerts_info {
1.1383    raeburn   236:     my ($lonhost,$hostname,$context) = @_;
                    237:     return if ($lonhost eq '');
                    238:     if ($hostname eq '') {
                    239:         $hostname = &hostname($lonhost);
                    240:     }
                    241:     return if ($hostname eq '');
1.1315    raeburn   242:     my ($rep,$uselocal);
1.1388    raeburn   243:     if ($context eq 'install') {
1.1387    raeburn   244:         $uselocal = 1;
                    245:     } elsif (grep { $_ eq $lonhost } &current_machine_ids()) {
1.1315    raeburn   246:         $uselocal = 1;
                    247:     }
1.1387    raeburn   248:     if (($context ne 'cgi') && ($context ne 'install') && ($uselocal)) {
1.1315    raeburn   249:         my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
1.1323    raeburn   250:         if ($distro eq '') {
                    251:             $uselocal = 0;
                    252:         } elsif ($distro =~ /^(?:centos|redhat|scientific)(\d+)$/) {
1.1315    raeburn   253:             if ($1 < 6) {
                    254:                 $uselocal = 0;
                    255:             }
1.1346    raeburn   256:         }  elsif ($distro =~ /^(?:sles)(\d+)$/) {
                    257:             if ($1 < 12) {
                    258:                 $uselocal = 0;
                    259:             }
1.1315    raeburn   260:         }
                    261:     }
                    262:     if ($uselocal) {
1.1383    raeburn   263:         $rep = LONCAPA::Lond::server_certs(\%perlvar,$lonhost,$hostname);
1.1315    raeburn   264:     } else {
                    265:         $rep=&reply('servercerts',$lonhost);
                    266:     }
                    267:     my ($result,%returnhash);
                    268:     if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
                    269:         ($rep eq 'unknown_cmd')) {
                    270:         $result = $rep;
                    271:     } else {
                    272:         $result = 'ok';
                    273:         my @pairs=split(/\&/,$rep);
                    274:         foreach my $item (@pairs) {
                    275:             my ($key,$value)=split(/=/,$item,2);
                    276:             my $what = &unescape($key);
                    277:             $returnhash{$what}=&thaw_unescape($value);
                    278:         }
                    279:     }
                    280:     return ($result,\%returnhash);
                    281: }
                    282: 
1.993     raeburn   283: sub get_server_loncaparev {
1.1073    raeburn   284:     my ($dom,$lonhost,$ignore_cache,$caller) = @_;
1.993     raeburn   285:     if (defined($lonhost)) {
                    286:         if (!defined(&hostname($lonhost))) {
                    287:             undef($lonhost);
                    288:         }
                    289:     }
                    290:     if (!defined($lonhost)) {
                    291:         if (defined(&domain($dom,'primary'))) {
                    292:             $lonhost=&domain($dom,'primary');
                    293:             if ($lonhost eq 'no_host') {
                    294:                 undef($lonhost);
                    295:             }
                    296:         }
                    297:     }
                    298:     if (defined($lonhost)) {
1.1073    raeburn   299:         my $cachetime = 12*3600;
                    300:         if (!$ignore_cache) {
                    301:             my ($loncaparev,$cached)=&is_cached_new('serverloncaparev',$lonhost);
                    302:             if (defined($cached)) {
                    303:                 return $loncaparev;
                    304:             }
                    305:         }
                    306:         my ($answer,$loncaparev);
                    307:         my @ids=&current_machine_ids();
                    308:         if (grep(/^\Q$lonhost\E$/,@ids)) {
                    309:             $answer = $perlvar{'lonVersion'};
1.1081    raeburn   310:             if ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
1.1073    raeburn   311:                 $loncaparev = $1;
                    312:             }
                    313:         } else {
                    314:             $answer = &reply('serverloncaparev',$lonhost);
                    315:             if (($answer eq 'unknown_cmd') || ($answer eq 'con_lost')) {
                    316:                 if ($caller eq 'loncron') {
1.1397    raeburn   317:                     my $hostname = &hostname($lonhost);
1.1073    raeburn   318:                     my $protocol = $protocol{$lonhost};
                    319:                     $protocol = 'http' if ($protocol ne 'https');
1.1397    raeburn   320:                     my $url = $protocol.'://'.$hostname.'/adm/about.html';
1.1073    raeburn   321:                     my $request=new HTTP::Request('GET',$url);
1.1345    raeburn   322:                     my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,4,1);
1.1073    raeburn   323:                     unless ($response->is_error()) {
                    324:                         my $content = $response->content;
1.1081    raeburn   325:                         if ($content =~ /<p>VERSION\:\s*([\w.\-]+)<\/p>/) {
1.1073    raeburn   326:                             $loncaparev = $1;
                    327:                         }
                    328:                     }
                    329:                 } else {
                    330:                     $loncaparev = $loncaparevs{$lonhost};
                    331:                 }
1.1081    raeburn   332:             } elsif ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
1.1073    raeburn   333:                 $loncaparev = $1;
                    334:             }
1.993     raeburn   335:         }
1.1073    raeburn   336:         return &do_cache_new('serverloncaparev',$lonhost,$loncaparev,$cachetime);
1.993     raeburn   337:     }
                    338: }
                    339: 
1.1074    raeburn   340: sub get_server_homeID {
                    341:     my ($hostname,$ignore_cache,$caller) = @_;
                    342:     unless ($ignore_cache) {
                    343:         my ($serverhomeID,$cached)=&is_cached_new('serverhomeID',$hostname);
                    344:         if (defined($cached)) {
                    345:             return $serverhomeID;
                    346:         }
                    347:     }
                    348:     my $cachetime = 12*3600;
                    349:     my $serverhomeID;
                    350:     if ($caller eq 'loncron') { 
                    351:         my @machine_ids = &machine_ids($hostname);
                    352:         foreach my $id (@machine_ids) {
                    353:             my $response = &reply('serverhomeID',$id);
                    354:             unless (($response eq 'unknown_cmd') || ($response eq 'con_lost')) {
                    355:                 $serverhomeID = $response;
                    356:                 last;
                    357:             }
                    358:         }
                    359:         if ($serverhomeID eq '') {
                    360:             $serverhomeID = $machine_ids[-1];
                    361:         }
                    362:     } else {
                    363:         $serverhomeID = $serverhomeIDs{$hostname};
                    364:     }
                    365:     return &do_cache_new('serverhomeID',$hostname,$serverhomeID,$cachetime);
                    366: }
                    367: 
1.1121    raeburn   368: sub get_remote_globals {
                    369:     my ($lonhost,$whathash,$ignore_cache) = @_;
1.1125    raeburn   370:     my ($result,%returnhash,%whatneeded);
                    371:     if (ref($whathash) eq 'HASH') {
1.1121    raeburn   372:         foreach my $what (sort(keys(%{$whathash}))) {
                    373:             my $hashid = $lonhost.'-'.$what;
1.1125    raeburn   374:             my ($response,$cached);
1.1121    raeburn   375:             unless ($ignore_cache) {
1.1125    raeburn   376:                 ($response,$cached)=&is_cached_new('lonnetglobal',$hashid);
1.1121    raeburn   377:             }
                    378:             if (defined($cached)) {
1.1125    raeburn   379:                 $returnhash{$what} = $response;
1.1121    raeburn   380:             } else {
1.1125    raeburn   381:                 $whatneeded{$what} = 1;
1.1121    raeburn   382:             }
                    383:         }
1.1125    raeburn   384:         if (keys(%whatneeded) == 0) {
                    385:             $result = 'ok';
                    386:         } else {
1.1121    raeburn   387:             my $requested = &freeze_escape(\%whatneeded);
                    388:             my $rep=&reply('readlonnetglobal:'.$requested,$lonhost);
1.1125    raeburn   389:             if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
                    390:                 ($rep eq 'unknown_cmd')) {
                    391:                 $result = $rep;
                    392:             } else {
                    393:                 $result = 'ok';
1.1121    raeburn   394:                 my @pairs=split(/\&/,$rep);
1.1125    raeburn   395:                 foreach my $item (@pairs) {
                    396:                     my ($key,$value)=split(/=/,$item,2);
                    397:                     my $what = &unescape($key);
                    398:                     my $hashid = $lonhost.'-'.$what;
                    399:                     $returnhash{$what}=&thaw_unescape($value);
                    400:                     &do_cache_new('lonnetglobal',$hashid,$returnhash{$what},600);
1.1121    raeburn   401:                 }
                    402:             }
                    403:         }
                    404:     }
1.1125    raeburn   405:     return ($result,\%returnhash);
1.1121    raeburn   406: }
                    407: 
1.1124    raeburn   408: sub remote_devalidate_cache {
1.1241    raeburn   409:     my ($lonhost,$cachekeys) = @_;
                    410:     my $items;
                    411:     return unless (ref($cachekeys) eq 'ARRAY');
                    412:     my $cachestr = join('&',@{$cachekeys});
                    413:     my $response = &reply('devalidatecache:'.&escape($cachestr),$lonhost);
1.1124    raeburn   414:     return $response;
                    415: }
                    416: 
1.1       albertel  417: # -------------------------------------------------- Non-critical communication
                    418: sub subreply {
                    419:     my ($cmd,$server)=@_;
1.838     albertel  420:     my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
1.549     foxr      421:     #
                    422:     #  With loncnew process trimming, there's a timing hole between lonc server
                    423:     #  process exit and the master server picking up the listen on the AF_UNIX
                    424:     #  socket.  In that time interval, a lock file will exist:
                    425: 
                    426:     my $lockfile=$peerfile.".lock";
                    427:     while (-e $lockfile) {	# Need to wait for the lockfile to disappear.
1.1289    damieng   428: 	sleep(0.1);
1.549     foxr      429:     }
                    430:     # At this point, either a loncnew parent is listening or an old lonc
1.550     foxr      431:     # or loncnew child is listening so we can connect or everything's dead.
1.549     foxr      432:     #
1.550     foxr      433:     #   We'll give the connection a few tries before abandoning it.  If
                    434:     #   connection is not possible, we'll con_lost back to the client.
                    435:     #   
                    436:     my $client;
                    437:     for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
                    438: 	$client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
                    439: 				      Type    => SOCK_STREAM,
                    440: 				      Timeout => 10);
1.869     albertel  441: 	if ($client) {
1.550     foxr      442: 	    last;		# Connected!
1.850     albertel  443: 	} else {
1.853     albertel  444: 	    &create_connection(&hostname($server),$server);
1.550     foxr      445: 	}
1.1289    damieng   446:         sleep(0.1);	# Try again later if failed connection.
1.550     foxr      447:     }
                    448:     my $answer;
                    449:     if ($client) {
1.704     albertel  450: 	print $client "sethost:$server:$cmd\n";
1.550     foxr      451: 	$answer=<$client>;
                    452: 	if (!$answer) { $answer="con_lost"; }
                    453: 	chomp($answer);
                    454:     } else {
                    455: 	$answer = 'con_lost';	# Failed connection.
                    456:     }
1.1       albertel  457:     return $answer;
                    458: }
                    459: 
                    460: sub reply {
                    461:     my ($cmd,$server)=@_;
1.838     albertel  462:     unless (defined(&hostname($server))) { return 'no_such_host'; }
1.1       albertel  463:     my $answer=subreply($cmd,$server);
1.65      www       464:     if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
1.1396    raeburn   465:         my $logged = $cmd;
                    466:         if ($cmd =~ /^encrypt:([^:]+):/) {
                    467:             my $subcmd = $1;
                    468:             if (($subcmd eq 'auth') || ($subcmd eq 'passwd') ||
                    469:                 ($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
                    470:                 ($subcmd eq 'putdom') || ($subcmd eq 'autoexportgrades')) {
                    471:                 (undef,undef,my @rest) = split(/:/,$cmd);
                    472:                 if (($subcmd eq 'auth') || ($subcmd eq 'putdom')) {
                    473:                     splice(@rest,2,1,'Hidden');
                    474:                 } elsif ($subcmd eq 'passwd') {
                    475:                     splice(@rest,2,2,('Hidden','Hidden'));
                    476:                 } elsif (($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
                    477:                          ($subcmd eq 'autoexportgrades')) {
                    478:                     splice(@rest,3,1,'Hidden');
                    479:                 }
                    480:                 $logged = join(':',('encrypt:'.$subcmd,@rest));
                    481:             }
                    482:         }
                    483:         &logthis("<font color=\"blue\">WARNING:".
                    484:                  " $logged to $server returned $answer</font>");
1.12      www       485:     }
1.1       albertel  486:     return $answer;
                    487: }
                    488: 
                    489: # ----------------------------------------------------------- Send USR1 to lonc
                    490: 
                    491: sub reconlonc {
1.891     albertel  492:     my ($lonid) = @_;
                    493:     if ($lonid) {
1.1295    raeburn   494:         my $hostname = &hostname($lonid);
1.891     albertel  495: 	my $peerfile="$perlvar{'lonSockDir'}/$hostname";
                    496: 	if ($hostname && -e $peerfile) {
                    497: 	    &logthis("Trying to reconnect lonc for $lonid ($hostname)");
                    498: 	    my $client=IO::Socket::UNIX->new(Peer    => $peerfile,
                    499: 					     Type    => SOCK_STREAM,
                    500: 					     Timeout => 10);
                    501: 	    if ($client) {
                    502: 		print $client ("reset_retries\n");
                    503: 		my $answer=<$client>;
                    504: 		#reset just this one.
                    505: 	    }
                    506: 	}
                    507: 	return;
                    508:     }
                    509: 
1.836     www       510:     &logthis("Trying to reconnect lonc");
1.1       albertel  511:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
1.1359    raeburn   512:     if (open(my $fh,"<",$loncfile)) {
1.1       albertel  513: 	my $loncpid=<$fh>;
                    514:         chomp($loncpid);
                    515:         if (kill 0 => $loncpid) {
                    516: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
                    517:             kill USR1 => $loncpid;
                    518:             sleep 1;
1.1295    raeburn   519:         } else {
1.12      www       520: 	    &logthis(
1.672     albertel  521:                "<font color=\"blue\">WARNING:".
1.12      www       522:                " lonc at pid $loncpid not responding, giving up</font>");
1.1       albertel  523:         }
                    524:     } else {
1.836     www       525: 	&logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
1.1       albertel  526:     }
                    527: }
                    528: 
                    529: # ------------------------------------------------------ Critical communication
1.12      www       530: 
1.1       albertel  531: sub critical {
                    532:     my ($cmd,$server)=@_;
1.838     albertel  533:     unless (&hostname($server)) {
1.672     albertel  534:         &logthis("<font color=\"blue\">WARNING:".
1.89      www       535:                " Critical message to unknown server ($server)</font>");
                    536:         return 'no_such_host';
                    537:     }
1.1       albertel  538:     my $answer=reply($cmd,$server);
                    539:     if ($answer eq 'con_lost') {
1.1295    raeburn   540: 	&reconlonc($server);
1.589     albertel  541: 	my $answer=reply($cmd,$server);
1.1       albertel  542:         if ($answer eq 'con_lost') {
                    543:             my $now=time;
                    544:             my $middlename=$cmd;
1.5       www       545:             $middlename=substr($middlename,0,16);
1.1       albertel  546:             $middlename=~s/\W//g;
                    547:             my $dfilename=
1.305     www       548:       "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
                    549:             $dumpcount++;
1.1       albertel  550:             {
1.448     albertel  551: 		my $dfh;
1.1359    raeburn   552: 		if (open($dfh,">",$dfilename)) {
1.448     albertel  553: 		    print $dfh "$cmd\n"; 
                    554: 		    close($dfh);
                    555: 		}
1.1       albertel  556:             }
1.1288    damieng   557:             sleep 1;
1.1       albertel  558:             my $wcmd='';
                    559:             {
1.448     albertel  560: 		my $dfh;
1.1359    raeburn   561: 		if (open($dfh,"<",$dfilename)) {
1.448     albertel  562: 		    $wcmd=<$dfh>; 
                    563: 		    close($dfh);
                    564: 		}
1.1       albertel  565:             }
                    566:             chomp($wcmd);
1.7       www       567:             if ($wcmd eq $cmd) {
1.672     albertel  568: 		&logthis("<font color=\"blue\">WARNING: ".
1.12      www       569:                          "Connection buffer $dfilename: $cmd</font>");
1.1       albertel  570:                 &logperm("D:$server:$cmd");
                    571: 	        return 'con_delayed';
                    572:             } else {
1.672     albertel  573:                 &logthis("<font color=\"red\">CRITICAL:"
1.12      www       574:                         ." Critical connection failed: $server $cmd</font>");
1.1       albertel  575:                 &logperm("F:$server:$cmd");
                    576:                 return 'con_failed';
                    577:             }
                    578:         }
                    579:     }
                    580:     return $answer;
1.405     albertel  581: }
                    582: 
1.755     albertel  583: # ------------------------------------------- check if return value is an error
                    584: 
                    585: sub error {
                    586:     my ($result) = @_;
1.756     albertel  587:     if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
1.755     albertel  588: 	if ($2 == 2) { return undef; }
                    589: 	return $1;
                    590:     }
                    591:     return undef;
                    592: }
                    593: 
1.783     albertel  594: sub convert_and_load_session_env {
                    595:     my ($lonidsdir,$handle)=@_;
                    596:     my @profile;
                    597:     {
1.917     albertel  598: 	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    599: 	if (!$opened) {
1.915     albertel  600: 	    return 0;
                    601: 	}
1.783     albertel  602: 	flock($idf,LOCK_SH);
                    603: 	@profile=<$idf>;
                    604: 	close($idf);
                    605:     }
                    606:     my %temp_env;
                    607:     foreach my $line (@profile) {
1.786     albertel  608: 	if ($line !~ m/=/) {
                    609: 	    return 0;
                    610: 	}
1.783     albertel  611: 	chomp($line);
                    612: 	my ($envname,$envvalue)=split(/=/,$line,2);
                    613: 	$temp_env{&unescape($envname)} = &unescape($envvalue);
                    614:     }
                    615:     unlink("$lonidsdir/$handle.id");
                    616:     if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
                    617: 	    0640)) {
                    618: 	%disk_env = %temp_env;
                    619: 	@env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
                    620: 	untie(%disk_env);
                    621:     }
1.786     albertel  622:     return 1;
1.783     albertel  623: }
                    624: 
1.374     www       625: # ------------------------------------------- Transfer profile into environment
1.780     albertel  626: my $env_loaded;
                    627: sub transfer_profile_to_env {
1.788     albertel  628:     my ($lonidsdir,$handle,$force_transfer) = @_;
                    629:     if (!$force_transfer && $env_loaded) { return; } 
1.374     www       630: 
1.720     albertel  631:     if (!defined($lonidsdir)) {
                    632: 	$lonidsdir = $perlvar{'lonIDsDir'};
                    633:     }
                    634:     if (!defined($handle)) {
                    635:         ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
                    636:     }
                    637: 
1.786     albertel  638:     my $convert;
                    639:     {
1.917     albertel  640:     	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    641: 	if (!$opened) {
1.915     albertel  642: 	    return;
                    643: 	}
1.786     albertel  644: 	flock($idf,LOCK_SH);
                    645: 	if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
                    646: 		&GDBM_READER(),0640)) {
                    647: 	    @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
                    648: 	    untie(%disk_env);
                    649: 	} else {
                    650: 	    $convert = 1;
                    651: 	}
                    652:     }
                    653:     if ($convert) {
                    654: 	if (!&convert_and_load_session_env($lonidsdir,$handle)) {
                    655: 	    &logthis("Failed to load session, or convert session.");
                    656: 	}
1.374     www       657:     }
1.783     albertel  658: 
1.786     albertel  659:     my %remove;
1.783     albertel  660:     while ( my $envname = each(%env) ) {
1.433     matthew   661:         if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
                    662:             if ($time < time-300) {
1.783     albertel  663:                 $remove{$key}++;
1.433     matthew   664:             }
                    665:         }
                    666:     }
1.783     albertel  667: 
1.619     albertel  668:     $env{'user.environment'} = "$lonidsdir/$handle.id";
1.780     albertel  669:     $env_loaded=1;
1.783     albertel  670:     foreach my $expired_key (keys(%remove)) {
1.433     matthew   671:         &delenv($expired_key);
1.374     www       672:     }
1.1       albertel  673: }
                    674: 
1.916     albertel  675: # ---------------------------------------------------- Check for valid session 
                    676: sub check_for_valid_session {
1.1355    raeburn   677:     my ($r,$name,$userhashref,$domref) = @_;
1.916     albertel  678:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
1.1378    raeburn   679:     my ($lonidsdir,$linkname,$pubname,$secure,$lonid);
                    680:     if ($name eq 'lonDAV') {
                    681:         $lonidsdir=$r->dir_config('lonDAVsessDir');
                    682:     } else {
                    683:         $lonidsdir=$r->dir_config('lonIDsDir');
                    684:         if ($name eq '') {
                    685:             $name = 'lonID';
                    686:         }
                    687:     }
                    688:     if ($name eq 'lonID') {
                    689:         $secure = 'lonSID';
1.1337    raeburn   690:         $linkname = 'lonLinkID';
                    691:         $pubname = 'lonPubID';
1.1378    raeburn   692:         if (exists($cookies{$secure})) {
                    693:             $lonid=$cookies{$secure};
                    694:         } elsif (exists($cookies{$name})) {
                    695:             $lonid=$cookies{$name};
1.1400    raeburn   696:         } elsif ((exists($cookies{$linkname})) && ($ENV{'SERVER_PORT'} != 443)) {
1.1337    raeburn   697:             $lonid=$cookies{$linkname};
1.1378    raeburn   698:         } elsif (exists($cookies{$pubname})) {
                    699:             $lonid=$cookies{$pubname};
1.1337    raeburn   700:         }
1.1378    raeburn   701:     } else {
                    702:         $lonid=$cookies{$name};
1.1337    raeburn   703:     }
1.916     albertel  704:     return undef if (!$lonid);
                    705: 
                    706:     my $handle=&LONCAPA::clean_handle($lonid->value);
1.1378    raeburn   707:     if (-l "$lonidsdir/$handle.id") {
                    708:         my $link = readlink("$lonidsdir/$handle.id");
                    709:         if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
                    710:             $handle = $1;
                    711:         }
1.1155    raeburn   712:     }
1.1355    raeburn   713:     if (!-e "$lonidsdir/$handle.id") {
                    714:         if ((ref($domref)) && ($name eq 'lonID') && 
                    715:             ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
                    716:             my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
                    717:             if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
                    718:                 $$domref = $possudom;
                    719:             }
                    720:         }
                    721:         return undef;
                    722:     }
1.916     albertel  723: 
1.917     albertel  724:     my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    725:     return undef if (!$opened);
1.916     albertel  726: 
                    727:     flock($idf,LOCK_SH);
                    728:     my %disk_env;
                    729:     if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
                    730: 	    &GDBM_READER(),0640)) {
                    731: 	return undef;	
                    732:     }
                    733: 
                    734:     if (!defined($disk_env{'user.name'})
                    735: 	|| !defined($disk_env{'user.domain'})) {
1.1394    raeburn   736:         untie(%disk_env);
1.916     albertel  737: 	return undef;
                    738:     }
1.1245    raeburn   739: 
                    740:     if (ref($userhashref) eq 'HASH') {
                    741:         $userhashref->{'name'} = $disk_env{'user.name'};
                    742:         $userhashref->{'domain'} = $disk_env{'user.domain'};
1.1448  ! raeburn   743:         if ($disk_env{'request.role'}) {
        !           744:             $userhashref->{'role'} = $disk_env{'request.role'};
        !           745:         }
1.1361    raeburn   746:         $userhashref->{'lti'} = $disk_env{'request.lti.login'};
1.1375    raeburn   747:         if ($userhashref->{'lti'}) {
                    748:             $userhashref->{'ltitarget'} = $disk_env{'request.lti.target'};
                    749:             $userhashref->{'ltiuri'} = $disk_env{'request.lti.uri'};
                    750:         }
1.1212    raeburn   751:     }
1.1394    raeburn   752:     untie(%disk_env);
1.1245    raeburn   753: 
1.916     albertel  754:     return $handle;
                    755: }
                    756: 
1.830     albertel  757: sub timed_flock {
                    758:     my ($file,$lock_type) = @_;
                    759:     my $failed=0;
                    760:     eval {
                    761: 	local $SIG{__DIE__}='DEFAULT';
                    762: 	local $SIG{ALRM}=sub {
                    763: 	    $failed=1;
                    764: 	    die("failed lock");
                    765: 	};
                    766: 	alarm(13);
                    767: 	flock($file,$lock_type);
                    768: 	alarm(0);
                    769:     };
                    770:     if ($failed) {
                    771: 	return undef;
                    772:     } else {
                    773: 	return 1;
                    774:     }
                    775: }
                    776: 
1.1392    raeburn   777: sub get_sessionfile_vars {
                    778:     my ($handle,$lonidsdir,$storearr) = @_;
                    779:     my %returnhash;
                    780:     unless (ref($storearr) eq 'ARRAY') {
                    781:         return %returnhash;
                    782:     }
                    783:     if (-l "$lonidsdir/$handle.id") {
                    784:         my $link = readlink("$lonidsdir/$handle.id");
                    785:         if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
                    786:             $handle = $1;
                    787:         }
                    788:     }
                    789:     if ((-e "$lonidsdir/$handle.id") &&
                    790:         ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
                    791:         my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
                    792:         if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
                    793:             if (open(my $idf,'+<',"$lonidsdir/$handle.id")) {
                    794:                 flock($idf,LOCK_SH);
                    795:                 if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
                    796:                         &GDBM_READER(),0640)) {
                    797:                     foreach my $item (@{$storearr}) {
                    798:                         $returnhash{$item} = $disk_env{$item};
                    799:                     }
                    800:                     untie(%disk_env);
                    801:                 }
                    802:             }
                    803:         }
                    804:     }
                    805:     return %returnhash;
                    806: }
                    807: 
1.5       www       808: # ---------------------------------------------------------- Append Environment
                    809: 
                    810: sub appenv {
1.949     raeburn   811:     my ($newenv,$roles) = @_;
                    812:     if (ref($newenv) eq 'HASH') {
                    813:         foreach my $key (keys(%{$newenv})) {
                    814:             my $refused = 0;
                    815: 	    if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
                    816:                 $refused = 1;
                    817:                 if (ref($roles) eq 'ARRAY') {
1.1250    raeburn   818:                     my ($type,$role) = ($key =~ m{^user\.(role|priv)\.(.+?)\./});
1.949     raeburn   819:                     if (grep(/^\Q$role\E$/,@{$roles})) {
                    820:                         $refused = 0;
                    821:                     }
                    822:                 }
                    823:             }
                    824:             if ($refused) {
                    825:                 &logthis("<font color=\"blue\">WARNING: ".
                    826:                          "Attempt to modify environment ".$key." to ".$newenv->{$key}
                    827:                          .'</font>');
                    828: 	        delete($newenv->{$key});
                    829:             } else {
                    830:                 $env{$key}=$newenv->{$key};
                    831:             }
                    832:         }
1.1376    raeburn   833:         my $lonids = $perlvar{'lonIDsDir'};
                    834:         if ($env{'user.environment'} =~ m{^\Q$lonids/\E$match_username\_\d+\_$match_domain\_[\w\-.]+\.id$}) {
                    835:             my $opened = open(my $env_file,'+<',$env{'user.environment'});
                    836:             if ($opened
                    837: 	        && &timed_flock($env_file,LOCK_EX)
                    838: 	        &&
                    839: 	        tie(my %disk_env,'GDBM_File',$env{'user.environment'},
                    840: 	            (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
                    841: 	        while (my ($key,$value) = each(%{$newenv})) {
                    842: 	            $disk_env{$key} = $value;
                    843: 	        }
                    844: 	        untie(%disk_env);
                    845:             }
1.35      www       846:         }
1.191     harris41  847:     }
1.56      www       848:     return 'ok';
                    849: }
                    850: # ----------------------------------------------------- Delete from Environment
                    851: 
                    852: sub delenv {
1.1104    raeburn   853:     my ($delthis,$regexp,$roles) = @_;
                    854:     if (($delthis=~/^user\.role/) || ($delthis=~/^user\.priv/)) {
                    855:         my $refused = 1;
                    856:         if (ref($roles) eq 'ARRAY') {
                    857:             my ($type,$role) = ($delthis =~ /^user\.(role|priv)\.([^.]+)\./);
                    858:             if (grep(/^\Q$role\E$/,@{$roles})) {
                    859:                 $refused = 0;
                    860:             }
                    861:         }
                    862:         if ($refused) {
                    863:             &logthis("<font color=\"blue\">WARNING: ".
                    864:                      "Attempt to delete from environment ".$delthis);
                    865:             return 'error';
                    866:         }
1.56      www       867:     }
1.917     albertel  868:     my $opened = open(my $env_file,'+<',$env{'user.environment'});
                    869:     if ($opened
1.915     albertel  870: 	&& &timed_flock($env_file,LOCK_EX)
1.830     albertel  871: 	&&
                    872: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
                    873: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783     albertel  874: 	foreach my $key (keys(%disk_env)) {
1.987     raeburn   875: 	    if ($regexp) {
                    876:                 if ($key=~/^$delthis/) {
                    877:                     delete($env{$key});
                    878:                     delete($disk_env{$key});
                    879:                 } 
                    880:             } else {
                    881:                 if ($key=~/^\Q$delthis\E/) {
                    882: 		    delete($env{$key});
                    883: 		    delete($disk_env{$key});
                    884: 	        }
                    885:             }
1.448     albertel  886: 	}
1.783     albertel  887: 	untie(%disk_env);
1.5       www       888:     }
                    889:     return 'ok';
1.369     albertel  890: }
                    891: 
1.790     albertel  892: sub get_env_multiple {
                    893:     my ($name) = @_;
                    894:     my @values;
                    895:     if (defined($env{$name})) {
                    896:         # exists is it an array
                    897:         if (ref($env{$name})) {
                    898:             @values=@{ $env{$name} };
                    899:         } else {
                    900:             $values[0]=$env{$name};
                    901:         }
                    902:     }
                    903:     return(@values);
                    904: }
                    905: 
1.958     www       906: # ------------------------------------------------------------------- Locking
                    907: 
                    908: sub set_lock {
                    909:     my ($text)=@_;
                    910:     $locknum++;
                    911:     my $id=$$.'-'.$locknum;
                    912:     &appenv({'session.locks' => $env{'session.locks'}.','.$id,
                    913:              'session.lock.'.$id => $text});
                    914:     return $id;
                    915: }
                    916: 
                    917: sub get_locks {
                    918:     my $num=0;
                    919:     my %texts=();
                    920:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                    921:        if ($lock=~/\w/) {
                    922:           $num++;
                    923:           $texts{$lock}=$env{'session.lock.'.$lock};
                    924:        }
                    925:    }
                    926:    return ($num,%texts);
                    927: }
                    928: 
                    929: sub remove_lock {
                    930:     my ($id)=@_;
                    931:     my $newlocks='';
                    932:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                    933:        if (($lock=~/\w/) && ($lock ne $id)) {
                    934:           $newlocks.=','.$lock;
                    935:        }
                    936:     }
                    937:     &appenv({'session.locks' => $newlocks});
                    938:     &delenv('session.lock.'.$id);
                    939: }
                    940: 
                    941: sub remove_all_locks {
                    942:     my $activelocks=$env{'session.locks'};
                    943:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                    944:        if ($lock=~/\w/) {
                    945:           &remove_lock($lock);
                    946:        }
                    947:     }
                    948: }
                    949: 
                    950: 
1.369     albertel  951: # ------------------------------------------ Find out current server userload
                    952: sub userload {
                    953:     my $numusers=0;
                    954:     {
                    955: 	opendir(LONIDS,$perlvar{'lonIDsDir'});
                    956: 	my $filename;
                    957: 	my $curtime=time;
                    958: 	while ($filename=readdir(LONIDS)) {
1.925     albertel  959: 	    next if ($filename eq '.' || $filename eq '..');
                    960: 	    next if ($filename =~ /publicuser_\d+\.id/);
1.1390    raeburn   961:             next if ($filename =~ /^[a-f0-9]+_linked\.id$/);
1.404     albertel  962: 	    my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
1.437     albertel  963: 	    if ($curtime-$mtime < 1800) { $numusers++; }
1.369     albertel  964: 	}
                    965: 	closedir(LONIDS);
                    966:     }
                    967:     my $userloadpercent=0;
                    968:     my $maxuserload=$perlvar{'lonUserLoadLim'};
                    969:     if ($maxuserload) {
1.371     albertel  970: 	$userloadpercent=100*$numusers/$maxuserload;
1.369     albertel  971:     }
1.372     albertel  972:     $userloadpercent=sprintf("%.2f",$userloadpercent);
1.369     albertel  973:     return $userloadpercent;
1.283     www       974: }
                    975: 
1.1       albertel  976: # ------------------------------ Find server with least workload from spare.tab
1.11      www       977: 
1.1       albertel  978: sub spareserver {
1.1083    raeburn   979:     my ($loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
1.784     albertel  980:     my $spare_server;
1.370     albertel  981:     if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
1.784     albertel  982:     my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent 
                    983:                                                      :  $userloadpercent;
1.1083    raeburn   984:     my ($uint_dom,$remotesessions);
                    985:     if (($udom ne '') && (&domain($udom) ne '')) {
                    986:         my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
                    987:         $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
                    988:         my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
                    989:         $remotesessions = $udomdefaults{'remotesessions'};
                    990:     }
1.1123    raeburn   991:     my $spareshash = &this_host_spares($udom);
                    992:     if (ref($spareshash) eq 'HASH') {
                    993:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
                    994:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
1.1279    raeburn   995:                 next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
                    996:                                              $try_server));
1.1123    raeburn   997: 	        ($spare_server, $lowest_load) =
                    998: 	            &compare_server_load($try_server, $spare_server, $lowest_load);
                    999:             }
1.1083    raeburn  1000:         }
1.784     albertel 1001: 
1.1123    raeburn  1002:         my $found_server = ($spare_server ne '' && $lowest_load < 100);
                   1003: 
                   1004:         if (!$found_server) {
                   1005:             if (ref($spareshash->{'default'}) eq 'ARRAY') { 
                   1006: 	        foreach my $try_server (@{ $spareshash->{'default'} }) {
1.1279    raeburn  1007:                     next unless (&spare_can_host($udom,$uint_dom,
                   1008:                                                  $remotesessions,$try_server));
1.1123    raeburn  1009: 	            ($spare_server, $lowest_load) =
                   1010: 		        &compare_server_load($try_server, $spare_server, $lowest_load);
                   1011:                 }
                   1012: 	    }
                   1013:         }
1.784     albertel 1014:     }
                   1015: 
                   1016:     if (!$want_server_name) {
1.1001    raeburn  1017:         if (defined($spare_server)) {
                   1018:             my $hostname = &hostname($spare_server);
1.1083    raeburn  1019:             if (defined($hostname)) {
1.1397    raeburn  1020:                 my $protocol = 'http';
                   1021:                 if ($protocol{$spare_server} eq 'https') {
                   1022:                     $protocol = $protocol{$spare_server};
                   1023:                 }
1.1001    raeburn  1024: 	        $spare_server = $protocol.'://'.$hostname;
                   1025:             }
                   1026:         }
1.784     albertel 1027:     }
                   1028:     return $spare_server;
                   1029: }
                   1030: 
                   1031: sub compare_server_load {
1.1253    raeburn  1032:     my ($try_server, $spare_server, $lowest_load, $required) = @_;
                   1033: 
                   1034:     if ($required) {
                   1035:         my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/);
                   1036:         my $remoterev = &get_server_loncaparev(undef,$try_server);
                   1037:         my ($major,$minor) = ($remoterev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
                   1038:         if (($major eq '' && $minor eq '') ||
                   1039:             (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) {
                   1040:             return ($spare_server,$lowest_load);
                   1041:         }
                   1042:     }
1.784     albertel 1043: 
                   1044:     my $loadans     = &reply('load',    $try_server);
                   1045:     my $userloadans = &reply('userload',$try_server);
                   1046: 
                   1047:     if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
1.1114    raeburn  1048: 	return ($spare_server, $lowest_load); #didn't get a number from the server
1.784     albertel 1049:     }
                   1050: 
                   1051:     my $load;
                   1052:     if ($loadans =~ /\d/) {
                   1053: 	if ($userloadans =~ /\d/) {
                   1054: 	    #both are numbers, pick the bigger one
                   1055: 	    $load = ($loadans > $userloadans) ? $loadans 
                   1056: 		                              : $userloadans;
1.411     albertel 1057: 	} else {
1.784     albertel 1058: 	    $load = $loadans;
1.411     albertel 1059: 	}
1.784     albertel 1060:     } else {
                   1061: 	$load = $userloadans;
                   1062:     }
                   1063: 
                   1064:     if (($load =~ /\d/) && ($load < $lowest_load)) {
                   1065: 	$spare_server = $try_server;
                   1066: 	$lowest_load  = $load;
1.370     albertel 1067:     }
1.784     albertel 1068:     return ($spare_server,$lowest_load);
1.202     matthew  1069: }
1.914     albertel 1070: 
                   1071: # --------------------------- ask offload servers if user already has a session
                   1072: sub find_existing_session {
                   1073:     my ($udom,$uname) = @_;
1.1123    raeburn  1074:     my $spareshash = &this_host_spares($udom);
                   1075:     if (ref($spareshash) eq 'HASH') {
                   1076:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
                   1077:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
                   1078:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
                   1079:             }
                   1080:         }
                   1081:         if (ref($spareshash->{'default'}) eq 'ARRAY') {
                   1082:             foreach my $try_server (@{ $spareshash->{'default'} }) {
                   1083:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
                   1084:             }
                   1085:         }
1.914     albertel 1086:     }
                   1087:     return;
                   1088: }
                   1089: 
1.1411    raeburn  1090: sub delusersession {
                   1091:     my ($lonid,$udom,$uname) = @_;
                   1092:     my $uprimary_id = &domain($udom,'primary');
                   1093:     my $uintdom = &internet_dom($uprimary_id);
                   1094:     my $intdom = &internet_dom($lonid);
                   1095:     my $serverhomedom = &host_domain($lonid);
                   1096:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
                   1097:         return &reply(join(':','delusersession',
                   1098:                             map {&escape($_)} ($udom,$uname)),$lonid);
                   1099:     }
                   1100:     return;
                   1101: }
                   1102: 
1.1389    raeburn  1103: # check if user's browser sent load balancer cookie and server still has session
                   1104: # and is not overloaded.
                   1105: sub check_for_balancer_cookie {
                   1106:     my ($r,$update_mtime) = @_;
                   1107:     my ($otherserver,$cookie);
                   1108:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
                   1109:     if (exists($cookies{'balanceID'})) {
                   1110:         my $balid = $cookies{'balanceID'};
                   1111:         $cookie=&LONCAPA::clean_handle($balid->value);
                   1112:         my $balancedir=$r->dir_config('lonBalanceDir');
                   1113:         if ((-d $balancedir) && (-e "$balancedir/$cookie.id")) {
                   1114:             if ($cookie =~ /^($match_domain)_($match_username)_[a-f0-9]+$/) {
                   1115:                 my ($possudom,$possuname) = ($1,$2);
                   1116:                 my $has_session = 0;
                   1117:                 if ((&domain($possudom) ne '') &&
                   1118:                     (&homeserver($possuname,$possudom) ne 'no_host')) {
                   1119:                     my $try_server;
                   1120:                     my $opened = open(my $idf,'+<',"$balancedir/$cookie.id");
                   1121:                     if ($opened) {
                   1122:                         flock($idf,LOCK_SH);
                   1123:                         while (my $line = <$idf>) {
                   1124:                             chomp($line);
                   1125:                             if (&hostname($line) ne '') {
                   1126:                                 $try_server = $line;
                   1127:                                 last;
                   1128:                             }
                   1129:                         }
                   1130:                         close($idf);
                   1131:                         if (($try_server) &&
                   1132:                             (&has_user_session($try_server,$possudom,$possuname))) {
                   1133:                             my $lowest_load = 30000;
                   1134:                             ($otherserver,$lowest_load) =
                   1135:                                 &compare_server_load($try_server,undef,$lowest_load);
                   1136:                             if ($otherserver ne '' && $lowest_load < 100) {
                   1137:                                 $has_session = 1;
                   1138:                             } else {
                   1139:                                 undef($otherserver);
                   1140:                             }
                   1141:                         }
                   1142:                     }
                   1143:                 }
                   1144:                 if ($has_session) {
                   1145:                     if ($update_mtime) {
                   1146:                         my $atime = my $mtime = time;
                   1147:                         utime($atime,$mtime,"$balancedir/$cookie.id");
                   1148:                     }
                   1149:                 } else {
                   1150:                     unlink("$balancedir/$cookie.id");
                   1151:                 }
                   1152:             }
                   1153:         }
                   1154:     }
                   1155:     return ($otherserver,$cookie);
                   1156: }
                   1157: 
1.1431    raeburn  1158: sub updatebalcookie {
                   1159:     my ($cookie,$balancer,$lastentry)=@_;
                   1160:     if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
                   1161:         my ($udom,$uname) = ($1,$2);
                   1162:         my $uprimary_id = &domain($udom,'primary');
                   1163:         my $uintdom = &internet_dom($uprimary_id);
                   1164:         my $intdom = &internet_dom($balancer);
                   1165:         my $serverhomedom = &host_domain($balancer);
                   1166:         if (($uintdom ne '') && ($uintdom eq $intdom)) {
                   1167:             return &reply('updatebalcookie:'.&escape($cookie).':'.&escape($lastentry),$balancer);
                   1168:         }
                   1169:     }
                   1170:     return;
                   1171: }
                   1172: 
1.1389    raeburn  1173: sub delbalcookie {
                   1174:     my ($cookie,$balancer) =@_;
                   1175:     if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
                   1176:         my ($udom,$uname) = ($1,$2);
                   1177:         my $uprimary_id = &domain($udom,'primary');
                   1178:         my $uintdom = &internet_dom($uprimary_id);
                   1179:         my $intdom = &internet_dom($balancer);
                   1180:         my $serverhomedom = &host_domain($balancer);
                   1181:         if (($uintdom ne '') && ($uintdom eq $intdom)) {
1.1431    raeburn  1182:             return &reply('delbalcookie:'.&escape($cookie),$balancer);
1.1389    raeburn  1183:         }
                   1184:     }
                   1185: }
                   1186: 
1.914     albertel 1187: # -------------------------------- ask if server already has a session for user
                   1188: sub has_user_session {
                   1189:     my ($lonid,$udom,$uname) = @_;
                   1190:     my $result = &reply(join(':','userhassession',
                   1191: 			     map {&escape($_)} ($udom,$uname)),$lonid);
                   1192:     return 1 if ($result eq 'ok');
                   1193: 
                   1194:     return 0;
                   1195: }
                   1196: 
1.1076    raeburn  1197: # --------- determine least loaded server in a user's domain which allows login
                   1198: 
                   1199: sub choose_server {
1.1259    raeburn  1200:     my ($udom,$checkloginvia,$required,$skiploadbal) = @_;
1.1076    raeburn  1201:     my %domconfhash = &Apache::loncommon::get_domainconf($udom);
1.1077    raeburn  1202:     my %servers = &get_servers($udom);
1.1076    raeburn  1203:     my $lowest_load = 30000;
1.1259    raeburn  1204:     my ($login_host,$hostname,$portal_path,$isredirect,$balancers);
                   1205:     if ($skiploadbal) {
                   1206:         ($balancers,my $cached)=&is_cached_new('loadbalancing',$udom);
                   1207:         unless (defined($cached)) {
                   1208:             my $cachetime = 60*60*24;
                   1209:             my %domconfig =
                   1210:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
                   1211:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
                   1212:                 $balancers = &do_cache_new('loadbalancing',$udom,$domconfig{'loadbalancing'},
                   1213:                                            $cachetime);
                   1214:             }
                   1215:         }
                   1216:     }
1.1076    raeburn  1217:     foreach my $lonhost (keys(%servers)) {
1.1259    raeburn  1218:         if ($skiploadbal) {
                   1219:             if (ref($balancers) eq 'HASH') {
                   1220:                 next if (exists($balancers->{$lonhost}));
                   1221:             }
1.1389    raeburn  1222:         }
1.1115    raeburn  1223:         my $loginvia;
                   1224:         if ($checkloginvia) {
                   1225:             $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
1.1116    raeburn  1226:             if ($loginvia) {
                   1227:                 my ($server,$path) = split(/:/,$loginvia);
                   1228:                 ($login_host, $lowest_load) =
1.1253    raeburn  1229:                     &compare_server_load($server, $login_host, $lowest_load, $required);
1.1116    raeburn  1230:                 if ($login_host eq $server) {
                   1231:                     $portal_path = $path;
1.1151    raeburn  1232:                     $isredirect = 1;
1.1116    raeburn  1233:                 }
                   1234:             } else {
                   1235:                 ($login_host, $lowest_load) =
1.1253    raeburn  1236:                     &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1.1116    raeburn  1237:                 if ($login_host eq $lonhost) {
                   1238:                     $portal_path = '';
1.1151    raeburn  1239:                     $isredirect = ''; 
1.1116    raeburn  1240:                 }
                   1241:             }
                   1242:         } else {
1.1076    raeburn  1243:             ($login_host, $lowest_load) =
1.1253    raeburn  1244:                 &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1.1076    raeburn  1245:         }
                   1246:     }
                   1247:     if ($login_host ne '') {
1.1116    raeburn  1248:         $hostname = &hostname($login_host);
1.1076    raeburn  1249:     }
1.1331    raeburn  1250:     return ($login_host,$hostname,$portal_path,$isredirect,$lowest_load);
1.1076    raeburn  1251: }
                   1252: 
1.1420    raeburn  1253: sub get_course_sessions {
                   1254:     my ($cnum,$cdom,$lastactivity) = @_;
                   1255:     my %servers = &internet_dom_servers($cdom);
                   1256:     my %returnhash;
                   1257:     foreach my $server (sort(keys(%servers))) {
                   1258:         my $rep = &reply("coursesessions:$cdom:$cnum:$lastactivity",$server);
                   1259:         my @pairs=split(/\&/,$rep);
                   1260:         unless (($rep eq 'unknown_cmd') || ($rep =~ /^error/)) {
                   1261:             foreach my $item (@pairs) {
                   1262:                 my ($key,$value)=split(/=/,$item,2);
                   1263:                 $key = &unescape($key);
                   1264:                 next if ($key =~ /^error: 2 /);
                   1265:                 if (exists($returnhash{$key})) {
                   1266:                     next if ($value < $returnhash{$key});
                   1267:                 }
                   1268:                 $returnhash{$key}=$value;
                   1269:             }
                   1270:         }
                   1271:     }
                   1272:     return %returnhash;
                   1273: }
                   1274: 
1.202     matthew  1275: # --------------------------------------------- Try to change a user's password
                   1276: 
                   1277: sub changepass {
1.799     raeburn  1278:     my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
1.202     matthew  1279:     $currentpass = &escape($currentpass);
                   1280:     $newpass     = &escape($newpass);
1.1030    raeburn  1281:     my $lonhost = $perlvar{'lonHostID'};
                   1282:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
1.202     matthew  1283: 		       $server);
                   1284:     if (! $answer) {
                   1285: 	&logthis("No reply on password change request to $server ".
                   1286: 		 "by $uname in domain $udom.");
                   1287:     } elsif ($answer =~ "^ok") {
                   1288:         &logthis("$uname in $udom successfully changed their password ".
                   1289: 		 "on $server.");
                   1290:     } elsif ($answer =~ "^pwchange_failure") {
                   1291: 	&logthis("$uname in $udom was unable to change their password ".
                   1292: 		 "on $server.  The action was blocked by either lcpasswd ".
                   1293: 		 "or pwchange");
                   1294:     } elsif ($answer =~ "^non_authorized") {
                   1295:         &logthis("$uname in $udom did not get their password correct when ".
                   1296: 		 "attempting to change it on $server.");
                   1297:     } elsif ($answer =~ "^auth_mode_error") {
                   1298:         &logthis("$uname in $udom attempted to change their password despite ".
                   1299: 		 "not being locally or internally authenticated on $server.");
                   1300:     } elsif ($answer =~ "^unknown_user") {
                   1301:         &logthis("$uname in $udom attempted to change their password ".
                   1302: 		 "on $server but were unable to because $server is not ".
                   1303: 		 "their home server.");
                   1304:     } elsif ($answer =~ "^refused") {
                   1305: 	&logthis("$server refused to change $uname in $udom password because ".
                   1306: 		 "it was sent an unencrypted request to change the password.");
1.1030    raeburn  1307:     } elsif ($answer =~ "invalid_client") {
                   1308:         &logthis("$server refused to change $uname in $udom password because ".
                   1309:                  "it was a reset by e-mail originating from an invalid server.");
1.1408    raeburn  1310:     } elsif ($answer =~ "^prioruse") {
                   1311:        &logthis("$server refused to change $uname in $udom password because ".
                   1312:                 "the password had been used before");
1.202     matthew  1313:     }
                   1314:     return $answer;
1.1       albertel 1315: }
                   1316: 
1.169     harris41 1317: # ----------------------- Try to determine user's current authentication scheme
                   1318: 
                   1319: sub queryauthenticate {
                   1320:     my ($uname,$udom)=@_;
1.456     albertel 1321:     my $uhome=&homeserver($uname,$udom);
                   1322:     if (!$uhome) {
                   1323: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
                   1324: 	return 'no_host';
                   1325:     }
                   1326:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
                   1327:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
                   1328: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.169     harris41 1329:     }
1.456     albertel 1330:     return $answer;
1.169     harris41 1331: }
                   1332: 
1.1       albertel 1333: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11      www      1334: 
1.1       albertel 1335: sub authenticate {
1.1073    raeburn  1336:     my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
1.807     albertel 1337:     $upass=&escape($upass);
                   1338:     $uname= &LONCAPA::clean_username($uname);
1.836     www      1339:     my $uhome=&homeserver($uname,$udom,1);
1.952     raeburn  1340:     my $newhome;
1.836     www      1341:     if ((!$uhome) || ($uhome eq 'no_host')) {
                   1342: # Maybe the machine was offline and only re-appeared again recently?
                   1343:         &reconlonc();
                   1344: # One more
1.952     raeburn  1345: 	$uhome=&homeserver($uname,$udom,1);
                   1346:         if (($uhome eq 'no_host') && $checkdefauth) {
                   1347:             if (defined(&domain($udom,'primary'))) {
                   1348:                 $newhome=&domain($udom,'primary');
                   1349:             }
                   1350:             if ($newhome ne '') {
                   1351:                 $uhome = $newhome;
                   1352:             }
                   1353:         }
1.836     www      1354: 	if ((!$uhome) || ($uhome eq 'no_host')) {
                   1355: 	    &logthis("User $uname at $udom is unknown in authenticate");
1.952     raeburn  1356: 	    return 'no_host';
                   1357:         }
1.1       albertel 1358:     }
1.1073    raeburn  1359:     my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
1.471     albertel 1360:     if ($answer eq 'authorized') {
1.952     raeburn  1361:         if ($newhome) {
                   1362:             &logthis("User $uname at $udom authorized by $uhome, but needs account");
                   1363:             return 'no_account_on_host'; 
                   1364:         } else {
                   1365:             &logthis("User $uname at $udom authorized by $uhome");
                   1366:             return $uhome;
                   1367:         }
1.471     albertel 1368:     }
                   1369:     if ($answer eq 'non_authorized') {
                   1370: 	&logthis("User $uname at $udom rejected by $uhome");
                   1371: 	return 'no_host'; 
1.9       www      1372:     }
1.471     albertel 1373:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.1       albertel 1374:     return 'no_host';
                   1375: }
                   1376: 
1.1073    raeburn  1377: sub can_host_session {
1.1074    raeburn  1378:     my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
1.1073    raeburn  1379:     my $canhost = 1;
1.1074    raeburn  1380:     my $host_idn = &Apache::lonnet::internet_dom($lonhost);
1.1073    raeburn  1381:     if (ref($remotesessions) eq 'HASH') {
                   1382:         if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
1.1074    raeburn  1383:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
1.1073    raeburn  1384:                 $canhost = 0;
                   1385:             } else {
                   1386:                 $canhost = 1;
                   1387:             }
                   1388:         }
                   1389:         if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
1.1074    raeburn  1390:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
1.1073    raeburn  1391:                 $canhost = 1;
                   1392:             } else {
                   1393:                 $canhost = 0;
                   1394:             }
                   1395:         }
                   1396:         if ($canhost) {
                   1397:             if ($remotesessions->{'version'} ne '') {
                   1398:                 my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
                   1399:                 if ($reqmajor ne '' && $reqminor ne '') {
                   1400:                     if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
                   1401:                         my $major = $1;
                   1402:                         my $minor = $2;
                   1403:                         if (($major < $reqmajor ) ||
                   1404:                             (($major == $reqmajor) && ($minor < $reqminor))) {
                   1405:                             $canhost = 0;
                   1406:                         }
                   1407:                     } else {
                   1408:                         $canhost = 0;
                   1409:                     }
                   1410:                 }
                   1411:             }
                   1412:         }
                   1413:     }
                   1414:     if ($canhost) {
                   1415:         if (ref($hostedsessions) eq 'HASH') {
1.1120    raeburn  1416:             my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
                   1417:             my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
1.1073    raeburn  1418:             if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
1.1120    raeburn  1419:                 if (($uint_dom ne '') && 
                   1420:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
1.1073    raeburn  1421:                     $canhost = 0;
                   1422:                 } else {
                   1423:                     $canhost = 1;
                   1424:                 }
                   1425:             }
                   1426:             if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
1.1120    raeburn  1427:                 if (($uint_dom ne '') && 
                   1428:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
1.1073    raeburn  1429:                     $canhost = 1;
                   1430:                 } else {
                   1431:                     $canhost = 0;
                   1432:                 }
                   1433:             }
                   1434:         }
                   1435:     }
                   1436:     return $canhost;
                   1437: }
                   1438: 
1.1083    raeburn  1439: sub spare_can_host {
                   1440:     my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
                   1441:     my $canhost=1;
1.1279    raeburn  1442:     my $try_server_hostname = &hostname($try_server);
                   1443:     my $serverhomeID = &get_server_homeID($try_server_hostname);
                   1444:     my $serverhomedom = &host_domain($serverhomeID);
                   1445:     my %defdomdefaults = &get_domain_defaults($serverhomedom);
                   1446:     if (ref($defdomdefaults{'offloadnow'}) eq 'HASH') {
                   1447:         if ($defdomdefaults{'offloadnow'}{$try_server}) {
                   1448:             $canhost = 0;
                   1449:         }
                   1450:     }
1.1439    raeburn  1451:     if ($canhost) {
                   1452:         if (ref($defdomdefaults{'offloadoth'}) eq 'HASH') {
                   1453:             if ($defdomdefaults{'offloadoth'}{$try_server}) {
                   1454:                 unless (&shared_institution($udom,$try_server)) {
                   1455:                     $canhost = 0;
                   1456:                 }
                   1457:             }
                   1458:         }
                   1459:     }
1.1279    raeburn  1460:     if (($canhost) && ($uint_dom)) {
                   1461:         my @intdoms;
                   1462:         my $internet_names = &get_internet_names($try_server);
                   1463:         if (ref($internet_names) eq 'ARRAY') {
                   1464:             @intdoms = @{$internet_names};
                   1465:         }
                   1466:         unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
                   1467:             my $remoterev = &get_server_loncaparev(undef,$try_server);
                   1468:             $canhost = &can_host_session($udom,$try_server,$remoterev,
                   1469:                                          $remotesessions,
                   1470:                                          $defdomdefaults{'hostedsessions'});
                   1471:         }
1.1083    raeburn  1472:     }
                   1473:     return $canhost;
                   1474: }
                   1475: 
1.1123    raeburn  1476: sub this_host_spares {
                   1477:     my ($dom) = @_;
1.1126    raeburn  1478:     my ($dom_in_use,$lonhost_in_use,$result);
1.1123    raeburn  1479:     my @hosts = &current_machine_ids();
                   1480:     foreach my $lonhost (@hosts) {
                   1481:         if (&host_domain($lonhost) eq $dom) {
1.1126    raeburn  1482:             $dom_in_use = $dom;
                   1483:             $lonhost_in_use = $lonhost;
1.1123    raeburn  1484:             last;
                   1485:         }
                   1486:     }
1.1126    raeburn  1487:     if ($dom_in_use ne '') {
                   1488:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
                   1489:     }
                   1490:     if (ref($result) ne 'HASH') {
                   1491:         $lonhost_in_use = $perlvar{'lonHostID'};
                   1492:         $dom_in_use = &host_domain($lonhost_in_use);
                   1493:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
                   1494:         if (ref($result) ne 'HASH') {
                   1495:             $result = \%spareid;
                   1496:         }
                   1497:     }
                   1498:     return $result;
                   1499: }
                   1500: 
                   1501: sub spares_for_offload  {
                   1502:     my ($dom_in_use,$lonhost_in_use) = @_;
                   1503:     my ($result,$cached)=&is_cached_new('spares',$dom_in_use);
1.1123    raeburn  1504:     if (defined($cached)) {
                   1505:         return $result;
                   1506:     } else {
1.1126    raeburn  1507:         my $cachetime = 60*60*24;
                   1508:         my %domconfig =
                   1509:             &Apache::lonnet::get_dom('configuration',['usersessions'],$dom_in_use);
                   1510:         if (ref($domconfig{'usersessions'}) eq 'HASH') {
                   1511:             if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
                   1512:                 if (ref($domconfig{'usersessions'}{'spares'}{$lonhost_in_use}) eq 'HASH') {
                   1513:                     return &do_cache_new('spares',$dom_in_use,$domconfig{'usersessions'}{'spares'}{$lonhost_in_use},$cachetime);
1.1123    raeburn  1514:                 }
                   1515:             }
                   1516:         }
                   1517:     }
1.1126    raeburn  1518:     return;
1.1123    raeburn  1519: }
                   1520: 
1.1129    raeburn  1521: sub get_lonbalancer_config {
                   1522:     my ($servers) = @_;
                   1523:     my ($currbalancer,$currtargets);
                   1524:     if (ref($servers) eq 'HASH') {
                   1525:         foreach my $server (keys(%{$servers})) {
                   1526:             my %what = (
                   1527:                          spareid => 1,
                   1528:                          perlvar => 1,
                   1529:                        );
                   1530:             my ($result,$returnhash) = &get_remote_globals($server,\%what);
                   1531:             if ($result eq 'ok') {
                   1532:                 if (ref($returnhash) eq 'HASH') {
                   1533:                     if (ref($returnhash->{'perlvar'}) eq 'HASH') {
                   1534:                         if ($returnhash->{'perlvar'}->{'lonBalancer'} eq 'yes') {
                   1535:                             $currbalancer = $server;
                   1536:                             $currtargets = {};
                   1537:                             if (ref($returnhash->{'spareid'}) eq 'HASH') {
                   1538:                                 if (ref($returnhash->{'spareid'}->{'primary'}) eq 'ARRAY') {
                   1539:                                     $currtargets->{'primary'} = $returnhash->{'spareid'}->{'primary'};
                   1540:                                 }
                   1541:                                 if (ref($returnhash->{'spareid'}->{'default'}) eq 'ARRAY') {
                   1542:                                     $currtargets->{'default'} = $returnhash->{'spareid'}->{'default'};
                   1543:                                 }
                   1544:                             }
                   1545:                             last;
                   1546:                         }
                   1547:                     }
                   1548:                 }
                   1549:             }
                   1550:         }
                   1551:     }
                   1552:     return ($currbalancer,$currtargets);
                   1553: }
                   1554: 
                   1555: sub check_loadbalancing {
1.1331    raeburn  1556:     my ($uname,$udom,$caller) = @_;
1.1191    raeburn  1557:     my ($is_balancer,$currtargets,$currrules,$dom_in_use,$homeintdom,
1.1391    raeburn  1558:         $rule_in_effect,$offloadto,$otherserver,$setcookie,$dom_balancers);
1.1129    raeburn  1559:     my $lonhost = $perlvar{'lonHostID'};
1.1175    raeburn  1560:     my @hosts = &current_machine_ids();
1.1129    raeburn  1561:     my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
                   1562:     my $uintdom = &Apache::lonnet::internet_dom($uprimary_id);
                   1563:     my $intdom = &Apache::lonnet::internet_dom($lonhost);
                   1564:     my $serverhomedom = &host_domain($lonhost);
1.1307    raeburn  1565:     my $domneedscache;
1.1129    raeburn  1566:     my $cachetime = 60*60*24;
                   1567: 
                   1568:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
                   1569:         $dom_in_use = $udom;
                   1570:         $homeintdom = 1;
                   1571:     } else {
                   1572:         $dom_in_use = $serverhomedom;
                   1573:     }
                   1574:     my ($result,$cached)=&is_cached_new('loadbalancing',$dom_in_use);
                   1575:     unless (defined($cached)) {
                   1576:         my %domconfig =
                   1577:             &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
                   1578:         if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.1130    raeburn  1579:             $result = &do_cache_new('loadbalancing',$dom_in_use,$domconfig{'loadbalancing'},$cachetime);
1.1307    raeburn  1580:         } else {
                   1581:             $domneedscache = $dom_in_use;
1.1129    raeburn  1582:         }
                   1583:     }
                   1584:     if (ref($result) eq 'HASH') {
1.1391    raeburn  1585:         ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
1.1191    raeburn  1586:             &check_balancer_result($result,@hosts);
1.1129    raeburn  1587:         if ($is_balancer) {
                   1588:             if (ref($currrules) eq 'HASH') {
                   1589:                 if ($homeintdom) {
                   1590:                     if ($uname ne '') {
                   1591:                         if (($currrules->{'_LC_adv'} ne '') || ($currrules->{'_LC_author'} ne '')) {
                   1592:                             my ($is_adv,$is_author) = &is_advanced_user($udom,$uname);
                   1593:                             if (($currrules->{'_LC_author'} ne '') && ($is_author)) {
                   1594:                                 $rule_in_effect = $currrules->{'_LC_author'};
                   1595:                             } elsif (($currrules->{'_LC_adv'} ne '') && ($is_adv)) {
                   1596:                                 $rule_in_effect = $currrules->{'_LC_adv'}
                   1597:                             }
                   1598:                         }
                   1599:                         if ($rule_in_effect eq '') {
                   1600:                             my %userenv = &userenvironment($udom,$uname,'inststatus');
                   1601:                             if ($userenv{'inststatus'} ne '') {
                   1602:                                 my @statuses = map { &unescape($_); } split(/:/,$userenv{'inststatus'});
                   1603:                                 my ($othertitle,$usertypes,$types) =
                   1604:                                     &Apache::loncommon::sorted_inst_types($udom);
                   1605:                                 if (ref($types) eq 'ARRAY') {
                   1606:                                     foreach my $type (@{$types}) {
                   1607:                                         if (grep(/^\Q$type\E$/,@statuses)) {
                   1608:                                             if (exists($currrules->{$type})) {
                   1609:                                                 $rule_in_effect = $currrules->{$type};
                   1610:                                             }
                   1611:                                         }
                   1612:                                     }
                   1613:                                 }
                   1614:                             } else {
                   1615:                                 if (exists($currrules->{'default'})) {
                   1616:                                     $rule_in_effect = $currrules->{'default'};
                   1617:                                 }
                   1618:                             }
                   1619:                         }
                   1620:                     } else {
                   1621:                         if (exists($currrules->{'default'})) {
                   1622:                             $rule_in_effect = $currrules->{'default'};
                   1623:                         }
                   1624:                     }
                   1625:                 } else {
                   1626:                     if ($currrules->{'_LC_external'} ne '') {
                   1627:                         $rule_in_effect = $currrules->{'_LC_external'};
                   1628:                     }
                   1629:                 }
                   1630:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
                   1631:                                                        $uname,$udom);
                   1632:             }
                   1633:         }
                   1634:     } elsif (($homeintdom) && ($udom ne $serverhomedom)) {
1.1239    raeburn  1635:         ($result,$cached)=&is_cached_new('loadbalancing',$serverhomedom);
1.1129    raeburn  1636:         unless (defined($cached)) {
                   1637:             my %domconfig =
                   1638:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$serverhomedom);
                   1639:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.1307    raeburn  1640:                 $result = &do_cache_new('loadbalancing',$serverhomedom,$domconfig{'loadbalancing'},$cachetime);
                   1641:             } else {
                   1642:                 $domneedscache = $serverhomedom;
1.1129    raeburn  1643:             }
                   1644:         }
                   1645:         if (ref($result) eq 'HASH') {
1.1391    raeburn  1646:             ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
1.1191    raeburn  1647:                 &check_balancer_result($result,@hosts);
                   1648:             if ($is_balancer) {
1.1129    raeburn  1649:                 if (ref($currrules) eq 'HASH') {
                   1650:                     if ($currrules->{'_LC_internetdom'} ne '') {
                   1651:                         $rule_in_effect = $currrules->{'_LC_internetdom'};
                   1652:                     }
                   1653:                 }
                   1654:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
                   1655:                                                        $uname,$udom);
                   1656:             }
                   1657:         } else {
                   1658:             if ($perlvar{'lonBalancer'} eq 'yes') {
                   1659:                 $is_balancer = 1;
                   1660:                 $offloadto = &this_host_spares($dom_in_use);
                   1661:             }
1.1307    raeburn  1662:             unless (defined($cached)) {
                   1663:                 $domneedscache = $serverhomedom;
                   1664:             }
1.1129    raeburn  1665:         }
                   1666:     } else {
                   1667:         if ($perlvar{'lonBalancer'} eq 'yes') {
                   1668:             $is_balancer = 1;
                   1669:             $offloadto = &this_host_spares($dom_in_use);
                   1670:         }
1.1307    raeburn  1671:         unless (defined($cached)) {
                   1672:             $domneedscache = $serverhomedom;
                   1673:         }
                   1674:     }
                   1675:     if ($domneedscache) {
                   1676:         &do_cache_new('loadbalancing',$domneedscache,$is_balancer,$cachetime);
1.1129    raeburn  1677:     }
1.1430    raeburn  1678:     if (($is_balancer) && ($caller ne 'switchserver')) {
1.1176    raeburn  1679:         my $lowest_load = 30000;
                   1680:         if (ref($offloadto) eq 'HASH') {
                   1681:             if (ref($offloadto->{'primary'}) eq 'ARRAY') {
                   1682:                 foreach my $try_server (@{$offloadto->{'primary'}}) {
                   1683:                     ($otherserver,$lowest_load) =
                   1684:                         &compare_server_load($try_server,$otherserver,$lowest_load);
                   1685:                 }
1.1129    raeburn  1686:             }
1.1176    raeburn  1687:             my $found_server = ($otherserver ne '' && $lowest_load < 100);
1.1129    raeburn  1688: 
1.1176    raeburn  1689:             if (!$found_server) {
                   1690:                 if (ref($offloadto->{'default'}) eq 'ARRAY') {
                   1691:                     foreach my $try_server (@{$offloadto->{'default'}}) {
                   1692:                         ($otherserver,$lowest_load) =
                   1693:                             &compare_server_load($try_server,$otherserver,$lowest_load);
                   1694:                     }
                   1695:                 }
                   1696:             }
                   1697:         } elsif (ref($offloadto) eq 'ARRAY') {
                   1698:             if (@{$offloadto} == 1) {
                   1699:                 $otherserver = $offloadto->[0];
                   1700:             } elsif (@{$offloadto} > 1) {
                   1701:                 foreach my $try_server (@{$offloadto}) {
1.1129    raeburn  1702:                     ($otherserver,$lowest_load) =
                   1703:                         &compare_server_load($try_server,$otherserver,$lowest_load);
                   1704:                 }
                   1705:             }
                   1706:         }
1.1331    raeburn  1707:         unless ($caller eq 'login') {
                   1708:             if (($otherserver ne '') && (grep(/^\Q$otherserver\E$/,@hosts))) {
                   1709:                 $is_balancer = 0;
                   1710:                 if ($uname ne '' && $udom ne '') {
                   1711:                     if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
1.1389    raeburn  1712:                         &appenv({'user.loadbalexempt'     => $lonhost,
1.1331    raeburn  1713:                                  'user.loadbalcheck.time' => time});
                   1714:                     }
1.1176    raeburn  1715:                 }
1.1129    raeburn  1716:             }
                   1717:         }
1.1430    raeburn  1718:     }
                   1719:     if (($is_balancer) && (!$homeintdom)) {
                   1720:         undef($setcookie);
1.1129    raeburn  1721:     }
1.1391    raeburn  1722:     return ($is_balancer,$otherserver,$setcookie,$offloadto,$dom_balancers);
1.1129    raeburn  1723: }
                   1724: 
1.1191    raeburn  1725: sub check_balancer_result {
                   1726:     my ($result,@hosts) = @_;
1.1391    raeburn  1727:     my ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
1.1191    raeburn  1728:     if (ref($result) eq 'HASH') {
                   1729:         if ($result->{'lonhost'} ne '') {
                   1730:             my $currbalancer = $result->{'lonhost'};
                   1731:             if (grep(/^\Q$currbalancer\E$/,@hosts)) {
                   1732:                 $is_balancer = 1;
                   1733:                 $currtargets = $result->{'targets'};
                   1734:                 $currrules = $result->{'rules'};
                   1735:             }
1.1391    raeburn  1736:             $dom_balancers = $currbalancer;
1.1191    raeburn  1737:         } else {
1.1391    raeburn  1738:             if (keys(%{$result})) {
                   1739:                 foreach my $key (keys(%{$result})) {
                   1740:                     if (($key ne '') && (grep(/^\Q$key\E$/,@hosts)) &&
                   1741:                         (ref($result->{$key}) eq 'HASH')) {
                   1742:                         $is_balancer = 1;
                   1743:                         $currrules = $result->{$key}{'rules'};
                   1744:                         $currtargets = $result->{$key}{'targets'};
                   1745:                         $setcookie = $result->{$key}{'cookie'};
                   1746:                         last;
                   1747:                     }
1.1191    raeburn  1748:                 }
1.1391    raeburn  1749:                 $dom_balancers = join(',',sort(keys(%{$result})));
1.1191    raeburn  1750:             }
                   1751:         }
                   1752:     }
1.1391    raeburn  1753:     return ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
1.1191    raeburn  1754: }
                   1755: 
1.1129    raeburn  1756: sub get_loadbalancer_targets {
                   1757:     my ($rule_in_effect,$currtargets,$uname,$udom) = @_;
                   1758:     my $offloadto;
1.1175    raeburn  1759:     if ($rule_in_effect eq 'none') {
                   1760:         return [$perlvar{'lonHostID'}];
                   1761:     } elsif ($rule_in_effect eq '') {
1.1129    raeburn  1762:         $offloadto = $currtargets;
                   1763:     } else {
                   1764:         if ($rule_in_effect eq 'homeserver') {
                   1765:             my $homeserver = &homeserver($uname,$udom);
                   1766:             if ($homeserver ne 'no_host') {
                   1767:                 $offloadto = [$homeserver];
                   1768:             }
                   1769:         } elsif ($rule_in_effect eq 'externalbalancer') {
                   1770:             my %domconfig =
                   1771:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
                   1772:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
                   1773:                 if ($domconfig{'loadbalancing'}{'lonhost'} ne '') {
                   1774:                     if (&hostname($domconfig{'loadbalancing'}{'lonhost'}) ne '') {
                   1775:                         $offloadto = [$domconfig{'loadbalancing'}{'lonhost'}];
                   1776:                     }
                   1777:                 }
                   1778:             } else {
1.1178    raeburn  1779:                 my %servers = &internet_dom_servers($udom);
1.1129    raeburn  1780:                 my ($remotebalancer,$remotetargets) = &get_lonbalancer_config(\%servers);
                   1781:                 if (&hostname($remotebalancer) ne '') {
                   1782:                     $offloadto = [$remotebalancer];
                   1783:                 }
                   1784:             }
                   1785:         } elsif (&hostname($rule_in_effect) ne '') {
                   1786:             $offloadto = [$rule_in_effect];
                   1787:         }
                   1788:     }
                   1789:     return $offloadto;
                   1790: }
                   1791: 
1.1127    raeburn  1792: sub internet_dom_servers {
                   1793:     my ($dom) = @_;
                   1794:     my (%uniqservers,%servers);
                   1795:     my $primaryserver = &hostname(&domain($dom,'primary'));
                   1796:     my @machinedoms = &machine_domains($primaryserver);
                   1797:     foreach my $mdom (@machinedoms) {
                   1798:         my %currservers = %servers;
                   1799:         my %server = &get_servers($mdom);
                   1800:         %servers = (%currservers,%server);
                   1801:     }
                   1802:     my %by_hostname;
                   1803:     foreach my $id (keys(%servers)) {
                   1804:         push(@{$by_hostname{$servers{$id}}},$id);
                   1805:     }
                   1806:     foreach my $hostname (sort(keys(%by_hostname))) {
                   1807:         if (@{$by_hostname{$hostname}} > 1) {
                   1808:             my $match = 0;
                   1809:             foreach my $id (@{$by_hostname{$hostname}}) {
                   1810:                 if (&host_domain($id) eq $dom) {
                   1811:                     $uniqservers{$id} = $hostname;
                   1812:                     $match = 1;
                   1813:                 }
                   1814:             }
                   1815:             unless ($match) {
                   1816:                 $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
                   1817:             }
                   1818:         } else {
                   1819:             $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
                   1820:         }
                   1821:     }
                   1822:     return %uniqservers;
                   1823: }
                   1824: 
1.1347    raeburn  1825: sub trusted_domains {
                   1826:     my ($cmdtype,$calldom) = @_;
1.1349    raeburn  1827:     my ($trusted,$untrusted);
1.1347    raeburn  1828:     if (&domain($calldom) eq '') {
1.1349    raeburn  1829:         return ($trusted,$untrusted);
1.1347    raeburn  1830:     }
1.1395    raeburn  1831:     unless ($cmdtype =~ /^(content|shared|enroll|coaurem|othcoau|domroles|catalog|reqcrs|msg)$/) {
1.1349    raeburn  1832:         return ($trusted,$untrusted);
1.1347    raeburn  1833:     }
                   1834:     my $callprimary = &domain($calldom,'primary');
                   1835:     my $intcalldom = &Apache::lonnet::internet_dom($callprimary);
                   1836:     if ($intcalldom eq '') {
1.1349    raeburn  1837:         return ($trusted,$untrusted);
1.1347    raeburn  1838:     }
                   1839: 
                   1840:     my ($trustconfig,$cached)=&Apache::lonnet::is_cached_new('trust',$calldom);
                   1841:     unless (defined($cached)) {
                   1842:         my %domconfig = &Apache::lonnet::get_dom('configuration',['trust'],$calldom);
                   1843:         &Apache::lonnet::do_cache_new('trust',$calldom,$domconfig{'trust'},3600);
                   1844:         $trustconfig = $domconfig{'trust'};
                   1845:     }
                   1846:     if (ref($trustconfig)) {
                   1847:         my (%possexc,%possinc,@allexc,@allinc); 
                   1848:         if (ref($trustconfig->{$cmdtype}) eq 'HASH') {
                   1849:             if (ref($trustconfig->{$cmdtype}->{'exc'}) eq 'ARRAY') {
                   1850:                 map { $possexc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'exc'}}; 
                   1851:             }
                   1852:             if (ref($trustconfig->{$cmdtype}->{'inc'}) eq 'ARRAY') {
1.1395    raeburn  1853:                 $possinc{$intcalldom} = 1;
1.1347    raeburn  1854:                 map { $possinc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'inc'}};
                   1855:             }
                   1856:         }
                   1857:         if (keys(%possexc)) {
                   1858:             if (keys(%possinc)) {
                   1859:                 foreach my $key (sort(keys(%possexc))) {
                   1860:                     next if ($key eq $intcalldom);
                   1861:                     unless ($possinc{$key}) {
                   1862:                         push(@allexc,$key);
                   1863:                     }
                   1864:                 }
                   1865:             } else {
                   1866:                 @allexc = sort(keys(%possexc));
                   1867:             }
                   1868:         }
                   1869:         if (keys(%possinc)) {
                   1870:             $possinc{$intcalldom} = 1;
                   1871:             @allinc = sort(keys(%possinc));
                   1872:         }
                   1873:         if ((@allexc > 0) || (@allinc > 0)) {
                   1874:             my %doms_by_intdom;
                   1875:             my %allintdoms = &all_host_intdom();
                   1876:             my %alldoms = &all_host_domain();
                   1877:             foreach my $key (%allintdoms) {
                   1878:                 if (ref($doms_by_intdom{$allintdoms{$key}}) eq 'ARRAY') {
                   1879:                     unless (grep(/^\Q$alldoms{$key}\E$/,@{$doms_by_intdom{$allintdoms{$key}}})) {
                   1880:                         push(@{$doms_by_intdom{$allintdoms{$key}}},$alldoms{$key});
                   1881:                     }
                   1882:                 } else {
                   1883:                     $doms_by_intdom{$allintdoms{$key}} = [$alldoms{$key}]; 
                   1884:                 }
                   1885:             }
                   1886:             foreach my $exc (@allexc) {
                   1887:                 if (ref($doms_by_intdom{$exc}) eq 'ARRAY') {
1.1395    raeburn  1888:                     push(@{$untrusted},@{$doms_by_intdom{$exc}});
1.1347    raeburn  1889:                 }
                   1890:             }
                   1891:             foreach my $inc (@allinc) {
                   1892:                 if (ref($doms_by_intdom{$inc}) eq 'ARRAY') {
1.1395    raeburn  1893:                     push(@{$trusted},@{$doms_by_intdom{$inc}});
1.1347    raeburn  1894:                 }
                   1895:             }
                   1896:         }
                   1897:     }
1.1349    raeburn  1898:     return ($trusted,$untrusted);
1.1347    raeburn  1899: }
                   1900: 
                   1901: sub will_trust {
                   1902:     my ($cmdtype,$domain,$possdom) = @_;
                   1903:     return 1 if ($domain eq $possdom);
                   1904:     my ($trustedref,$untrustedref) = &trusted_domains($cmdtype,$possdom);
                   1905:     my $willtrust; 
                   1906:     if ((ref($trustedref) eq 'ARRAY') && (@{$trustedref} > 0)) {
                   1907:         if (grep(/^\Q$domain\E$/,@{$trustedref})) {
                   1908:             $willtrust = 1;
                   1909:         }
                   1910:     } elsif ((ref($untrustedref) eq 'ARRAY') && (@{$untrustedref} > 0)) {
                   1911:         unless (grep(/^\Q$domain\E$/,@{$untrustedref})) {
                   1912:             $willtrust = 1;
                   1913:         }
                   1914:     } else {
                   1915:         $willtrust = 1;
                   1916:     }
                   1917:     return $willtrust;
                   1918: }
                   1919: 
1.1       albertel 1920: # ---------------------- Find the homebase for a user from domain's lib servers
1.11      www      1921: 
1.599     albertel 1922: my %homecache;
1.1       albertel 1923: sub homeserver {
1.230     stredwic 1924:     my ($uname,$udom,$ignoreBadCache)=@_;
1.1       albertel 1925:     my $index="$uname:$udom";
1.426     albertel 1926: 
1.599     albertel 1927:     if (exists($homecache{$index})) { return $homecache{$index}; }
1.841     albertel 1928: 
                   1929:     my %servers = &get_servers($udom,'library');
                   1930:     foreach my $tryserver (keys(%servers)) {
1.230     stredwic 1931:         next if ($ignoreBadCache ne 'true' && 
1.231     stredwic 1932: 		 exists($badServerCache{$tryserver}));
1.841     albertel 1933: 
                   1934: 	my $answer=reply("home:$udom:$uname",$tryserver);
                   1935: 	if ($answer eq 'found') {
                   1936: 	    delete($badServerCache{$tryserver}); 
                   1937: 	    return $homecache{$index}=$tryserver;
                   1938: 	} elsif ($answer eq 'no_host') {
                   1939: 	    $badServerCache{$tryserver}=1;
                   1940: 	}
1.1       albertel 1941:     }    
                   1942:     return 'no_host';
1.70      www      1943: }
                   1944: 
1.1300    raeburn  1945: # ----- Find the usernames behind a list of student/employee IDs or clicker IDs
1.70      www      1946: 
                   1947: sub idget {
1.1300    raeburn  1948:     my ($udom,$idsref,$namespace)=@_;
1.70      www      1949:     my %returnhash=();
1.1300    raeburn  1950:     my @ids=(); 
                   1951:     if (ref($idsref) eq 'ARRAY') {
                   1952:         @ids = @{$idsref};
                   1953:     } else {
                   1954:         return %returnhash; 
                   1955:     }
                   1956:     if ($namespace eq '') {
                   1957:         $namespace = 'ids';
                   1958:     }
1.70      www      1959:     
1.841     albertel 1960:     my %servers = &get_servers($udom,'library');
                   1961:     foreach my $tryserver (keys(%servers)) {
1.1299    raeburn  1962: 	my $idlist=join('&', map { &escape($_); } @ids);
1.1300    raeburn  1963: 	if ($namespace eq 'ids') {
                   1964: 	    $idlist=~tr/A-Z/a-z/;
                   1965: 	}
                   1966: 	my $reply;
                   1967: 	if ($namespace eq 'ids') {
                   1968: 	    $reply=&reply("idget:$udom:".$idlist,$tryserver);
                   1969: 	} else {
                   1970: 	    $reply=&reply("getdom:$udom:$namespace:$idlist",$tryserver);
                   1971: 	}
1.841     albertel 1972: 	my @answer=();
                   1973: 	if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
                   1974: 	    @answer=split(/\&/,$reply);
                   1975: 	}                    ;
                   1976: 	my $i;
                   1977: 	for ($i=0;$i<=$#ids;$i++) {
                   1978: 	    if ($answer[$i]) {
1.1299    raeburn  1979: 		$returnhash{$ids[$i]}=&unescape($answer[$i]);
1.1300    raeburn  1980: 	    }
1.841     albertel 1981: 	}
1.1300    raeburn  1982:     }
1.70      www      1983:     return %returnhash;
                   1984: }
                   1985: 
                   1986: # ------------------------------------- Find the IDs behind a list of usernames
                   1987: 
                   1988: sub idrget {
                   1989:     my ($udom,@unames)=@_;
                   1990:     my %returnhash=();
1.800     albertel 1991:     foreach my $uname (@unames) {
                   1992:         $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
1.191     harris41 1993:     }
1.70      www      1994:     return %returnhash;
                   1995: }
                   1996: 
1.1300    raeburn  1997: # Store away a list of names and associated student/employee IDs or clicker IDs
1.70      www      1998: 
                   1999: sub idput {
1.1300    raeburn  2000:     my ($udom,$idsref,$uhom,$namespace)=@_;
1.70      www      2001:     my %servers=();
1.1300    raeburn  2002:     my %ids=();
                   2003:     my %byid = ();
                   2004:     if (ref($idsref) eq 'HASH') {
                   2005:         %ids=%{$idsref};
                   2006:     }
                   2007:     if ($namespace eq '') {
                   2008:         $namespace = 'ids'; 
                   2009:     }
1.800     albertel 2010:     foreach my $uname (keys(%ids)) {
                   2011: 	&cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
1.1300    raeburn  2012:         if ($uhom eq '') {
                   2013:             $uhom=&homeserver($uname,$udom);
                   2014:         }
1.70      www      2015:         if ($uhom ne 'no_host') {
1.800     albertel 2016:             my $esc_unam=&escape($uname);
1.1300    raeburn  2017:             if ($namespace eq 'ids') {
                   2018:                 my $id=&escape($ids{$uname});
                   2019:                 $id=~tr/A-Z/a-z/;
                   2020:                 my $esc_unam=&escape($uname);
                   2021:                 $servers{$uhom}.=$id.'='.$esc_unam.'&';
1.70      www      2022:             } else {
1.1300    raeburn  2023:                 my @currids = split(/,/,$ids{$uname});
                   2024:                 foreach my $id (@currids) {
                   2025:                     $byid{$uhom}{$id} .= $uname.',';
                   2026:                 }
                   2027:             }
                   2028:         }
                   2029:     }
                   2030:     if ($namespace eq 'clickers') {
                   2031:         foreach my $server (keys(%byid)) {
                   2032:             if (ref($byid{$server}) eq 'HASH') {
                   2033:                 foreach my $id (keys(%{$byid{$server}})) {
                   2034:                     $byid{$server} =~ s/,$//;
                   2035:                     $servers{$uhom}.=&escape($id).'='.&escape($byid{$server}).'&'; 
                   2036:                 }
1.70      www      2037:             }
                   2038:         }
1.191     harris41 2039:     }
1.800     albertel 2040:     foreach my $server (keys(%servers)) {
1.1300    raeburn  2041:         $servers{$server} =~ s/\&$//;
                   2042:         if ($namespace eq 'ids') {     
                   2043:             &critical('idput:'.$udom.':'.$servers{$server},$server);
                   2044:         } else {
                   2045:             &critical('updateclickers:'.$udom.':add:'.$servers{$server},$server);
                   2046:         }
1.191     harris41 2047:     }
1.344     www      2048: }
                   2049: 
1.1300    raeburn  2050: # ------------- Delete unwanted student/employee IDs or clicker IDs from domain
1.1231    raeburn  2051: 
                   2052: sub iddel {
1.1300    raeburn  2053:     my ($udom,$idshashref,$uhome,$namespace)=@_;
1.1231    raeburn  2054:     my %result=();
1.1300    raeburn  2055:     my %ids=();
                   2056:     my %byid = ();
                   2057:     if (ref($idshashref) eq 'HASH') {
                   2058:         %ids=%{$idshashref};
                   2059:     } else {
1.1231    raeburn  2060:         return %result;
                   2061:     }
1.1300    raeburn  2062:     if ($namespace eq '') {
                   2063:         $namespace = 'ids';
                   2064:     }
1.1231    raeburn  2065:     my %servers=();
1.1300    raeburn  2066:     while (my ($id,$unamestr) = each(%ids)) {
                   2067:         if ($namespace eq 'ids') {
                   2068:             my $uhom = $uhome;
                   2069:             if ($uhom eq '') { 
                   2070:                 $uhom=&homeserver($unamestr,$udom);
                   2071:             }
                   2072:             if ($uhom ne 'no_host') {
                   2073:                 $servers{$uhom}.='&'.&escape($id);
                   2074:             }
                   2075:          } else {
                   2076:             my @curritems = split(/,/,$ids{$id});
                   2077:             foreach my $uname (@curritems) {
                   2078:                 my $uhom = $uhome;
                   2079:                 if ($uhom eq '') {
                   2080:                     $uhom=&homeserver($uname,$udom);
                   2081:                 }
                   2082:                 if ($uhom ne 'no_host') { 
                   2083:                     $byid{$uhom}{$id} .= $uname.',';
                   2084:                 }
                   2085:             }
1.1231    raeburn  2086:         }
1.1300    raeburn  2087:     }
                   2088:     if ($namespace eq 'clickers') {
                   2089:         foreach my $server (keys(%byid)) {
                   2090:             if (ref($byid{$server}) eq 'HASH') {
                   2091:                 foreach my $id (keys(%{$byid{$server}})) {
                   2092:                     $byid{$server}{$id} =~ s/,$//;
                   2093:                     $servers{$server}.=&escape($id).'='.&escape($byid{$server}{$id}).'&';
                   2094:                 }
1.1231    raeburn  2095:             }
                   2096:         }
                   2097:     }
                   2098:     foreach my $server (keys(%servers)) {
1.1300    raeburn  2099:         $servers{$server} =~ s/\&$//;
                   2100:         if ($namespace eq 'ids') {
                   2101:             $result{$server} = &critical('iddel:'.$udom.':'.$servers{$server},$uhome);
                   2102:         } elsif ($namespace eq 'clickers') {
                   2103:             $result{$server} = &critical('updateclickers:'.$udom.':del:'.$servers{$server},$server);
                   2104:         }
1.1231    raeburn  2105:     }
                   2106:     return %result;
                   2107: }
                   2108: 
1.1300    raeburn  2109: # ----- Update clicker ID-to-username look-ups in clickers.db on library server 
                   2110: 
                   2111: sub updateclickers {
                   2112:     my ($udom,$action,$idshashref,$uhome,$critical) = @_;
                   2113:     my %clickers;
                   2114:     if (ref($idshashref) eq 'HASH') {
                   2115:         %clickers=%{$idshashref};
                   2116:     } else {
                   2117:         return;
                   2118:     }
                   2119:     my $items='';
                   2120:     foreach my $item (keys(%clickers)) {
                   2121:         $items.=&escape($item).'='.&escape($clickers{$item}).'&';
                   2122:     }
                   2123:     $items=~s/\&$//;
                   2124:     my $request = "updateclickers:$udom:$action:$items";
                   2125:     if ($critical) {
                   2126:         return &critical($request,$uhome);
                   2127:     } else {
                   2128:         return &reply($request,$uhome);
                   2129:     }
                   2130: }
                   2131: 
1.1023    raeburn  2132: # ------------------------------dump from db file owned by domainconfig user
1.1012    raeburn  2133: sub dump_dom {
1.1165    droeschl 2134:     my ($namespace, $udom, $regexp) = @_;
                   2135: 
                   2136:     $udom ||= $env{'user.domain'};
                   2137: 
                   2138:     return () unless $udom;
                   2139: 
                   2140:     return &dump($namespace, $udom, &get_domainconfiguser($udom), $regexp);
1.1012    raeburn  2141: }
                   2142: 
1.1023    raeburn  2143: # ------------------------------------------ get items from domain db files   
1.806     raeburn  2144: 
                   2145: sub get_dom {
1.860     raeburn  2146:     my ($namespace,$storearr,$udom,$uhome)=@_;
1.1267    raeburn  2147:     return if ($udom eq 'public');
1.806     raeburn  2148:     my $items='';
                   2149:     foreach my $item (@$storearr) {
                   2150:         $items.=&escape($item).'&';
                   2151:     }
                   2152:     $items=~s/\&$//;
1.860     raeburn  2153:     if (!$udom) {
                   2154:         $udom=$env{'user.domain'};
1.1267    raeburn  2155:         return if ($udom eq 'public');
1.860     raeburn  2156:         if (defined(&domain($udom,'primary'))) {
                   2157:             $uhome=&domain($udom,'primary');
                   2158:         } else {
1.874     albertel 2159:             undef($uhome);
1.860     raeburn  2160:         }
                   2161:     } else {
                   2162:         if (!$uhome) {
                   2163:             if (defined(&domain($udom,'primary'))) {
                   2164:                 $uhome=&domain($udom,'primary');
                   2165:             }
                   2166:         }
                   2167:     }
                   2168:     if ($udom && $uhome && ($uhome ne 'no_host')) {
1.1344    raeburn  2169:         my $rep;
1.1442    raeburn  2170:         if (grep { $_ eq $uhome } &current_machine_ids()) {
                   2171:             # domain information is hosted on this machine
                   2172:             my $cmd = 'getdom';
                   2173:             if ($namespace =~ /^enc/) {
                   2174:                 $cmd = 'egetdom';
                   2175:             }
                   2176:             $rep = &LONCAPA::Lond::get_dom("$cmd:$udom:$namespace:$items");
1.1344    raeburn  2177:         } else {
1.1442    raeburn  2178:             if ($namespace =~ /^enc/) {
                   2179:                 $rep=&reply("encrypt:egetdom:$udom:$namespace:$items",$uhome);
                   2180:             } else {
                   2181:                 $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
                   2182:             }
1.1344    raeburn  2183:         }
1.866     raeburn  2184:         my %returnhash;
1.875     albertel 2185:         if ($rep eq '' || $rep =~ /^error: 2 /) {
1.866     raeburn  2186:             return %returnhash;
                   2187:         }
1.806     raeburn  2188:         my @pairs=split(/\&/,$rep);
                   2189:         if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                   2190:             return @pairs;
                   2191:         }
                   2192:         my $i=0;
                   2193:         foreach my $item (@$storearr) {
                   2194:             $returnhash{$item}=&thaw_unescape($pairs[$i]);
                   2195:             $i++;
                   2196:         }
                   2197:         return %returnhash;
                   2198:     } else {
1.880     banghart 2199:         &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
1.806     raeburn  2200:     }
                   2201: }
                   2202: 
                   2203: # -------------------------------------------- put items in domain db files 
                   2204: 
                   2205: sub put_dom {
1.860     raeburn  2206:     my ($namespace,$storehash,$udom,$uhome)=@_;
                   2207:     if (!$udom) {
                   2208:         $udom=$env{'user.domain'};
                   2209:         if (defined(&domain($udom,'primary'))) {
                   2210:             $uhome=&domain($udom,'primary');
                   2211:         } else {
1.874     albertel 2212:             undef($uhome);
1.860     raeburn  2213:         }
                   2214:     } else {
                   2215:         if (!$uhome) {
                   2216:             if (defined(&domain($udom,'primary'))) {
                   2217:                 $uhome=&domain($udom,'primary');
                   2218:             }
                   2219:         }
                   2220:     } 
                   2221:     if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806     raeburn  2222:         my $items='';
                   2223:         foreach my $item (keys(%$storehash)) {
                   2224:             $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
                   2225:         }
                   2226:         $items=~s/\&$//;
1.1344    raeburn  2227:         if ($namespace =~ /^enc/) {
                   2228:             return &reply("encrypt:putdom:$udom:$namespace:$items",$uhome);
                   2229:         } else {
                   2230:             return &reply("putdom:$udom:$namespace:$items",$uhome);
                   2231:         }
1.806     raeburn  2232:     } else {
1.860     raeburn  2233:         &logthis("put_dom failed - no homeserver and/or domain");
1.806     raeburn  2234:     }
                   2235: }
                   2236: 
1.1023    raeburn  2237: # --------------------- newput for items in db file owned by domainconfig user
1.1012    raeburn  2238: sub newput_dom {
1.1023    raeburn  2239:     my ($namespace,$storehash,$udom) = @_;
1.1012    raeburn  2240:     my $result;
                   2241:     if (!$udom) {
                   2242:         $udom=$env{'user.domain'};
                   2243:     }
1.1023    raeburn  2244:     if ($udom) {
                   2245:         my $uname = &get_domainconfiguser($udom);
                   2246:         $result = &newput($namespace,$storehash,$udom,$uname);
1.1012    raeburn  2247:     }
                   2248:     return $result;
                   2249: }
                   2250: 
1.1023    raeburn  2251: # --------------------- delete for items in db file owned by domainconfig user
1.1012    raeburn  2252: sub del_dom {
1.1023    raeburn  2253:     my ($namespace,$storearr,$udom)=@_;
1.1012    raeburn  2254:     if (ref($storearr) eq 'ARRAY') {
                   2255:         if (!$udom) {
                   2256:             $udom=$env{'user.domain'};
                   2257:         }
1.1023    raeburn  2258:         if ($udom) {
                   2259:             my $uname = &get_domainconfiguser($udom); 
                   2260:             return &del($namespace,$storearr,$udom,$uname);
1.1012    raeburn  2261:         }
                   2262:     }
                   2263: }
                   2264: 
1.1023    raeburn  2265: # ----------------------------------construct domainconfig user for a domain 
                   2266: sub get_domainconfiguser {
                   2267:     my ($udom) = @_;
                   2268:     return $udom.'-domainconfig';
                   2269: }
                   2270: 
1.837     raeburn  2271: sub retrieve_inst_usertypes {
                   2272:     my ($udom) = @_;
                   2273:     my (%returnhash,@order);
1.989     raeburn  2274:     my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
                   2275:     if ((ref($domdefs{'inststatustypes'}) eq 'HASH') && 
                   2276:         (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
1.1256    raeburn  2277:         return ($domdefs{'inststatustypes'},$domdefs{'inststatusorder'});
1.989     raeburn  2278:     } else {
                   2279:         if (defined(&domain($udom,'primary'))) {
                   2280:             my $uhome=&domain($udom,'primary');
                   2281:             my $rep=&reply("inst_usertypes:$udom",$uhome);
                   2282:             if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
1.1256    raeburn  2283:                 &logthis("retrieve_inst_usertypes failed - $rep returned from $uhome in domain: $udom");
1.989     raeburn  2284:                 return (\%returnhash,\@order);
                   2285:             }
                   2286:             my ($hashitems,$orderitems) = split(/:/,$rep); 
                   2287:             my @pairs=split(/\&/,$hashitems);
                   2288:             foreach my $item (@pairs) {
                   2289:                 my ($key,$value)=split(/=/,$item,2);
                   2290:                 $key = &unescape($key);
                   2291:                 next if ($key =~ /^error: 2 /);
                   2292:                 $returnhash{$key}=&thaw_unescape($value);
                   2293:             }
                   2294:             my @esc_order = split(/\&/,$orderitems);
                   2295:             foreach my $item (@esc_order) {
                   2296:                 push(@order,&unescape($item));
                   2297:             }
                   2298:         } else {
1.1256    raeburn  2299:             &logthis("retrieve_inst_usertypes failed - no primary domain server for $udom");
1.837     raeburn  2300:         }
1.1256    raeburn  2301:         return (\%returnhash,\@order);
1.837     raeburn  2302:     }
                   2303: }
                   2304: 
1.868     raeburn  2305: sub is_domainimage {
                   2306:     my ($url) = @_;
1.1306    raeburn  2307:     if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo)/+[^/]-) {
1.868     raeburn  2308:         if (&domain($1) ne '') {
                   2309:             return '1';
                   2310:         }
                   2311:     }
                   2312:     return;
                   2313: }
                   2314: 
1.899     raeburn  2315: sub inst_directory_query {
                   2316:     my ($srch) = @_;
                   2317:     my $udom = $srch->{'srchdomain'};
                   2318:     my %results;
                   2319:     my $homeserver = &domain($udom,'primary');
1.909     raeburn  2320:     my $outcome;
1.899     raeburn  2321:     if ($homeserver ne '') {
1.1357    raeburn  2322:         unless ($homeserver eq $perlvar{'lonHostID'}) {
                   2323:             if ($srch->{'srchby'} eq 'email') {
1.1417    raeburn  2324:                 my $lcrev = &get_server_loncaparev($udom,$homeserver);
1.1357    raeburn  2325:                 my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
                   2326:                 if (($major eq '' && $minor eq '') || ($major < 2) ||
                   2327:                     (($major == 2) && ($minor < 12))) {
                   2328:                     return;
                   2329:                 }
                   2330:             }
                   2331:         }
1.904     albertel 2332: 	my $queryid=&reply("querysend:instdirsearch:".
                   2333: 			   &escape($srch->{'srchby'}).':'.
                   2334: 			   &escape($srch->{'srchterm'}).':'.
                   2335: 			   &escape($srch->{'srchtype'}),$homeserver);
                   2336: 	my $host=&hostname($homeserver);
                   2337: 	if ($queryid !~/^\Q$host\E\_/) {
1.1343    raeburn  2338: 	    &logthis('institutional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.' in domain '.$udom);
1.904     albertel 2339: 	    return;
                   2340: 	}
                   2341: 	my $response = &get_query_reply($queryid);
                   2342: 	my $maxtries = 5;
                   2343: 	my $tries = 1;
                   2344: 	while (($response=~/^timeout/) && ($tries < $maxtries)) {
                   2345: 	    $response = &get_query_reply($queryid);
                   2346: 	    $tries ++;
                   2347: 	}
                   2348: 
                   2349:         if (!&error($response) && $response ne 'refused') {
1.909     raeburn  2350:             if ($response eq 'unavailable') {
                   2351:                 $outcome = $response;
                   2352:             } else {
                   2353:                 $outcome = 'ok';
                   2354:                 my @matches = split(/\n/,$response);
                   2355:                 foreach my $match (@matches) {
                   2356:                     my ($key,$value) = split(/=/,$match);
                   2357:                     $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
                   2358:                 }
1.899     raeburn  2359:             }
                   2360:         }
                   2361:     }
1.909     raeburn  2362:     return ($outcome,%results);
1.899     raeburn  2363: }
                   2364: 
                   2365: sub usersearch {
                   2366:     my ($srch) = @_;
                   2367:     my $dom = $srch->{'srchdomain'};
                   2368:     my %results;
                   2369:     my %libserv = &all_library();
                   2370:     my $query = 'usersearch';
                   2371:     foreach my $tryserver (keys(%libserv)) {
                   2372:         if (&host_domain($tryserver) eq $dom) {
1.1357    raeburn  2373:             unless ($tryserver eq $perlvar{'lonHostID'}) {
                   2374:                 if ($srch->{'srchby'} eq 'email') {
1.1417    raeburn  2375:                     my $lcrev = &get_server_loncaparev($dom,$tryserver);
1.1357    raeburn  2376:                     my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
                   2377:                     next if (($major eq '' && $minor eq '') || ($major < 2) ||
                   2378:                              (($major == 2) && ($minor < 12)));
                   2379:                 }
                   2380:             }
1.899     raeburn  2381:             my $host=&hostname($tryserver);
                   2382:             my $queryid=
1.911     raeburn  2383:                 &reply("querysend:".&escape($query).':'.
                   2384:                        &escape($srch->{'srchby'}).':'.
1.899     raeburn  2385:                        &escape($srch->{'srchtype'}).':'.
                   2386:                        &escape($srch->{'srchterm'}),$tryserver);
                   2387:             if ($queryid !~/^\Q$host\E\_/) {
                   2388:                 &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
1.902     raeburn  2389:                 next;
1.899     raeburn  2390:             }
                   2391:             my $reply = &get_query_reply($queryid);
                   2392:             my $maxtries = 1;
                   2393:             my $tries = 1;
                   2394:             while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   2395:                 $reply = &get_query_reply($queryid);
                   2396:                 $tries ++;
                   2397:             }
                   2398:             if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   2399:                 &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') -  maxtries: '.$maxtries.' tries: '.$tries);
                   2400:             } else {
1.911     raeburn  2401:                 my @matches;
                   2402:                 if ($reply =~ /\n/) {
                   2403:                     @matches = split(/\n/,$reply);
                   2404:                 } else {
                   2405:                     @matches = split(/\&/,$reply);
                   2406:                 }
1.899     raeburn  2407:                 foreach my $match (@matches) {
                   2408:                     my ($uname,$udom,%userhash);
1.911     raeburn  2409:                     foreach my $entry (split(/:/,$match)) {
                   2410:                         my ($key,$value) =
                   2411:                             map {&unescape($_);} split(/=/,$entry);
1.899     raeburn  2412:                         $userhash{$key} = $value;
                   2413:                         if ($key eq 'username') {
                   2414:                             $uname = $value;
                   2415:                         } elsif ($key eq 'domain') {
                   2416:                             $udom = $value;
1.911     raeburn  2417:                         }
1.899     raeburn  2418:                     }
                   2419:                     $results{$uname.':'.$udom} = \%userhash;
                   2420:                 }
                   2421:             }
                   2422:         }
                   2423:     }
                   2424:     return %results;
                   2425: }
                   2426: 
1.912     raeburn  2427: sub get_instuser {
                   2428:     my ($udom,$uname,$id) = @_;
                   2429:     my $homeserver = &domain($udom,'primary');
                   2430:     my ($outcome,%results);
                   2431:     if ($homeserver ne '') {
                   2432:         my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
                   2433:                            &escape($id).':'.&escape($udom),$homeserver);
                   2434:         my $host=&hostname($homeserver);
                   2435:         if ($queryid !~/^\Q$host\E\_/) {
                   2436:             &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
                   2437:             return;
                   2438:         }
                   2439:         my $response = &get_query_reply($queryid);
                   2440:         my $maxtries = 5;
                   2441:         my $tries = 1;
                   2442:         while (($response=~/^timeout/) && ($tries < $maxtries)) {
                   2443:             $response = &get_query_reply($queryid);
                   2444:             $tries ++;
                   2445:         }
                   2446:         if (!&error($response) && $response ne 'refused') {
                   2447:             if ($response eq 'unavailable') {
                   2448:                 $outcome = $response;
                   2449:             } else {
                   2450:                 $outcome = 'ok';
                   2451:                 my @matches = split(/\n/,$response);
                   2452:                 foreach my $match (@matches) {
                   2453:                     my ($key,$value) = split(/=/,$match);
                   2454:                     $results{&unescape($key)} = &thaw_unescape($value);
                   2455:                 }
                   2456:             }
                   2457:         }
                   2458:     }
                   2459:     my %userinfo;
                   2460:     if (ref($results{$uname}) eq 'HASH') {
                   2461:         %userinfo = %{$results{$uname}};
                   2462:     } 
                   2463:     return ($outcome,%userinfo);
                   2464: }
                   2465: 
1.1290    raeburn  2466: sub get_multiple_instusers {
                   2467:     my ($udom,$users,$caller) = @_;
                   2468:     my ($outcome,$results);
                   2469:     if (ref($users) eq 'HASH') {
                   2470:         my $count = keys(%{$users}); 
                   2471:         my $requested = &freeze_escape($users);
                   2472:         my $homeserver = &domain($udom,'primary');
                   2473:         if ($homeserver ne '') {
                   2474:             my $queryid=&reply('querysend:getmultinstusers:::'.$caller.'='.$requested,$homeserver);
                   2475:             my $host=&hostname($homeserver);
                   2476:             if ($queryid !~/^\Q$host\E\_/) {
                   2477:                 &logthis('get_multiple_instusers invalid queryid: '.$queryid.
                   2478:                          ' for host: '.$homeserver.'in domain '.$udom);
                   2479:                 return ($outcome,$results);
                   2480:             }
                   2481:             my $response = &get_query_reply($queryid);
                   2482:             my $maxtries = 5;
                   2483:             if ($count > 100) {
                   2484:                 $maxtries = 1+int($count/20);
                   2485:             }
                   2486:             my $tries = 1;
                   2487:             while (($response=~/^timeout/) && ($tries <= $maxtries)) {
                   2488:                 $response = &get_query_reply($queryid);
                   2489:                 $tries ++;
                   2490:             }
                   2491:             if ($response eq '') {
                   2492:                 $results = {};
                   2493:                 foreach my $key (keys(%{$users})) {
                   2494:                     my ($uname,$id);
                   2495:                     if ($caller eq 'id') {
                   2496:                         $id = $key;
                   2497:                     } else {
                   2498:                         $uname = $key;
                   2499:                     }
                   2500:                     my ($resp,%info) = &get_instuser($udom,$uname,$id);
1.1291    raeburn  2501:                     $outcome = $resp;
1.1290    raeburn  2502:                     if ($resp eq 'ok') {
                   2503:                         %{$results} = (%{$results}, %info);
                   2504:                     } else {
                   2505:                         last;
                   2506:                     }
                   2507:                 }
                   2508:             } elsif(!&error($response) && ($response ne 'refused')) {
                   2509:                 if (($response eq 'unavailable') || ($response eq 'invalid') || ($response eq 'timeout')) {
                   2510:                     $outcome = $response;
                   2511:                 } else {
1.1291    raeburn  2512:                     ($outcome,my $userdata) = split(/=/,$response,2);
1.1290    raeburn  2513:                     if ($outcome eq 'ok') {
                   2514:                         $results = &thaw_unescape($userdata); 
                   2515:                     }
                   2516:                 }
                   2517:             }
                   2518:         }
                   2519:     }
                   2520:     return ($outcome,$results);
                   2521: }
                   2522: 
1.912     raeburn  2523: sub inst_rulecheck {
1.923     raeburn  2524:     my ($udom,$uname,$id,$item,$rules) = @_;
1.912     raeburn  2525:     my %returnhash;
                   2526:     if ($udom ne '') {
                   2527:         if (ref($rules) eq 'ARRAY') {
                   2528:             @{$rules} = map {&escape($_);} (@{$rules});
                   2529:             my $rulestr = join(':',@{$rules});
                   2530:             my $homeserver=&domain($udom,'primary');
                   2531:             if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923     raeburn  2532:                 my $response;
                   2533:                 if ($item eq 'username') {                
                   2534:                     $response=&unescape(&reply('instrulecheck:'.&escape($udom).
                   2535:                                               ':'.&escape($uname).':'.$rulestr,
1.912     raeburn  2536:                                               $homeserver));
1.923     raeburn  2537:                 } elsif ($item eq 'id') {
                   2538:                     $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
                   2539:                                               ':'.&escape($id).':'.$rulestr,
                   2540:                                               $homeserver));
1.945     raeburn  2541:                 } elsif ($item eq 'selfcreate') {
                   2542:                     $response=&unescape(&reply('instselfcreatecheck:'.
1.943     raeburn  2543:                                                &escape($udom).':'.&escape($uname).
                   2544:                                               ':'.$rulestr,$homeserver));
1.923     raeburn  2545:                 }
1.912     raeburn  2546:                 if ($response ne 'refused') {
                   2547:                     my @pairs=split(/\&/,$response);
                   2548:                     foreach my $item (@pairs) {
                   2549:                         my ($key,$value)=split(/=/,$item,2);
                   2550:                         $key = &unescape($key);
                   2551:                         next if ($key =~ /^error: 2 /);
                   2552:                         $returnhash{$key}=&thaw_unescape($value);
                   2553:                     }
                   2554:                 }
                   2555:             }
                   2556:         }
                   2557:     }
                   2558:     return %returnhash;
                   2559: }
                   2560: 
                   2561: sub inst_userrules {
1.923     raeburn  2562:     my ($udom,$check) = @_;
1.912     raeburn  2563:     my (%ruleshash,@ruleorder);
                   2564:     if ($udom ne '') {
                   2565:         my $homeserver=&domain($udom,'primary');
                   2566:         if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923     raeburn  2567:             my $response;
                   2568:             if ($check eq 'id') {
                   2569:                 $response=&reply('instidrules:'.&escape($udom),
1.912     raeburn  2570:                                  $homeserver);
1.943     raeburn  2571:             } elsif ($check eq 'email') {
                   2572:                 $response=&reply('instemailrules:'.&escape($udom),
                   2573:                                  $homeserver);
1.923     raeburn  2574:             } else {
                   2575:                 $response=&reply('instuserrules:'.&escape($udom),
                   2576:                                  $homeserver);
                   2577:             }
1.912     raeburn  2578:             if (($response ne 'refused') && ($response ne 'error') && 
1.923     raeburn  2579:                 ($response ne 'unknown_cmd') && 
1.912     raeburn  2580:                 ($response ne 'no_such_host')) {
                   2581:                 my ($hashitems,$orderitems) = split(/:/,$response);
                   2582:                 my @pairs=split(/\&/,$hashitems);
                   2583:                 foreach my $item (@pairs) {
                   2584:                     my ($key,$value)=split(/=/,$item,2);
                   2585:                     $key = &unescape($key);
                   2586:                     next if ($key =~ /^error: 2 /);
                   2587:                     $ruleshash{$key}=&thaw_unescape($value);
                   2588:                 }
                   2589:                 my @esc_order = split(/\&/,$orderitems);
                   2590:                 foreach my $item (@esc_order) {
                   2591:                     push(@ruleorder,&unescape($item));
                   2592:                 }
                   2593:             }
                   2594:         }
                   2595:     }
                   2596:     return (\%ruleshash,\@ruleorder);
                   2597: }
                   2598: 
1.976     raeburn  2599: # ------------- Get Authentication, Language and User Tools Defaults for Domain
1.943     raeburn  2600: 
                   2601: sub get_domain_defaults {
1.1240    raeburn  2602:     my ($domain,$ignore_cache) = @_;
1.1242    raeburn  2603:     return if (($domain eq '') || ($domain eq 'public'));
1.943     raeburn  2604:     my $cachetime = 60*60*24;
1.1240    raeburn  2605:     unless ($ignore_cache) {
                   2606:         my ($result,$cached)=&is_cached_new('domdefaults',$domain);
                   2607:         if (defined($cached)) {
                   2608:             if (ref($result) eq 'HASH') {
                   2609:                 return %{$result};
                   2610:             }
1.943     raeburn  2611:         }
                   2612:     }
                   2613:     my %domdefaults;
                   2614:     my %domconfig =
1.989     raeburn  2615:          &Apache::lonnet::get_dom('configuration',['defaults','quotas',
1.1047    raeburn  2616:                                   'requestcourses','inststatus',
1.1183    raeburn  2617:                                   'coursedefaults','usersessions',
1.1258    raeburn  2618:                                   'requestauthor','selfenrollment',
1.1320    raeburn  2619:                                   'coursecategories','ssl','autoenroll',
1.1434    raeburn  2620:                                   'trust','helpsettings','wafproxy'],$domain);
1.1305    raeburn  2621:     my @coursetypes = ('official','unofficial','community','textbook','placement');
1.943     raeburn  2622:     if (ref($domconfig{'defaults'}) eq 'HASH') {
                   2623:         $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'}; 
                   2624:         $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
                   2625:         $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
1.982     raeburn  2626:         $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
1.985     raeburn  2627:         $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
1.1147    raeburn  2628:         $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
1.1340    raeburn  2629:         $domdefaults{'intauth_cost'} = $domconfig{'defaults'}{'intauth_cost'};
                   2630:         $domdefaults{'intauth_switch'} = $domconfig{'defaults'}{'intauth_switch'};
                   2631:         $domdefaults{'intauth_check'} = $domconfig{'defaults'}{'intauth_check'};
1.943     raeburn  2632:     } else {
                   2633:         $domdefaults{'lang_def'} = &domain($domain,'lang_def');
                   2634:         $domdefaults{'auth_def'} = &domain($domain,'auth_def');
                   2635:         $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
                   2636:     }
1.976     raeburn  2637:     if (ref($domconfig{'quotas'}) eq 'HASH') {
                   2638:         if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
                   2639:             $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
                   2640:         } else {
                   2641:             $domdefaults{'defaultquota'} = $domconfig{'quotas'};
1.1229    raeburn  2642:         }
1.1177    raeburn  2643:         my @usertools = ('aboutme','blog','webdav','portfolio');
1.976     raeburn  2644:         foreach my $item (@usertools) {
                   2645:             if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
                   2646:                 $domdefaults{$item} = $domconfig{'quotas'}{$item};
                   2647:             }
                   2648:         }
1.1229    raeburn  2649:         if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
                   2650:             $domdefaults{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
                   2651:         }
1.976     raeburn  2652:     }
1.985     raeburn  2653:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
1.1305    raeburn  2654:         foreach my $item ('official','unofficial','community','textbook','placement') {
1.985     raeburn  2655:             $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
                   2656:         }
                   2657:     }
1.1183    raeburn  2658:     if (ref($domconfig{'requestauthor'}) eq 'HASH') {
                   2659:         $domdefaults{'requestauthor'} = $domconfig{'requestauthor'};
                   2660:     }
1.989     raeburn  2661:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
1.1256    raeburn  2662:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
1.989     raeburn  2663:             $domdefaults{$item} = $domconfig{'inststatus'}{$item};
                   2664:         }
                   2665:     }
1.1047    raeburn  2666:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
1.1230    raeburn  2667:         $domdefaults{'canuse_pdfforms'} = $domconfig{'coursedefaults'}{'canuse_pdfforms'};
1.1277    raeburn  2668:         $domdefaults{'usejsme'} = $domconfig{'coursedefaults'}{'usejsme'};
                   2669:         $domdefaults{'uselcmath'} = $domconfig{'coursedefaults'}{'uselcmath'};
                   2670:         if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
                   2671:             $domdefaults{'postsubmit'} = $domconfig{'coursedefaults'}{'postsubmit'}{'client'};
                   2672:         }
1.1254    raeburn  2673:         foreach my $type (@coursetypes) {
                   2674:             if (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
                   2675:                 unless ($type eq 'community') {
                   2676:                     $domdefaults{$type.'credits'} = $domconfig{'coursedefaults'}{'coursecredits'}{$type};
                   2677:                 }
                   2678:             }
                   2679:             if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
                   2680:                 $domdefaults{$type.'quota'} = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
                   2681:             }
1.1277    raeburn  2682:             if ($domdefaults{'postsubmit'} eq 'on') {
                   2683:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
                   2684:                     $domdefaults{$type.'postsubtimeout'} = 
                   2685:                         $domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}{$type}; 
                   2686:                 }
                   2687:             }
1.1230    raeburn  2688:         }
1.1286    raeburn  2689:         if (ref($domconfig{'coursedefaults'}{'canclone'}) eq 'HASH') {
                   2690:             if (ref($domconfig{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
                   2691:                 my @clonecodes = @{$domconfig{'coursedefaults'}{'canclone'}{'instcode'}};
                   2692:                 if (@clonecodes) {
                   2693:                     $domdefaults{'canclone'} = join('+',@clonecodes);
                   2694:                 }
                   2695:             }
                   2696:         } elsif ($domconfig{'coursedefaults'}{'canclone'}) {
                   2697:             $domdefaults{'canclone'}=$domconfig{'coursedefaults'}{'canclone'};
                   2698:         }
1.1356    raeburn  2699:         if ($domconfig{'coursedefaults'}{'texengine'}) {
                   2700:             $domdefaults{'texengine'} = $domconfig{'coursedefaults'}{'texengine'};
                   2701:         } 
1.1047    raeburn  2702:     }
1.1073    raeburn  2703:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
                   2704:         if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
                   2705:             $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
                   2706:         }
                   2707:         if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
                   2708:             $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
                   2709:         }
1.1279    raeburn  2710:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
                   2711:             $domdefaults{'offloadnow'} = $domconfig{'usersessions'}{'offloadnow'};
                   2712:         }
1.1440    raeburn  2713:         if (ref($domconfig{'usersessions'}{'offloadoth'}) eq 'HASH') {
1.1439    raeburn  2714:             $domdefaults{'offloadoth'} = $domconfig{'usersessions'}{'offloadoth'};
                   2715:         }
1.1073    raeburn  2716:     }
1.1254    raeburn  2717:     if (ref($domconfig{'selfenrollment'}) eq 'HASH') {
                   2718:         if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
                   2719:             my @settings = ('types','registered','enroll_dates','access_dates','section',
                   2720:                             'approval','limit');
                   2721:             foreach my $type (@coursetypes) {
                   2722:                 if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
                   2723:                     my @mgrdc = ();
                   2724:                     foreach my $item (@settings) {
                   2725:                         if ($domconfig{'selfenrollment'}{'admin'}{$type}{$item} eq '0') {
                   2726:                             push(@mgrdc,$item);
                   2727:                         }
                   2728:                     }
                   2729:                     if (@mgrdc) {
                   2730:                         $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
                   2731:                     }
                   2732:                 }
                   2733:             }
                   2734:         }
                   2735:         if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
                   2736:             foreach my $type (@coursetypes) {
                   2737:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
                   2738:                     foreach my $item (keys(%{$domconfig{'selfenrollment'}{'default'}{$type}})) {
                   2739:                         $domdefaults{$type.'selfenroll'.$item} = $domconfig{'selfenrollment'}{'default'}{$type}{$item};
                   2740:                     }
                   2741:                 }
                   2742:             }
                   2743:         }
                   2744:     }
1.1258    raeburn  2745:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
                   2746:         $domdefaults{'catauth'} = 'std';
                   2747:         $domdefaults{'catunauth'} = 'std';
1.1413    raeburn  2748:         if ($domconfig{'coursecategories'}{'auth'}) {
1.1258    raeburn  2749:             $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
                   2750:         }
                   2751:         if ($domconfig{'coursecategories'}{'unauth'}) {
                   2752:             $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
                   2753:         }
                   2754:     }
1.1315    raeburn  2755:     if (ref($domconfig{'ssl'}) eq 'HASH') {
                   2756:         if (ref($domconfig{'ssl'}{'replication'}) eq 'HASH') {
                   2757:             $domdefaults{'replication'} = $domconfig{'ssl'}{'replication'};
                   2758:         }
1.1338    raeburn  2759:         if (ref($domconfig{'ssl'}{'connto'}) eq 'HASH') {
                   2760:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connto'};
                   2761:         }
                   2762:         if (ref($domconfig{'ssl'}{'connfrom'}) eq 'HASH') {
                   2763:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connfrom'};
1.1315    raeburn  2764:         }
                   2765:     }
1.1320    raeburn  2766:     if (ref($domconfig{'trust'}) eq 'HASH') {
                   2767:         my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
                   2768:         foreach my $prefix (@prefixes) {
                   2769:             if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
                   2770:                 $domdefaults{'trust'.$prefix} = $domconfig{'trust'}{$prefix};
                   2771:             }
                   2772:         }
                   2773:     }
1.1314    raeburn  2774:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
                   2775:         $domdefaults{'autofailsafe'} = $domconfig{'autoenroll'}{'autofailsafe'};
                   2776:     }
1.1332    raeburn  2777:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
                   2778:         $domdefaults{'submitbugs'} = $domconfig{'helpsettings'}{'submitbugs'};
                   2779:         if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
                   2780:             $domdefaults{'adhocroles'} = $domconfig{'helpsettings'}{'adhoc'};
                   2781:         }
                   2782:     }
1.1434    raeburn  2783:     if (ref($domconfig{'wafproxy'}) eq 'HASH') {
1.1437    raeburn  2784:         foreach my $item ('ipheader','trusted','vpnint','vpnext') {
1.1434    raeburn  2785:             if ($domconfig{'wafproxy'}{$item}) {
                   2786:                 $domdefaults{'waf_'.$item} = $domconfig{'wafproxy'}{$item};
                   2787:             }
                   2788:         }
                   2789:     } 
1.1219    raeburn  2790:     &do_cache_new('domdefaults',$domain,\%domdefaults,$cachetime);
1.943     raeburn  2791:     return %domdefaults;
                   2792: }
                   2793: 
1.1412    raeburn  2794: sub get_dom_cats {
                   2795:     my ($dom) = @_;
                   2796:     return unless (&domain($dom));
                   2797:     my ($cats,$cached)=&is_cached_new('cats',$dom);
                   2798:     unless (defined($cached)) {
                   2799:         my %domconfig = &get_dom('configuration',['coursecategories'],$dom);
                   2800:         if (ref($domconfig{'coursecategories'}) eq 'HASH') {
                   2801:             if (ref($domconfig{'coursecategories'}{'cats'}) eq 'HASH') {
                   2802:                 %{$cats} = %{$domconfig{'coursecategories'}{'cats'}};
                   2803:             } else {
                   2804:                 $cats = {};
                   2805:             }
                   2806:         } else {
                   2807:             $cats = {};
                   2808:         }
                   2809:         &Apache::lonnet::do_cache_new('cats',$dom,$cats,3600);
                   2810:     }
1.1413    raeburn  2811:     return $cats;
                   2812: }
                   2813: 
                   2814: sub get_dom_instcats {
                   2815:     my ($dom) = @_;
                   2816:     return unless (&domain($dom));
                   2817:     my ($instcats,$cached)=&is_cached_new('instcats',$dom);
                   2818:     unless (defined($cached)) {
                   2819:         my (%coursecodes,%codes,@codetitles,%cat_titles,%cat_order);
                   2820:         my $totcodes = &retrieve_instcodes(\%coursecodes,$dom);
                   2821:         if ($totcodes > 0) {
                   2822:             my $caller = 'global';
                   2823:             if (&auto_instcode_format($caller,$dom,\%coursecodes,\%codes,
                   2824:                                       \@codetitles,\%cat_titles,\%cat_order) eq 'ok') {
                   2825:                 $instcats = {
                   2826:                                 codes => \%codes,
                   2827:                                 codetitles => \@codetitles,
                   2828:                                 cat_titles => \%cat_titles,
                   2829:                                 cat_order => \%cat_order,
                   2830:                             };
                   2831:                 &do_cache_new('instcats',$dom,$instcats,3600);
                   2832:             }
                   2833:         }
                   2834:     }
                   2835:     return $instcats;
                   2836: }
                   2837: 
                   2838: sub retrieve_instcodes {
                   2839:     my ($coursecodes,$dom) = @_;
                   2840:     my $totcodes;
                   2841:     my %courses = &courseiddump($dom,'.',1,'.','.','.',undef,undef,'Course');
                   2842:     foreach my $course (keys(%courses)) {
                   2843:         if (ref($courses{$course}) eq 'HASH') {
                   2844:             if ($courses{$course}{'inst_code'} ne '') {
                   2845:                 $$coursecodes{$course} = $courses{$course}{'inst_code'};
                   2846:                 $totcodes ++;
                   2847:             }
                   2848:         }
                   2849:     }
                   2850:     return $totcodes;
1.1412    raeburn  2851: }
                   2852: 
1.1311    raeburn  2853: sub course_portal_url {
                   2854:     my ($cnum,$cdom) = @_;
                   2855:     my $chome = &homeserver($cnum,$cdom);
                   2856:     my $hostname = &hostname($chome);
                   2857:     my $protocol = $protocol{$chome};
                   2858:     $protocol = 'http' if ($protocol ne 'https');
                   2859:     my %domdefaults = &get_domain_defaults($cdom);
                   2860:     my $firsturl;
                   2861:     if ($domdefaults{'portal_def'}) {
                   2862:         $firsturl = $domdefaults{'portal_def'};
                   2863:     } else {
                   2864:         $firsturl = $protocol.'://'.$hostname;
                   2865:     }
                   2866:     return $firsturl;
                   2867: }
                   2868: 
1.1407    raeburn  2869: # --------------------------------------------- Get domain config for passwords
                   2870: 
                   2871: sub get_passwdconf {
                   2872:     my ($dom) = @_;
                   2873:     my (%passwdconf,$gotconf,$lookup);
                   2874:     my ($result,$cached)=&is_cached_new('passwdconf',$dom);
                   2875:     if (defined($cached)) {
                   2876:         if (ref($result) eq 'HASH') {
                   2877:             %passwdconf = %{$result};
                   2878:             $gotconf = 1;
                   2879:         }
                   2880:     }
                   2881:     unless ($gotconf) {
                   2882:         my %domconfig = &get_dom('configuration',['passwords'],$dom);
                   2883:         if (ref($domconfig{'passwords'}) eq 'HASH') {
                   2884:             %passwdconf = %{$domconfig{'passwords'}};
                   2885:         }
                   2886:         my $cachetime = 24*60*60;
                   2887:         &do_cache_new('passwdconf',$dom,\%passwdconf,$cachetime);
                   2888:     }
                   2889:     return %passwdconf;
                   2890: }
                   2891: 
1.344     www      2892: # --------------------------------------------------- Assign a key to a student
                   2893: 
                   2894: sub assign_access_key {
1.364     www      2895: #
                   2896: # a valid key looks like uname:udom#comments
                   2897: # comments are being appended
                   2898: #
1.498     www      2899:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
                   2900:     $kdom=
1.620     albertel 2901:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
1.498     www      2902:     $knum=
1.620     albertel 2903:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
1.344     www      2904:     $cdom=
1.620     albertel 2905:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      2906:     $cnum=
1.620     albertel 2907:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
                   2908:     $udom=$env{'user.name'} unless (defined($udom));
                   2909:     $uname=$env{'user.domain'} unless (defined($uname));
1.498     www      2910:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
1.364     www      2911:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
1.479     albertel 2912:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
1.364     www      2913:                                                   # assigned to this person
                   2914:                                                   # - this should not happen,
1.345     www      2915:                                                   # unless something went wrong
                   2916:                                                   # the first time around
                   2917: # ready to assign
1.364     www      2918:         $logentry=$1.'; '.$logentry;
1.496     www      2919:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
1.498     www      2920:                                                  $kdom,$knum) eq 'ok') {
1.345     www      2921: # key now belongs to user
1.346     www      2922: 	    my $envkey='key.'.$cdom.'_'.$cnum;
1.345     www      2923:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
1.949     raeburn  2924:                 &appenv({'environment.'.$envkey => $ckey});
1.345     www      2925:                 return 'ok';
                   2926:             } else {
                   2927:                 return 
                   2928:   'error: Count not permanently assign key, will need to be re-entered later.';
                   2929: 	    }
                   2930:         } else {
                   2931:             return 'error: Could not assign key, try again later.';
                   2932:         }
1.364     www      2933:     } elsif (!$existing{$ckey}) {
1.345     www      2934: # the key does not exist
                   2935: 	return 'error: The key does not exist';
                   2936:     } else {
                   2937: # the key is somebody else's
                   2938: 	return 'error: The key is already in use';
                   2939:     }
1.344     www      2940: }
                   2941: 
1.364     www      2942: # ------------------------------------------ put an additional comment on a key
                   2943: 
                   2944: sub comment_access_key {
                   2945: #
                   2946: # a valid key looks like uname:udom#comments
                   2947: # comments are being appended
                   2948: #
                   2949:     my ($ckey,$cdom,$cnum,$logentry)=@_;
                   2950:     $cdom=
1.620     albertel 2951:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.364     www      2952:     $cnum=
1.620     albertel 2953:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.364     www      2954:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
                   2955:     if ($existing{$ckey}) {
                   2956:         $existing{$ckey}.='; '.$logentry;
                   2957: # ready to assign
1.367     www      2958:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
1.364     www      2959:                                                  $cdom,$cnum) eq 'ok') {
                   2960: 	    return 'ok';
                   2961:         } else {
                   2962: 	    return 'error: Count not store comment.';
                   2963:         }
                   2964:     } else {
                   2965: # the key does not exist
                   2966: 	return 'error: The key does not exist';
                   2967:     }
                   2968: }
                   2969: 
1.344     www      2970: # ------------------------------------------------------ Generate a set of keys
                   2971: 
                   2972: sub generate_access_keys {
1.364     www      2973:     my ($number,$cdom,$cnum,$logentry)=@_;
1.344     www      2974:     $cdom=
1.620     albertel 2975:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      2976:     $cnum=
1.620     albertel 2977:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.361     www      2978:     unless (&allowed('mky',$cdom)) { return 0; }
1.344     www      2979:     unless (($cdom) && ($cnum)) { return 0; }
                   2980:     if ($number>10000) { return 0; }
                   2981:     sleep(2); # make sure don't get same seed twice
                   2982:     srand(time()^($$+($$<<15))); # from "Programming Perl"
                   2983:     my $total=0;
                   2984:     for (my $i=1;$i<=$number;$i++) {
                   2985:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
                   2986:                   sprintf("%lx",int(100000*rand)).'-'.
                   2987:                   sprintf("%lx",int(100000*rand));
                   2988:        $newkey=~s/1/g/g; # folks mix up 1 and l
                   2989:        $newkey=~s/0/h/g; # and also 0 and O
                   2990:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
                   2991:        if ($existing{$newkey}) {
                   2992:            $i--;
                   2993:        } else {
1.364     www      2994: 	  if (&put('accesskeys',
                   2995:               { $newkey => '# generated '.localtime().
1.620     albertel 2996:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
1.364     www      2997:                            '; '.$logentry },
                   2998: 		   $cdom,$cnum) eq 'ok') {
1.344     www      2999:               $total++;
                   3000: 	  }
                   3001:        }
                   3002:     }
1.620     albertel 3003:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.344     www      3004:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
                   3005:     return $total;
                   3006: }
                   3007: 
                   3008: # ------------------------------------------------------- Validate an accesskey
                   3009: 
                   3010: sub validate_access_key {
                   3011:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
                   3012:     $cdom=
1.620     albertel 3013:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      3014:     $cnum=
1.620     albertel 3015:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
                   3016:     $udom=$env{'user.domain'} unless (defined($udom));
                   3017:     $uname=$env{'user.name'} unless (defined($uname));
1.345     www      3018:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1.479     albertel 3019:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
1.70      www      3020: }
                   3021: 
                   3022: # ------------------------------------- Find the section of student in a course
1.652     albertel 3023: sub devalidate_getsection_cache {
                   3024:     my ($udom,$unam,$courseid)=@_;
                   3025:     my $hashid="$udom:$unam:$courseid";
                   3026:     &devalidate_cache_new('getsection',$hashid);
                   3027: }
1.298     matthew  3028: 
1.815     albertel 3029: sub courseid_to_courseurl {
                   3030:     my ($courseid) = @_;
                   3031:     #already url style courseid
                   3032:     return $courseid if ($courseid =~ m{^/});
                   3033: 
                   3034:     if (exists($env{'course.'.$courseid.'.num'})) {
                   3035: 	my $cnum = $env{'course.'.$courseid.'.num'};
                   3036: 	my $cdom = $env{'course.'.$courseid.'.domain'};
                   3037: 	return "/$cdom/$cnum";
                   3038:     }
                   3039: 
                   3040:     my %courseinfo=&Apache::lonnet::coursedescription($courseid);
                   3041:     if (exists($courseinfo{'num'})) {
                   3042: 	return "/$courseinfo{'domain'}/$courseinfo{'num'}";
                   3043:     }
                   3044: 
                   3045:     return undef;
                   3046: }
                   3047: 
1.298     matthew  3048: sub getsection {
                   3049:     my ($udom,$unam,$courseid)=@_;
1.599     albertel 3050:     my $cachetime=1800;
1.551     albertel 3051: 
                   3052:     my $hashid="$udom:$unam:$courseid";
1.599     albertel 3053:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
1.551     albertel 3054:     if (defined($cached)) { return $result; }
                   3055: 
1.298     matthew  3056:     my %Pending; 
                   3057:     my %Expired;
                   3058:     #
                   3059:     # Each role can either have not started yet (pending), be active, 
                   3060:     #    or have expired.
                   3061:     #
                   3062:     # If there is an active role, we are done.
                   3063:     #
                   3064:     # If there is more than one role which has not started yet, 
                   3065:     #     choose the one which will start sooner
                   3066:     # If there is one role which has not started yet, return it.
                   3067:     #
                   3068:     # If there is more than one expired role, choose the one which ended last.
                   3069:     # If there is a role which has expired, return it.
                   3070:     #
1.815     albertel 3071:     $courseid = &courseid_to_courseurl($courseid);
1.1166    raeburn  3072:     my %roleshash = &dump('roles',$udom,$unam,$courseid);
1.817     raeburn  3073:     foreach my $key (keys(%roleshash)) {
1.479     albertel 3074:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1.298     matthew  3075:         my $section=$1;
                   3076:         if ($key eq $courseid.'_st') { $section=''; }
1.817     raeburn  3077:         my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
1.298     matthew  3078:         my $now=time;
1.548     albertel 3079:         if (defined($end) && $end && ($now > $end)) {
1.298     matthew  3080:             $Expired{$end}=$section;
                   3081:             next;
                   3082:         }
1.548     albertel 3083:         if (defined($start) && $start && ($now < $start)) {
1.298     matthew  3084:             $Pending{$start}=$section;
                   3085:             next;
                   3086:         }
1.599     albertel 3087:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
1.298     matthew  3088:     }
                   3089:     #
                   3090:     # Presumedly there will be few matching roles from the above
                   3091:     # loop and the sorting time will be negligible.
                   3092:     if (scalar(keys(%Pending))) {
                   3093:         my ($time) = sort {$a <=> $b} keys(%Pending);
1.599     albertel 3094:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
1.298     matthew  3095:     } 
                   3096:     if (scalar(keys(%Expired))) {
                   3097:         my @sorted = sort {$a <=> $b} keys(%Expired);
                   3098:         my $time = pop(@sorted);
1.599     albertel 3099:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
1.298     matthew  3100:     }
1.599     albertel 3101:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
1.298     matthew  3102: }
1.70      www      3103: 
1.599     albertel 3104: sub save_cache {
                   3105:     &purge_remembered();
1.722     albertel 3106:     #&Apache::loncommon::validate_page();
1.620     albertel 3107:     undef(%env);
1.780     albertel 3108:     undef($env_loaded);
1.599     albertel 3109: }
1.452     albertel 3110: 
1.599     albertel 3111: my $to_remember=-1;
                   3112: my %remembered;
                   3113: my %accessed;
                   3114: my $kicks=0;
                   3115: my $hits=0;
1.849     albertel 3116: sub make_key {
                   3117:     my ($name,$id) = @_;
1.872     albertel 3118:     if (length($id) > 65 
                   3119: 	&& length(&escape($id)) > 200) {
                   3120: 	$id=length($id).':'.&Digest::MD5::md5_hex($id);
                   3121:     }
1.849     albertel 3122:     return &escape($name.':'.$id);
                   3123: }
                   3124: 
1.599     albertel 3125: sub devalidate_cache_new {
                   3126:     my ($name,$id,$debug) = @_;
                   3127:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
1.1319    damieng  3128:     my $remembered_id=$name.':'.$id;
1.849     albertel 3129:     $id=&make_key($name,$id);
1.599     albertel 3130:     $memcache->delete($id);
1.1319    damieng  3131:     delete($remembered{$remembered_id});
                   3132:     delete($accessed{$remembered_id});
1.599     albertel 3133: }
                   3134: 
                   3135: sub is_cached_new {
                   3136:     my ($name,$id,$debug) = @_;
1.1319    damieng  3137:     my $remembered_id=$name.':'.$id; # this is to avoid make_key (which is slow) whenever possible
                   3138:     if (exists($remembered{$remembered_id})) {
                   3139: 	if ($debug) { &Apache::lonnet::logthis("Early return $remembered_id of $remembered{$remembered_id} "); }
                   3140: 	$accessed{$remembered_id}=[&gettimeofday()];
1.599     albertel 3141: 	$hits++;
1.1319    damieng  3142: 	return ($remembered{$remembered_id},1);
1.599     albertel 3143:     }
1.1319    damieng  3144:     $id=&make_key($name,$id);
1.599     albertel 3145:     my $value = $memcache->get($id);
                   3146:     if (!(defined($value))) {
                   3147: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
1.417     albertel 3148: 	return (undef,undef);
1.416     albertel 3149:     }
1.599     albertel 3150:     if ($value eq '__undef__') {
                   3151: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
                   3152: 	$value=undef;
                   3153:     }
1.1319    damieng  3154:     &make_room($remembered_id,$value,$debug);
1.599     albertel 3155:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
                   3156:     return ($value,1);
                   3157: }
                   3158: 
                   3159: sub do_cache_new {
                   3160:     my ($name,$id,$value,$time,$debug) = @_;
1.1319    damieng  3161:     my $remembered_id=$name.':'.$id;
1.849     albertel 3162:     $id=&make_key($name,$id);
1.599     albertel 3163:     my $setvalue=$value;
                   3164:     if (!defined($setvalue)) {
                   3165: 	$setvalue='__undef__';
                   3166:     }
1.623     albertel 3167:     if (!defined($time) ) {
                   3168: 	$time=600;
                   3169:     }
1.599     albertel 3170:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
1.910     albertel 3171:     my $result = $memcache->set($id,$setvalue,$time);
                   3172:     if (! $result) {
1.872     albertel 3173: 	&logthis("caching of id -> $id  failed");
1.910     albertel 3174: 	$memcache->disconnect_all();
1.872     albertel 3175:     }
1.600     albertel 3176:     # need to make a copy of $value
1.1319    damieng  3177:     &make_room($remembered_id,$value,$debug);
1.599     albertel 3178:     return $value;
                   3179: }
                   3180: 
                   3181: sub make_room {
1.1319    damieng  3182:     my ($remembered_id,$value,$debug)=@_;
1.919     albertel 3183: 
1.1319    damieng  3184:     $remembered{$remembered_id}= (ref($value)) ? &Storable::dclone($value)
1.919     albertel 3185:                                     : $value;
1.599     albertel 3186:     if ($to_remember<0) { return; }
1.1319    damieng  3187:     $accessed{$remembered_id}=[&gettimeofday()];
1.599     albertel 3188:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
                   3189:     my $to_kick;
                   3190:     my $max_time=0;
                   3191:     foreach my $other (keys(%accessed)) {
                   3192: 	if (&tv_interval($accessed{$other}) > $max_time) {
                   3193: 	    $to_kick=$other;
                   3194: 	    $max_time=&tv_interval($accessed{$other});
                   3195: 	}
                   3196:     }
                   3197:     delete($remembered{$to_kick});
                   3198:     delete($accessed{$to_kick});
                   3199:     $kicks++;
                   3200:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
1.541     albertel 3201:     return;
                   3202: }
                   3203: 
1.599     albertel 3204: sub purge_remembered {
1.604     albertel 3205:     #&logthis("Tossing ".scalar(keys(%remembered)));
                   3206:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
1.599     albertel 3207:     undef(%remembered);
                   3208:     undef(%accessed);
1.428     albertel 3209: }
1.70      www      3210: # ------------------------------------- Read an entry from a user's environment
                   3211: 
                   3212: sub userenvironment {
                   3213:     my ($udom,$unam,@what)=@_;
1.976     raeburn  3214:     my $items;
                   3215:     foreach my $item (@what) {
                   3216:         $items.=&escape($item).'&';
                   3217:     }
                   3218:     $items=~s/\&$//;
1.70      www      3219:     my %returnhash=();
1.1009    raeburn  3220:     my $uhome = &homeserver($unam,$udom);
                   3221:     unless ($uhome eq 'no_host') {
                   3222:         my @answer=split(/\&/, 
                   3223:             &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
1.1048    raeburn  3224:         if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
                   3225:             return %returnhash;
                   3226:         }
1.1009    raeburn  3227:         my $i;
                   3228:         for ($i=0;$i<=$#what;$i++) {
                   3229: 	    $returnhash{$what[$i]}=&unescape($answer[$i]);
                   3230:         }
1.70      www      3231:     }
                   3232:     return %returnhash;
1.1       albertel 3233: }
                   3234: 
1.617     albertel 3235: # ---------------------------------------------------------- Get a studentphoto
                   3236: sub studentphoto {
                   3237:     my ($udom,$unam,$ext) = @_;
                   3238:     my $home=&Apache::lonnet::homeserver($unam,$udom);
1.706     raeburn  3239:     if (defined($env{'request.course.id'})) {
1.708     raeburn  3240:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.706     raeburn  3241:             if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
                   3242:                 return(&retrievestudentphoto($udom,$unam,$ext)); 
                   3243:             } else {
                   3244:                 my ($result,$perm_reqd)=
1.707     albertel 3245: 		    &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706     raeburn  3246:                 if ($result eq 'ok') {
                   3247:                     if (!($perm_reqd eq 'yes')) {
                   3248:                         return(&retrievestudentphoto($udom,$unam,$ext));
                   3249:                     }
                   3250:                 }
                   3251:             }
                   3252:         }
                   3253:     } else {
                   3254:         my ($result,$perm_reqd) = 
1.707     albertel 3255: 	    &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706     raeburn  3256:         if ($result eq 'ok') {
                   3257:             if (!($perm_reqd eq 'yes')) {
                   3258:                 return(&retrievestudentphoto($udom,$unam,$ext));
                   3259:             }
                   3260:         }
                   3261:     }
                   3262:     return '/adm/lonKaputt/lonlogo_broken.gif';
                   3263: }
                   3264: 
                   3265: sub retrievestudentphoto {
                   3266:     my ($udom,$unam,$ext,$type) = @_;
                   3267:     my $home=&Apache::lonnet::homeserver($unam,$udom);
                   3268:     my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
                   3269:     if ($ret eq 'ok') {
                   3270:         my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
                   3271:         if ($type eq 'thumbnail') {
                   3272:             $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext"; 
                   3273:         }
                   3274:         my $tokenurl=&Apache::lonnet::tokenwrapper($url);
                   3275:         return $tokenurl;
                   3276:     } else {
                   3277:         if ($type eq 'thumbnail') {
                   3278:             return '/adm/lonKaputt/genericstudent_tn.gif';
                   3279:         } else { 
                   3280:             return '/adm/lonKaputt/lonlogo_broken.gif';
                   3281:         }
1.617     albertel 3282:     }
                   3283: }
                   3284: 
1.263     www      3285: # -------------------------------------------------------------------- New chat
                   3286: 
                   3287: sub chatsend {
1.724     raeburn  3288:     my ($newentry,$anon,$group)=@_;
1.620     albertel 3289:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
                   3290:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   3291:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.263     www      3292:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
1.620     albertel 3293: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
1.724     raeburn  3294: 		   &escape($newentry)).':'.$group,$chome);
1.292     www      3295: }
                   3296: 
                   3297: # ------------------------------------------ Find current version of a resource
                   3298: 
                   3299: sub getversion {
                   3300:     my $fname=&clutter(shift);
1.1189    raeburn  3301:     unless ($fname=~m{^(/adm/wrapper|)/res/}) { return -1; }
1.292     www      3302:     return &currentversion(&filelocation('',$fname));
                   3303: }
                   3304: 
                   3305: sub currentversion {
                   3306:     my $fname=shift;
                   3307:     my $author=$fname;
                   3308:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   3309:     my ($udom,$uname)=split(/\//,$author);
1.1112    www      3310:     my $home=&homeserver($uname,$udom);
1.292     www      3311:     if ($home eq 'no_host') { 
                   3312:         return -1; 
                   3313:     }
1.1112    www      3314:     my $answer=&reply("currentversion:$fname",$home);
1.292     www      3315:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   3316: 	return -1;
                   3317:     }
1.1112    www      3318:     return $answer;
1.263     www      3319: }
                   3320: 
1.1111    www      3321: #
                   3322: # Return special version number of resource if set by override, empty otherwise
                   3323: #
                   3324: sub usedversion {
                   3325:     my $fname=shift;
                   3326:     unless ($fname) { $fname=$env{'request.uri'}; }
                   3327:     my ($urlversion)=($fname=~/\.(\d+)\.\w+$/);
                   3328:     if ($urlversion) { return $urlversion; }
                   3329:     return '';
                   3330: }
                   3331: 
1.1       albertel 3332: # ----------------------------- Subscribe to a resource, return URL if possible
1.11      www      3333: 
1.1       albertel 3334: sub subscribe {
                   3335:     my $fname=shift;
1.761     raeburn  3336:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1.532     albertel 3337:     $fname=~s/[\n\r]//g;
1.1       albertel 3338:     my $author=$fname;
                   3339:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   3340:     my ($udom,$uname)=split(/\//,$author);
                   3341:     my $home=homeserver($uname,$udom);
1.335     albertel 3342:     if ($home eq 'no_host') {
                   3343:         return 'not_found';
1.1       albertel 3344:     }
                   3345:     my $answer=reply("sub:$fname",$home);
1.64      www      3346:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   3347: 	$answer.=' by '.$home;
                   3348:     }
1.1       albertel 3349:     return $answer;
                   3350: }
                   3351:     
1.8       www      3352: # -------------------------------------------------------------- Replicate file
                   3353: 
                   3354: sub repcopy {
                   3355:     my $filename=shift;
1.23      www      3356:     $filename=~s/\/+/\//g;
1.1142    raeburn  3357:     my $londocroot = $perlvar{'lonDocRoot'};
                   3358:     if ($filename=~m{^\Q$londocroot/adm/\E}) { return 'ok'; }
1.1164    raeburn  3359:     if ($filename=~m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
1.1142    raeburn  3360:     if ($filename=~m{^\Q$londocroot/userfiles/\E} or
                   3361: 	$filename=~m{^/*(uploaded|editupload)/}) {
1.538     albertel 3362: 	return &repcopy_userfile($filename);
                   3363:     }
1.532     albertel 3364:     $filename=~s/[\n\r]//g;
1.8       www      3365:     my $transname="$filename.in.transfer";
1.828     www      3366: # FIXME: this should flock
1.607     raeburn  3367:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
1.8       www      3368:     my $remoteurl=subscribe($filename);
1.64      www      3369:     if ($remoteurl =~ /^con_lost by/) {
                   3370: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.607     raeburn  3371:            return 'unavailable';
1.8       www      3372:     } elsif ($remoteurl eq 'not_found') {
1.441     albertel 3373: 	   #&logthis("Subscribe returned not_found: $filename");
1.607     raeburn  3374: 	   return 'not_found';
1.64      www      3375:     } elsif ($remoteurl =~ /^rejected by/) {
                   3376: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.607     raeburn  3377:            return 'forbidden';
1.20      www      3378:     } elsif ($remoteurl eq 'directory') {
1.607     raeburn  3379:            return 'ok';
1.8       www      3380:     } else {
1.290     www      3381:         my $author=$filename;
                   3382:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   3383:         my ($udom,$uname)=split(/\//,$author);
                   3384:         my $home=homeserver($uname,$udom);
                   3385:         unless ($home eq $perlvar{'lonHostID'}) {
1.8       www      3386:            my @parts=split(/\//,$filename);
                   3387:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
1.1142    raeburn  3388:            if ($path ne "$londocroot/res") {
1.8       www      3389:                &logthis("Malconfiguration for replication: $filename");
1.607     raeburn  3390: 	       return 'bad_request';
1.8       www      3391:            }
                   3392:            my $count;
                   3393:            for ($count=5;$count<$#parts;$count++) {
                   3394:                $path.="/$parts[$count]";
                   3395:                if ((-e $path)!=1) {
                   3396: 		   mkdir($path,0777);
                   3397:                }
                   3398:            }
                   3399:            my $request=new HTTP::Request('GET',"$remoteurl");
1.1345    raeburn  3400:            my $response;
                   3401:            if ($remoteurl =~ m{/raw/}) {
                   3402:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',0,1);
                   3403:            } else {
                   3404:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',1);
                   3405:            }
1.8       www      3406:            if ($response->is_error()) {
                   3407: 	       unlink($transname);
                   3408:                my $message=$response->status_line;
1.672     albertel 3409:                &logthis("<font color=\"blue\">WARNING:"
1.12      www      3410:                        ." LWP get: $message: $filename</font>");
1.607     raeburn  3411:                return 'unavailable';
1.8       www      3412:            } else {
1.16      www      3413: 	       if ($remoteurl!~/\.meta$/) {
                   3414:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
1.1345    raeburn  3415:                   my $mresponse;
                   3416:                   if ($remoteurl =~ m{/raw/}) {
                   3417:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',0,1);
                   3418:                   } else {
                   3419:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',1);
                   3420:                   }
1.16      www      3421:                   if ($mresponse->is_error()) {
                   3422: 		      unlink($filename.'.meta');
                   3423:                       &logthis(
1.672     albertel 3424:                      "<font color=\"yellow\">INFO: No metadata: $filename</font>");
1.16      www      3425:                   }
                   3426: 	       }
1.8       www      3427:                rename($transname,$filename);
1.607     raeburn  3428:                return 'ok';
1.8       www      3429:            }
1.290     www      3430:        }
1.8       www      3431:     }
1.330     www      3432: }
                   3433: 
1.1422    raeburn  3434: # ------------------------------------------------- Unsubscribe from a resource
                   3435: 
                   3436: sub unsubscribe {
                   3437:     my ($fname) = @_;
                   3438:     my $answer;
                   3439:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return $answer; }
                   3440:     $fname=~s/[\n\r]//g;
                   3441:     my $author=$fname;
                   3442:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   3443:     my ($udom,$uname)=split(/\//,$author);
                   3444:     my $home=homeserver($uname,$udom);
                   3445:     if ($home eq 'no_host') {
                   3446:         $answer = 'no_host';
                   3447:     } elsif (grep { $_ eq $home } &current_machine_ids()) {
                   3448:         $answer = 'home';
                   3449:     } else {
                   3450:         my $defdom = $perlvar{'lonDefDomain'};
                   3451:         if (&will_trust('content',$defdom,$udom)) {
                   3452:             $answer = reply("unsub:$fname",$home);
                   3453:         } else {
                   3454:             $answer = 'untrusted';
                   3455:         }
                   3456:     }
                   3457:     return $answer;
                   3458: }
                   3459: 
1.330     www      3460: # ------------------------------------------------ Get server side include body
                   3461: sub ssi_body {
1.381     albertel 3462:     my ($filelink,%form)=@_;
1.606     matthew  3463:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
                   3464:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
                   3465:     }
1.953     www      3466:     my $output='';
                   3467:     my $response;
1.980     raeburn  3468:     if ($filelink=~/^https?\:/) {
1.954     raeburn  3469:        ($output,$response)=&externalssi($filelink);
1.953     www      3470:     } else {
1.1004    droeschl 3471:        $filelink .= $filelink=~/\?/ ? '&' : '?';
                   3472:        $filelink .= 'inhibitmenu=yes';
1.953     www      3473:        ($output,$response)=&ssi($filelink,%form);
                   3474:     }
1.778     albertel 3475:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
1.451     albertel 3476:     $output=~s/^.*?\<body[^\>]*\>//si;
1.930     albertel 3477:     $output=~s/\<\/body\s*\>.*?$//si;
1.953     www      3478:     if (wantarray) {
                   3479:         return ($output, $response);
                   3480:     } else {
                   3481:         return $output;
                   3482:     }
1.8       www      3483: }
                   3484: 
1.15      www      3485: # --------------------------------------------------------- Server Side Include
                   3486: 
1.782     albertel 3487: sub absolute_url {
1.1447    raeburn  3488:     my ($host_name,$unalias,$keep_proto) = @_;
1.782     albertel 3489:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
                   3490:     if ($host_name eq '') {
                   3491: 	$host_name = $ENV{'SERVER_NAME'};
                   3492:     }
1.1447    raeburn  3493:     if ($unalias) {
                   3494:         my $alias = &get_proxy_alias();
                   3495:         if ($alias eq $host_name) {
                   3496:             my $lonhost = $perlvar{'lonHostID'};
                   3497:             my $hostname = &hostname($lonhost);
                   3498:             my $lcproto; 
                   3499:             if (($keep_proto) || ($hostname eq '')) {
                   3500:                 $lcproto = $protocol;
                   3501:             } else {
                   3502:                 $lcproto = $protocol{$lonhost};
                   3503:                 $lcproto = 'http' if ($lcproto ne 'https');
                   3504:                 $lcproto .= '://';
                   3505:             }
                   3506:             unless ($hostname eq '') {
                   3507:                 return $lcproto.$hostname;
                   3508:             }
                   3509:         }
                   3510:     }
1.782     albertel 3511:     return $protocol.$host_name;
                   3512: }
                   3513: 
1.942     foxr     3514: #
                   3515: #   Server side include.
                   3516: # Parameters:
                   3517: #  fn     Possibly encrypted resource name/id.
                   3518: #  form   Hash that describes how the rendering should be done
                   3519: #         and other things.
1.944     foxr     3520: # Returns:
1.950     raeburn  3521: #   Scalar context: The content of the response.
                   3522: #   Array context:  2 element list of the content and the full response object.
1.942     foxr     3523: #     
1.15      www      3524: sub ssi {
                   3525: 
1.944     foxr     3526:     my ($fn,%form)=@_;
1.1447    raeburn  3527:     my ($host,$request,$response);
                   3528:     $host = &absolute_url('',1);
1.711     albertel 3529: 
                   3530:     $form{'no_update_last_known'}=1;
1.895     albertel 3531:     &Apache::lonenc::check_encrypt(\$fn);
1.23      www      3532:     if (%form) {
1.1447    raeburn  3533:       $request=new HTTP::Request('POST',$host.$fn);
1.1272    droeschl 3534:       $request->content(join('&',map { 
                   3535:             my $name = escape($_);
                   3536:             "$name=" . ( ref($form{$_}) eq 'ARRAY' 
                   3537:             ? join("&$name=", map {escape($_) } @{$form{$_}}) 
                   3538:             : &escape($form{$_}) );    
                   3539:         } keys(%form)));
1.23      www      3540:     } else {
1.1447    raeburn  3541:       $request=new HTTP::Request('GET',$host.$fn);
1.23      www      3542:     }
                   3543: 
1.15      www      3544:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
1.1345    raeburn  3545:     my $lonhost = $perlvar{'lonHostID'};
1.1385    raeburn  3546:     my $islocal;
                   3547:     if (($env{'request.course.id'}) &&
                   3548:         ($form{'grade_courseid'} eq $env{'request.course.id'}) &&
                   3549:         ($form{'grade_username'} ne '') && ($form{'grade_domain'} ne '') &&
                   3550:         ($form{'grade_symb'} ne '') &&
                   3551:         (&Apache::lonnet::allowed('mgr',$env{'request.course.id'}.
                   3552:                                  ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:'')))) {
                   3553:         $islocal = 1;
                   3554:     }
1.1447    raeburn  3555:     $response= &LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,
                   3556:                                              '','','',$islocal);
1.1182    foxr     3557: 
1.944     foxr     3558:     if (wantarray) {
1.1345    raeburn  3559: 	return ($response->content, $response);
1.944     foxr     3560:     } else {
1.1345    raeburn  3561: 	return $response->content;
1.942     foxr     3562:     }
1.324     www      3563: }
                   3564: 
                   3565: sub externalssi {
                   3566:     my ($url)=@_;
                   3567:     my $request=new HTTP::Request('GET',$url);
1.1345    raeburn  3568:     my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar);
1.954     raeburn  3569:     if (wantarray) {
                   3570:         return ($response->content, $response);
                   3571:     } else {
                   3572:         return $response->content;
                   3573:     }
1.15      www      3574: }
1.254     www      3575: 
1.1354    raeburn  3576: 
                   3577: # If the local copy of a replicated resource is outdated, trigger a  
                   3578: # connection from the homeserver to flush the delayed queue. If no update 
                   3579: # happens, remove local copies of outdated resource (and corresponding
                   3580: # metadata file).
                   3581: 
1.1352    raeburn  3582: sub remove_stale_resfile {
                   3583:     my ($url) = @_;
1.1354    raeburn  3584:     my $removed;
1.1352    raeburn  3585:     if ($url=~m{^/res/($match_domain)/($match_username)/}) {
                   3586:         my $audom = $1;
                   3587:         my $auname = $2;
1.1353    raeburn  3588:         unless (($url =~ /\.\d+\.\w+$/) || ($url =~ m{^/res/lib/templates/})) {
1.1352    raeburn  3589:             my $homeserver = &homeserver($auname,$audom);
                   3590:             unless (($homeserver eq 'no_host') ||
                   3591:                     (grep { $_ eq $homeserver } &current_machine_ids())) {
                   3592:                 my $fname = &filelocation('',$url);
                   3593:                 if (-e $fname) {
                   3594:                     my $hostname = &hostname($homeserver);
                   3595:                     if ($hostname) {
1.1397    raeburn  3596:                         my $protocol = $protocol{$homeserver};
                   3597:                         $protocol = 'http' if ($protocol ne 'https');
1.1352    raeburn  3598:                         my $uri = &declutter($url);
                   3599:                         my $request=new HTTP::Request('HEAD',$protocol.'://'.$hostname.'/raw/'.$uri);
                   3600:                         my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,5,0,1);
                   3601:                         if ($response->is_success()) {
                   3602:                             my $remmodtime = &HTTP::Date::str2time( $response->header('Last-modified') );
                   3603:                             my $locmodtime = (stat($fname))[9];
                   3604:                             if ($locmodtime < $remmodtime) {
1.1354    raeburn  3605:                                 my $stale;
                   3606:                                 my $answer = &reply('pong',$homeserver);
                   3607:                                 if ($answer eq $homeserver.':'.$perlvar{'lonHostID'}) {
                   3608:                                     sleep(0.2);
                   3609:                                     $locmodtime = (stat($fname))[9];
                   3610:                                     if ($locmodtime < $remmodtime) {
                   3611:                                         my $posstransfer = $fname.'.in.transfer';
                   3612:                                         if ((-e $posstransfer) && ($remmodtime < (stat($posstransfer))[9])) {
                   3613:                                             $removed = 1;
                   3614:                                         } else {
                   3615:                                             $stale = 1;
                   3616:                                         }
                   3617:                                     } else {
                   3618:                                         $removed = 1;
                   3619:                                     }
                   3620:                                 } else {
                   3621:                                     $stale = 1;
                   3622:                                 }
                   3623:                                 if ($stale) {
1.1421    raeburn  3624:                                     if (unlink($fname)) {
                   3625:                                         if ($uri!~/\.meta$/) {
                   3626:                                             if (-e $fname.'.meta') {
                   3627:                                                 unlink($fname.'.meta');
                   3628:                                             }
                   3629:                                         }
1.1422    raeburn  3630:                                         my $unsubresult = &unsubscribe($fname);
                   3631:                                         unless ($unsubresult eq 'ok') {
                   3632:                                             &logthis("no unsub of $fname from $homeserver, reason: $unsubresult");
                   3633:                                         }
1.1421    raeburn  3634:                                         $removed = 1;
1.1354    raeburn  3635:                                     }
1.1352    raeburn  3636:                                 }
                   3637:                             }
                   3638:                         }
                   3639:                     }
                   3640:                 }
                   3641:             }
                   3642:         }
                   3643:     }
1.1354    raeburn  3644:     return $removed;
1.1352    raeburn  3645: }
                   3646: 
1.492     albertel 3647: # -------------------------------- Allow a /uploaded/ URI to be vouched for
                   3648: 
                   3649: sub allowuploaded {
                   3650:     my ($srcurl,$url)=@_;
                   3651:     $url=&clutter(&declutter($url));
                   3652:     my $dir=$url;
                   3653:     $dir=~s/\/[^\/]+$//;
                   3654:     my %httpref=();
                   3655:     my $httpurl=&hreflocation('',$url);
                   3656:     $httpref{'httpref.'.$httpurl}=$srcurl;
1.949     raeburn  3657:     &Apache::lonnet::appenv(\%httpref);
1.254     www      3658: }
1.477     raeburn  3659: 
1.1193    raeburn  3660: #
                   3661: # Determine if the current user should be able to edit a particular resource,
                   3662: # when viewing in course context.
                   3663: # (a) When viewing resource used to determine if "Edit" item is included in 
                   3664: #     Functions.
                   3665: # (b) When displaying folder contents in course editor, used to determine if
                   3666: #     "Edit" link will be displayed alongside resource.
                   3667: #
1.1196    raeburn  3668: #  input: six args -- filename (decluttered), course number, course domain,
                   3669: #                   url, symb (if registered) and group (if this is a group
                   3670: #                   item -- e.g., bulletin board, group page etc.).
                   3671: #  output: array of five scalars -- 
1.1193    raeburn  3672: #          $cfile -- url for file editing if editable on current server
                   3673: #          $home -- homeserver of resource (i.e., for author if published,
                   3674: #                                           or course if uploaded.).
                   3675: #          $switchserver --  1 if server switch will be needed.
1.1196    raeburn  3676: #          $forceedit -- 1 if icon/link should be to go to edit mode 
                   3677: #          $forceview -- 1 if icon/link should be to go to view mode
1.1193    raeburn  3678: #
                   3679: 
                   3680: sub can_edit_resource {
1.1194    raeburn  3681:     my ($file,$cnum,$cdom,$resurl,$symb,$group) = @_;
                   3682:     my ($cfile,$home,$switchserver,$forceedit,$forceview,$uploaded,$incourse);
                   3683: #
                   3684: # For aboutme pages user can only edit his/her own.
                   3685: #
1.1199    raeburn  3686:     if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
1.1194    raeburn  3687:         my ($sdom,$sname) = ($1,$2);
                   3688:         if (($sdom eq $env{'user.domain'}) && ($sname eq $env{'user.name'})) {
                   3689:             $home = $env{'user.home'};
                   3690:             $cfile = $resurl;
                   3691:             if ($env{'form.forceedit'}) {
                   3692:                 $forceview = 1;
                   3693:             } else {
                   3694:                 $forceedit = 1;
                   3695:             }
                   3696:             return ($cfile,$home,$switchserver,$forceedit,$forceview);
                   3697:         } else {
                   3698:             return;
                   3699:         }
                   3700:     }
                   3701: 
                   3702:     if ($env{'request.course.id'}) {
                   3703:         my $crsedit = &Apache::lonnet::allowed('mdc',$env{'request.course.id'});
                   3704:         if ($group ne '') {
                   3705: # if this is a group homepage or group bulletin board, check group privs
                   3706:             my $allowed = 0;
1.1197    raeburn  3707:             if ($resurl =~ m{^/?adm/$cdom/$cnum/$group/smppg$}) {
                   3708:                 if ((&allowed('mdg',$env{'request.course.id'}.
1.1198    raeburn  3709:                               ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
1.1194    raeburn  3710:                         (&allowed('mgh',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
                   3711:                     $allowed = 1;
                   3712:                 }
1.1197    raeburn  3713:             } elsif ($resurl =~ m{^/?adm/$cdom/$cnum/\d+/bulletinboard$}) {
                   3714:                 if ((&allowed('mdg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
                   3715:                         (&allowed('cgb',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
1.1194    raeburn  3716:                     $allowed = 1;
                   3717:                 }
                   3718:             }
                   3719:             if ($allowed) {
                   3720:                 $home=&homeserver($cnum,$cdom);
                   3721:                 if ($env{'form.forceedit'}) {
                   3722:                     $forceview = 1;
                   3723:                 } else {
                   3724:                     $forceedit = 1;
                   3725:                 }
                   3726:                 $cfile = $resurl;
                   3727:             } else {
                   3728:                 return;
                   3729:             }
                   3730:         } else {
1.1208    raeburn  3731:             if ($resurl =~ m{^/?adm/viewclasslist$}) {
                   3732:                 unless (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
                   3733:                     return;
                   3734:                 }
                   3735:             } elsif (!$crsedit) {
1.1194    raeburn  3736: #
                   3737: # No edit allowed where CC has switched to student role.
                   3738: #
                   3739:                 return;
                   3740:             }
                   3741:         }
                   3742:     }
                   3743: 
1.1193    raeburn  3744:     if ($file ne '') {
                   3745:         if (($cnum =~ /$match_courseid/) && ($cdom =~ /$match_domain/)) {
1.1194    raeburn  3746:             if (&is_course_upload($file,$cnum,$cdom)) {
                   3747:                 $uploaded = 1;
                   3748:                 $incourse = 1;
1.1193    raeburn  3749:                 if ($file =~/\.(htm|html|css|js|txt)$/) {
                   3750:                     $cfile = &hreflocation('',$file);
1.1201    raeburn  3751:                     if ($env{'form.forceedit'}) {
                   3752:                         $forceview = 1;
                   3753:                     } else {
                   3754:                         $forceedit = 1;
                   3755:                     }
1.1194    raeburn  3756:                 }
                   3757:             } elsif ($resurl =~ m{^/public/$cdom/$cnum/syllabus}) {
                   3758:                 $incourse = 1;
                   3759:                 if ($env{'form.forceedit'}) {
                   3760:                     $forceview = 1;
                   3761:                 } else {
                   3762:                     $forceedit = 1;
                   3763:                 }
                   3764:                 $cfile = $resurl;
                   3765:             } elsif (($resurl ne '') && (&is_on_map($resurl))) { 
                   3766:                 if ($resurl =~ m{^/adm/$match_domain/$match_username/\d+/smppg|bulletinboard$}) {
                   3767:                     $incourse = 1;
                   3768:                     if ($env{'form.forceedit'}) {
                   3769:                         $forceview = 1;
                   3770:                     } else {
                   3771:                         $forceedit = 1;
                   3772:                     }
                   3773:                     $cfile = $resurl;
1.1199    raeburn  3774:                 } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem') {
1.1194    raeburn  3775:                     $incourse = 1;
                   3776:                     $cfile = $resurl.'/smpedit';
1.1199    raeburn  3777:                 } elsif ($resurl =~ m{^/adm/wrapper/ext/}) {
1.1194    raeburn  3778:                     $incourse = 1;
1.1199    raeburn  3779:                     if ($env{'form.forceedit'}) {
                   3780:                         $forceview = 1;
                   3781:                     } else {
                   3782:                         $forceedit = 1;
                   3783:                     }
                   3784:                     $cfile = $resurl;
1.1419    raeburn  3785:                 } elsif (($resurl =~ m{^/ext/}) && ($symb ne '')) {
                   3786:                     my ($map,$id,$res) = &decode_symb($symb);
                   3787:                     if ($map =~ /\.page$/) {
                   3788:                         $incourse = 1;
                   3789:                         if ($env{'form.forceedit'}) {
                   3790:                             $forceview = 1;
                   3791:                             $cfile = $map;
                   3792:                         } else {
                   3793:                             $forceedit = 1;
                   3794:                             $cfile =  '/adm/wrapper'.$resurl;
                   3795:                         }
                   3796:                     }
1.1343    raeburn  3797:                 } elsif ($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) {
1.1298    raeburn  3798:                     $incourse = 1;
                   3799:                     if ($env{'form.forceedit'}) {
                   3800:                         $forceview = 1;
                   3801:                     } else {
                   3802:                         $forceedit = 1;
                   3803:                     }
                   3804:                     $cfile = $resurl;
1.1208    raeburn  3805:                 } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
                   3806:                     $incourse = 1;
                   3807:                     if ($env{'form.forceedit'}) {
                   3808:                         $forceview = 1;
                   3809:                     } else {
                   3810:                         $forceedit = 1;
                   3811:                     }
                   3812:                     $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
1.1194    raeburn  3813:                 }
                   3814:             } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
                   3815:                 my $template = '/res/lib/templates/simpleproblem.problem';
                   3816:                 if (&is_on_map($template)) { 
                   3817:                     $incourse = 1;
                   3818:                     $forceview = 1;
                   3819:                     $cfile = $template;
1.1193    raeburn  3820:                 }
1.1199    raeburn  3821:             } elsif (($resurl =~ m{^/adm/wrapper/ext/}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
1.1418    raeburn  3822:                 $incourse = 1;
                   3823:                 if ($env{'form.forceedit'}) {
                   3824:                     $forceview = 1;
                   3825:                 } else {
                   3826:                     $forceedit = 1;
                   3827:                 }
                   3828:                 $cfile = $resurl;
1.1343    raeburn  3829:             } elsif (($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
1.1298    raeburn  3830:                 $incourse = 1;
                   3831:                 if ($env{'form.forceedit'}) {
                   3832:                     $forceview = 1;
                   3833:                 } else {
                   3834:                     $forceedit = 1;
                   3835:                 }
                   3836:                 $cfile = $resurl;
1.1199    raeburn  3837:             } elsif (($resurl eq '/adm/extresedit') && ($symb || $env{'form.folderpath'})) {
                   3838:                 $incourse = 1;
                   3839:                 $forceview = 1;
                   3840:                 if ($symb) {
                   3841:                     my ($map,$id,$res)=&decode_symb($symb);
                   3842:                     $env{'request.symb'} = $symb;
                   3843:                     $cfile = &clutter($res);
                   3844:                 } else {
                   3845:                     $cfile = $env{'form.suppurl'};
1.1298    raeburn  3846:                     my $escfile = &unescape($cfile);
1.1343    raeburn  3847:                     if ($escfile =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
1.1298    raeburn  3848:                         $cfile = '/adm/wrapper'.$escfile;
                   3849:                     } else {
                   3850:                         $escfile =~ s{^http://}{};
                   3851:                         $cfile = &escape("/adm/wrapper/ext/$escfile");
                   3852:                     }
1.1199    raeburn  3853:                 }
1.1234    raeburn  3854:             } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
                   3855:                 if ($env{'form.forceedit'}) {
                   3856:                     $forceview = 1;
                   3857:                 } else {
                   3858:                     $forceedit = 1;
                   3859:                 }
                   3860:                 $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
1.1193    raeburn  3861:             }
                   3862:         }
1.1194    raeburn  3863:         if ($uploaded || $incourse) {
                   3864:             $home=&homeserver($cnum,$cdom);
1.1207    raeburn  3865:         } elsif ($file !~ m{/$}) {
1.1193    raeburn  3866:             $file=~s{^(priv/$match_domain/$match_username)}{/$1};
                   3867:             $file=~s{^($match_domain/$match_username)}{/priv/$1};
                   3868:             # Check that the user has permission to edit this resource
                   3869:             my $setpriv = 1;
                   3870:             my ($cfuname,$cfudom)=&constructaccess($file,$setpriv);
                   3871:             if (defined($cfudom)) {
                   3872:                 $home=&homeserver($cfuname,$cfudom);
                   3873:                 $cfile=$file;
                   3874:             }
                   3875:         }
1.1194    raeburn  3876:         if (($cfile ne '') && (!$incourse || $uploaded) && 
                   3877:             (($home ne '') && ($home ne 'no_host'))) {
1.1193    raeburn  3878:             my @ids=&current_machine_ids();
                   3879:             unless (grep(/^\Q$home\E$/,@ids)) {
                   3880:                 $switchserver=1;
                   3881:             }
                   3882:         }
                   3883:     }
1.1194    raeburn  3884:     return ($cfile,$home,$switchserver,$forceedit,$forceview);
1.1193    raeburn  3885: }
                   3886: 
                   3887: sub is_course_upload {
                   3888:     my ($file,$cnum,$cdom) = @_;
                   3889:     my $uploadpath = &LONCAPA::propath($cdom,$cnum);
                   3890:     $uploadpath =~ s{^\/}{};
1.1201    raeburn  3891:     if (($file =~ m{^\Q$uploadpath\E/userfiles/(docs|supplemental)/}) ||
                   3892:         ($file =~ m{^userfiles/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/})) {
1.1193    raeburn  3893:         return 1;
                   3894:     }
                   3895:     return;
                   3896: }
                   3897: 
1.1194    raeburn  3898: sub in_course {
1.1195    raeburn  3899:     my ($udom,$uname,$cdom,$cnum,$type,$hideprivileged) = @_;
                   3900:     if ($hideprivileged) {
                   3901:         my $skipuser;
1.1219    raeburn  3902:         my %coursehash = &coursedescription($cdom.'_'.$cnum);
                   3903:         my @possdoms = ($cdom);  
                   3904:         if ($coursehash{'checkforpriv'}) { 
                   3905:             push(@possdoms,split(/,/,$coursehash{'checkforpriv'})); 
                   3906:         }
                   3907:         if (&privileged($uname,$udom,\@possdoms)) {
1.1195    raeburn  3908:             $skipuser = 1;
                   3909:             if ($coursehash{'nothideprivileged'}) {
                   3910:                 foreach my $item (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
                   3911:                     my $user;
                   3912:                     if ($item =~ /:/) {
                   3913:                         $user = $item;
                   3914:                     } else {
                   3915:                         $user = join(':',split(/[\@]/,$item));
                   3916:                     }
                   3917:                     if ($user eq $uname.':'.$udom) {
                   3918:                         undef($skipuser);
                   3919:                         last;
                   3920:                     }
                   3921:                 }
                   3922:             }
                   3923:             if ($skipuser) {
                   3924:                 return 0;
                   3925:             }
                   3926:         }
                   3927:     }
1.1194    raeburn  3928:     $type ||= 'any';
                   3929:     if (!defined($cdom) || !defined($cnum)) {
                   3930:         my $cid  = $env{'request.course.id'};
                   3931:         $cdom = $env{'course.'.$cid.'.domain'};
                   3932:         $cnum = $env{'course.'.$cid.'.num'};
                   3933:     }
                   3934:     my $typesref;
1.1195    raeburn  3935:     if (($type eq 'any') || ($type eq 'all')) {
1.1194    raeburn  3936:         $typesref = ['active','previous','future'];
                   3937:     } elsif ($type eq 'previous' || $type eq 'future') {
                   3938:         $typesref = [$type];
                   3939:     }
                   3940:     my %roles = &get_my_roles($uname,$udom,'userroles',
                   3941:                               $typesref,undef,[$cdom]);
                   3942:     my ($tmp) = keys(%roles);
                   3943:     return 0 if ($tmp =~ /^(con_lost|error|no_such_host)/i);
                   3944:     my @course_roles = grep(/^\Q$cnum\E:\Q$cdom\E:/, keys(%roles));
                   3945:     if (@course_roles > 0) {
                   3946:         return 1;
                   3947:     }
                   3948:     return 0;
                   3949: }
                   3950: 
1.478     albertel 3951: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1.638     albertel 3952: # input: action, courseID, current domain, intended
1.637     raeburn  3953: #        path to file, source of file, instruction to parse file for objects,
                   3954: #        ref to hash for embedded objects,
                   3955: #        ref to hash for codebase of java objects.
1.1095    raeburn  3956: #        reference to scalar to accommodate mime type determined
                   3957: #          from File::MMagic if $parser = parse.
1.637     raeburn  3958: #
1.485     raeburn  3959: # output: url to file (if action was uploaddoc), 
                   3960: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1.477     raeburn  3961: #
1.478     albertel 3962: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
                   3963: # course.
1.477     raeburn  3964: #
1.478     albertel 3965: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   3966: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
                   3967: #          course's home server.
1.477     raeburn  3968: #
1.478     albertel 3969: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
                   3970: #          be copied from $source (current location) to 
                   3971: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   3972: #         and will then be copied to
                   3973: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
                   3974: #         course's home server.
1.485     raeburn  3975: #
1.481     raeburn  3976: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1.620     albertel 3977: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
1.481     raeburn  3978: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   3979: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
                   3980: #         in course's home server.
1.637     raeburn  3981: #
1.477     raeburn  3982: 
                   3983: sub process_coursefile {
1.1095    raeburn  3984:     my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
                   3985:         $mimetype)=@_;
1.477     raeburn  3986:     my $fetchresult;
1.638     albertel 3987:     my $home=&homeserver($docuname,$docudom);
1.477     raeburn  3988:     if ($action eq 'propagate') {
1.638     albertel 3989:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
                   3990: 			     $home);
1.481     raeburn  3991:     } else {
1.477     raeburn  3992:         my $fpath = '';
                   3993:         my $fname = $file;
1.478     albertel 3994:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1.477     raeburn  3995:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1.637     raeburn  3996:         my $filepath = &build_filepath($fpath);
1.481     raeburn  3997:         if ($action eq 'copy') {
                   3998:             if ($source eq '') {
                   3999:                 $fetchresult = 'no source file';
                   4000:                 return $fetchresult;
                   4001:             } else {
                   4002:                 my $destination = $filepath.'/'.$fname;
                   4003:                 rename($source,$destination);
                   4004:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 4005:                                  $home);
1.481     raeburn  4006:             }
                   4007:         } elsif ($action eq 'uploaddoc') {
1.1359    raeburn  4008:             open(my $fh,'>',$filepath.'/'.$fname);
1.620     albertel 4009:             print $fh $env{'form.'.$source};
1.481     raeburn  4010:             close($fh);
1.637     raeburn  4011:             if ($parser eq 'parse') {
1.1024    raeburn  4012:                 my $mm = new File::MMagic;
1.1095    raeburn  4013:                 my $type = $mm->checktype_filename($filepath.'/'.$fname);
                   4014:                 if ($type eq 'text/html') {
1.1024    raeburn  4015:                     my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
                   4016:                     unless ($parse_result eq 'ok') {
                   4017:                         &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
                   4018:                     }
1.637     raeburn  4019:                 }
1.1095    raeburn  4020:                 if (ref($mimetype)) {
                   4021:                     $$mimetype = $type;
                   4022:                 } 
1.637     raeburn  4023:             }
1.477     raeburn  4024:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 4025:                                  $home);
1.481     raeburn  4026:             if ($fetchresult eq 'ok') {
                   4027:                 return '/uploaded/'.$fpath.'/'.$fname;
                   4028:             } else {
                   4029:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638     albertel 4030:                         ' to host '.$home.': '.$fetchresult);
1.481     raeburn  4031:                 return '/adm/notfound.html';
                   4032:             }
1.477     raeburn  4033:         }
                   4034:     }
1.485     raeburn  4035:     unless ( $fetchresult eq 'ok') {
1.477     raeburn  4036:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638     albertel 4037:              ' to host '.$home.': '.$fetchresult);
1.477     raeburn  4038:     }
                   4039:     return $fetchresult;
                   4040: }
                   4041: 
1.637     raeburn  4042: sub build_filepath {
                   4043:     my ($fpath) = @_;
                   4044:     my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
                   4045:     unless ($fpath eq '') {
                   4046:         my @parts=split('/',$fpath);
                   4047:         foreach my $part (@parts) {
                   4048:             $filepath.= '/'.$part;
                   4049:             if ((-e $filepath)!=1) {
                   4050:                 mkdir($filepath,0777);
                   4051:             }
                   4052:         }
                   4053:     }
                   4054:     return $filepath;
                   4055: }
                   4056: 
                   4057: sub store_edited_file {
1.638     albertel 4058:     my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
1.637     raeburn  4059:     my $file = $primary_url;
                   4060:     $file =~ s#^/uploaded/$docudom/$docuname/##;
                   4061:     my $fpath = '';
                   4062:     my $fname = $file;
                   4063:     ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
                   4064:     $fpath=$docudom.'/'.$docuname.'/'.$fpath;
                   4065:     my $filepath = &build_filepath($fpath);
1.1359    raeburn  4066:     open(my $fh,'>',$filepath.'/'.$fname);
1.637     raeburn  4067:     print $fh $content;
                   4068:     close($fh);
1.638     albertel 4069:     my $home=&homeserver($docuname,$docudom);
1.637     raeburn  4070:     $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 4071: 			  $home);
1.637     raeburn  4072:     if ($$fetchresult eq 'ok') {
                   4073:         return '/uploaded/'.$fpath.'/'.$fname;
                   4074:     } else {
1.638     albertel 4075:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
                   4076: 		 ' to host '.$home.': '.$$fetchresult);
1.637     raeburn  4077:         return '/adm/notfound.html';
                   4078:     }
                   4079: }
                   4080: 
1.531     albertel 4081: sub clean_filename {
1.831     albertel 4082:     my ($fname,$args)=@_;
1.315     www      4083: # Replace Windows backslashes by forward slashes
1.257     www      4084:     $fname=~s/\\/\//g;
1.831     albertel 4085:     if (!$args->{'keep_path'}) {
                   4086:         # Get rid of everything but the actual filename
                   4087: 	$fname=~s/^.*\/([^\/]+)$/$1/;
                   4088:     }
1.315     www      4089: # Replace spaces by underscores
                   4090:     $fname=~s/\s+/\_/g;
1.1406    raeburn  4091: # Transliterate non-ascii text to ascii
                   4092:     my $lang = &Apache::lonlocal::current_language();
                   4093:     $fname = &LONCAPA::transliterate::fname_to_ascii($fname,$lang);
1.315     www      4094: # Replace all other weird characters by nothing
1.831     albertel 4095:     $fname=~s{[^/\w\.\-]}{}g;
1.540     albertel 4096: # Replace all .\d. sequences with _\d. so they no longer look like version
                   4097: # numbers
                   4098:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
1.1443    raeburn  4099: # Replace three or more adjacent underscores with one for consistency 
                   4100: # with loncfile::filename_check() so complete url can be extracted by
                   4101: # lonnet::decode_symb()
                   4102:     $fname=~s/_{3,}/_/g;
1.531     albertel 4103:     return $fname;
                   4104: }
1.1406    raeburn  4105: 
1.1051    raeburn  4106: # This Function checks if an Image's dimensions exceed either $resizewidth (width) 
                   4107: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an 
                   4108: # image with the same aspect ratio as the original, but with dimensions which do 
                   4109: # not exceed $resizewidth and $resizeheight.
                   4110:  
1.984     neumanie 4111: sub resizeImage {
1.1051    raeburn  4112:     my ($img_path,$resizewidth,$resizeheight) = @_;
                   4113:     my $ima = Image::Magick->new;
                   4114:     my $resized;
                   4115:     if (-e $img_path) {
                   4116:         $ima->Read($img_path);
                   4117:         if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
                   4118:             my $width = $ima->Get('width');
                   4119:             my $height = $ima->Get('height');
                   4120:             if ($width > $resizewidth) {
                   4121: 	        my $factor = $width/$resizewidth;
                   4122:                 my $newheight = $height/$factor;
                   4123:                 $ima->Scale(width=>$resizewidth,height=>$newheight);
                   4124:                 $resized = 1;
                   4125:             }
                   4126:         }
                   4127:         if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
                   4128:             my $width = $ima->Get('width');
                   4129:             my $height = $ima->Get('height');
                   4130:             if ($height > $resizeheight) {
                   4131:                 my $factor = $height/$resizeheight;
                   4132:                 my $newwidth = $width/$factor;
                   4133:                 $ima->Scale(width=>$newwidth,height=>$resizeheight);
                   4134:                 $resized = 1;
                   4135:             }
                   4136:         }
                   4137:         if ($resized) {
                   4138:             $ima->Write($img_path);
                   4139:         }
                   4140:     }
                   4141:     return;
1.977     amueller 4142: }
                   4143: 
1.608     albertel 4144: # --------------- Take an uploaded file and put it into the userfiles directory
1.686     albertel 4145: # input: $formname - the contents of the file are in $env{"form.$formname"}
1.1093    raeburn  4146: #                    the desired filename is in $env{"form.$formname.filename"}
1.1090    raeburn  4147: #        $context - possible values: coursedoc, existingfile, overwrite, 
1.1401    raeburn  4148: #                                    canceloverwrite, scantron or ''.
1.1090    raeburn  4149: #                   if 'coursedoc': upload to the current course
                   4150: #                   if 'existingfile': write file to tmp/overwrites directory 
                   4151: #                   if 'canceloverwrite': delete file written to tmp/overwrites directory
                   4152: #                   $context is passed as argument to &finishuserfileupload
1.686     albertel 4153: #        $subdir - directory in userfile to store the file into
1.1401    raeburn  4154: #        $parser - instruction to parse file for objects ($parser = parse) or
                   4155: #                  if context is 'scantron', $parser is hashref of csv column mapping
                   4156: #                  (e.g.,{ PaperID => 0, LastName => 1, FirstName => 2, ID => 3, 
                   4157: #                          Section => 4, CODE => 5, FirstQuestion => 9 }).
1.858     raeburn  4158: #        $allfiles - reference to hash for embedded objects
                   4159: #        $codebase - reference to hash for codebase of java objects
                   4160: #        $desuname - username for permanent storage of uploaded file
                   4161: #        $dsetudom - domain for permanaent storage of uploaded file
1.860     raeburn  4162: #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
                   4163: #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
1.1051    raeburn  4164: #        $resizewidth - width (pixels) to which to resize uploaded image
                   4165: #        $resizeheight - height (pixels) to which to resize uploaded image
1.1095    raeburn  4166: #        $mimetype - reference to scalar to accommodate mime type determined
1.1152    raeburn  4167: #                    from File::MMagic.
1.858     raeburn  4168: # 
1.686     albertel 4169: # output: url of file in userspace, or error: <message> 
                   4170: #             or /adm/notfound.html if failure to upload occurse
1.608     albertel 4171: 
1.531     albertel 4172: sub userfileupload {
1.1090    raeburn  4173:     my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
1.1095    raeburn  4174:         $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
1.531     albertel 4175:     if (!defined($subdir)) { $subdir='unknown'; }
1.620     albertel 4176:     my $fname=$env{'form.'.$formname.'.filename'};
1.531     albertel 4177:     $fname=&clean_filename($fname);
1.1090    raeburn  4178:     # See if there is anything left
1.257     www      4179:     unless ($fname) { return 'error: no uploaded file'; }
1.1406    raeburn  4180:     # If filename now begins with a . prepend unix timestamp _ milliseconds
                   4181:     if ($fname =~ /^\./) {
                   4182:         my ($s,$usec) = &gettimeofday();
                   4183:         while (length($usec) < 6) {
                   4184:             $usec = '0'.$usec;
                   4185:         }
                   4186:         $fname = $s.'_'.substr($usec,0,3).$fname;
                   4187:     }
1.1090    raeburn  4188:     # Files uploaded to help request form, or uploaded to "create course" page are handled differently
                   4189:     if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
                   4190:         (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
                   4191:          ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
1.523     raeburn  4192:         my $now = time;
1.1090    raeburn  4193:         my $filepath;
1.1095    raeburn  4194:         if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
1.1090    raeburn  4195:              $filepath = 'tmp/helprequests/'.$now;
                   4196:         } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
                   4197:              $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
                   4198:                          '_'.$env{'user.domain'}.'/pending';
                   4199:         } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
                   4200:             my ($docuname,$docudom);
1.1350    raeburn  4201:             if ($destudom =~ /^$match_domain$/) {
1.1090    raeburn  4202:                 $docudom = $destudom;
                   4203:             } else {
                   4204:                 $docudom = $env{'user.domain'};
                   4205:             }
1.1350    raeburn  4206:             if ($destuname =~ /^$match_username$/) {
1.1090    raeburn  4207:                 $docuname = $destuname;
                   4208:             } else {
                   4209:                 $docuname = $env{'user.name'};
                   4210:             }
                   4211:             if (exists($env{'form.group'})) {
                   4212:                 $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   4213:                 $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   4214:             }
                   4215:             $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
                   4216:             if ($context eq 'canceloverwrite') {
                   4217:                 my $tempfile =  $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
                   4218:                 if (-e  $tempfile) {
                   4219:                     my @info = stat($tempfile);
                   4220:                     if ($info[9] eq $env{'form.timestamp'}) {
                   4221:                         unlink($tempfile);
                   4222:                     }
                   4223:                 }
                   4224:                 return;
1.523     raeburn  4225:             }
                   4226:         }
1.1090    raeburn  4227:         # Create the directory if not present
1.741     raeburn  4228:         my @parts=split(/\//,$filepath);
                   4229:         my $fullpath = $perlvar{'lonDaemons'};
                   4230:         for (my $i=0;$i<@parts;$i++) {
                   4231:             $fullpath .= '/'.$parts[$i];
                   4232:             if ((-e $fullpath)!=1) {
                   4233:                 mkdir($fullpath,0777);
                   4234:             }
                   4235:         }
1.1359    raeburn  4236:         open(my $fh,'>',$fullpath.'/'.$fname);
1.741     raeburn  4237:         print $fh $env{'form.'.$formname};
                   4238:         close($fh);
1.1090    raeburn  4239:         if ($context eq 'existingfile') {
                   4240:             my @info = stat($fullpath.'/'.$fname);
                   4241:             return ($fullpath.'/'.$fname,$info[9]);
                   4242:         } else {
                   4243:             return $fullpath.'/'.$fname;
                   4244:         }
1.523     raeburn  4245:     }
1.995     raeburn  4246:     if ($subdir eq 'scantron') {
                   4247:         $fname = 'scantron_orig_'.$fname;
1.1093    raeburn  4248:     } else {
1.995     raeburn  4249:         $fname="$subdir/$fname";
                   4250:     }
1.1090    raeburn  4251:     if ($context eq 'coursedoc') {
1.638     albertel 4252: 	my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   4253: 	my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.646     raeburn  4254:         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
1.638     albertel 4255:             return &finishuserfileupload($docuname,$docudom,
                   4256: 					 $formname,$fname,$parser,$allfiles,
1.1051    raeburn  4257: 					 $codebase,$thumbwidth,$thumbheight,
1.1095    raeburn  4258:                                          $resizewidth,$resizeheight,$context,$mimetype);
1.481     raeburn  4259:         } else {
1.1218    raeburn  4260:             if ($env{'form.folder'}) {
                   4261:                 $fname=$env{'form.folder'}.'/'.$fname;
                   4262:             }
1.638     albertel 4263:             return &process_coursefile('uploaddoc',$docuname,$docudom,
                   4264: 				       $fname,$formname,$parser,
1.1095    raeburn  4265: 				       $allfiles,$codebase,$mimetype);
1.481     raeburn  4266:         }
1.719     banghart 4267:     } elsif (defined($destuname)) {
                   4268:         my $docuname=$destuname;
                   4269:         my $docudom=$destudom;
1.860     raeburn  4270: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
                   4271: 				     $parser,$allfiles,$codebase,
1.1051    raeburn  4272:                                      $thumbwidth,$thumbheight,
1.1095    raeburn  4273:                                      $resizewidth,$resizeheight,$context,$mimetype);
1.259     www      4274:     } else {
1.638     albertel 4275:         my $docuname=$env{'user.name'};
                   4276:         my $docudom=$env{'user.domain'};
1.1220    raeburn  4277:         if ((exists($env{'form.group'})) || ($context eq 'syllabus')) {
1.714     raeburn  4278:             $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   4279:             $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   4280:         }
1.860     raeburn  4281: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
                   4282: 				     $parser,$allfiles,$codebase,
1.1051    raeburn  4283:                                      $thumbwidth,$thumbheight,
1.1095    raeburn  4284:                                      $resizewidth,$resizeheight,$context,$mimetype);
1.259     www      4285:     }
1.271     www      4286: }
                   4287: 
                   4288: sub finishuserfileupload {
1.860     raeburn  4289:     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
1.1095    raeburn  4290:         $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
1.477     raeburn  4291:     my $path=$docudom.'/'.$docuname.'/';
1.258     www      4292:     my $filepath=$perlvar{'lonDocRoot'};
1.984     neumanie 4293:   
1.860     raeburn  4294:     my ($fnamepath,$file,$fetchthumb);
1.494     albertel 4295:     $file=$fname;
                   4296:     if ($fname=~m|/|) {
                   4297:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
                   4298: 	$path.=$fnamepath.'/';
                   4299:     }
1.259     www      4300:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258     www      4301:     my $count;
                   4302:     for ($count=4;$count<=$#parts;$count++) {
                   4303:         $filepath.="/$parts[$count]";
                   4304:         if ((-e $filepath)!=1) {
                   4305: 	    mkdir($filepath,0777);
                   4306:         }
                   4307:     }
1.984     neumanie 4308: 
1.258     www      4309: # Save the file
                   4310:     {
1.1359    raeburn  4311: 	if (!open(FH,'>',$filepath.'/'.$file)) {
1.701     albertel 4312: 	    &logthis('Failed to create '.$filepath.'/'.$file);
                   4313: 	    print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
                   4314: 	    return '/adm/notfound.html';
                   4315: 	}
1.1090    raeburn  4316:         if ($context eq 'overwrite') {
1.1117    foxr     4317:             my $source =  LONCAPA::tempdir().'/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
1.1090    raeburn  4318:             my $target = $filepath.'/'.$file;
                   4319:             if (-e $source) {
                   4320:                 my @info = stat($source);
                   4321:                 if ($info[9] eq $env{'form.timestamp'}) {   
                   4322:                     unless (&File::Copy::move($source,$target)) {
                   4323:                         &logthis('Failed to overwrite '.$filepath.'/'.$file);
                   4324:                         return "Moving from $source failed";
                   4325:                     }
                   4326:                 } else {
                   4327:                     return "Temporary file: $source had unexpected date/time for last modification";
                   4328:                 }
                   4329:             } else {
                   4330:                 return "Temporary file: $source missing";
                   4331:             }
                   4332:         } elsif (!print FH ($env{'form.'.$formname})) {
1.701     albertel 4333: 	    &logthis('Failed to write to '.$filepath.'/'.$file);
                   4334: 	    print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
                   4335: 	    return '/adm/notfound.html';
                   4336: 	}
1.570     albertel 4337: 	close(FH);
1.1051    raeburn  4338:         if ($resizewidth && $resizeheight) {
                   4339:             my $mm = new File::MMagic;
                   4340:             my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
                   4341:             if ($mime_type =~ m{^image/}) {
                   4342: 	        &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
                   4343:             }  
1.977     amueller 4344: 	}
1.258     www      4345:     }
1.1152    raeburn  4346:     if (($context eq 'coursedoc') || ($parser eq 'parse')) {
                   4347:         if (ref($mimetype)) {
                   4348:             if ($$mimetype eq '') {
                   4349:                 my $mm = new File::MMagic;
                   4350:                 my $type = $mm->checktype_filename($filepath.'/'.$file);
                   4351:                 $$mimetype = $type;
                   4352:             }
                   4353:         }
                   4354:     }
1.1401    raeburn  4355:     if (($context ne 'scantron') && ($parser eq 'parse')) {
1.1152    raeburn  4356:         if ((ref($mimetype)) && ($$mimetype eq 'text/html')) {
1.1024    raeburn  4357:             my $parse_result = &extract_embedded_items($filepath.'/'.$file,
                   4358:                                                        $allfiles,$codebase);
                   4359:             unless ($parse_result eq 'ok') {
                   4360:                 &logthis('Failed to parse '.$filepath.$file.
                   4361: 	   	         ' for embedded media: '.$parse_result); 
                   4362:             }
1.637     raeburn  4363:         }
1.1401    raeburn  4364:     } elsif (($context eq 'scantron') && (ref($parser) eq 'HASH')) {
                   4365:         my $format = $env{'form.scantron_format'};
                   4366:         &bubblesheet_converter($docudom,$filepath.'/'.$file,$parser,$format);
1.637     raeburn  4367:     }
1.860     raeburn  4368:     if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
                   4369:         my $input = $filepath.'/'.$file;
                   4370:         my $output = $filepath.'/'.'tn-'.$file;
                   4371:         my $thumbsize = $thumbwidth.'x'.$thumbheight;
1.1359    raeburn  4372:         my @args = ('convert','-sample',$thumbsize,$input,$output);
                   4373:         system({$args[0]} @args);
1.860     raeburn  4374:         if (-e $filepath.'/'.'tn-'.$file) {
                   4375:             $fetchthumb  = 1; 
                   4376:         }
                   4377:     }
1.858     raeburn  4378:  
1.259     www      4379: # Notify homeserver to grep it
                   4380: #
1.984     neumanie 4381:     my $docuhome=&homeserver($docuname,$docudom);	
1.494     albertel 4382:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1.295     www      4383:     if ($fetchresult eq 'ok') {
1.860     raeburn  4384:         if ($fetchthumb) {
                   4385:             my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
                   4386:             if ($thumbresult ne 'ok') {
                   4387:                 &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
                   4388:                          $docuhome.': '.$thumbresult);
                   4389:             }
                   4390:         }
1.259     www      4391: #
1.258     www      4392: # Return the URL to it
1.494     albertel 4393:         return '/uploaded/'.$path.$file;
1.263     www      4394:     } else {
1.494     albertel 4395:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
                   4396: 		 ': '.$fetchresult);
1.263     www      4397:         return '/adm/notfound.html';
1.858     raeburn  4398:     }
1.493     albertel 4399: }
                   4400: 
1.637     raeburn  4401: sub extract_embedded_items {
1.961     raeburn  4402:     my ($fullpath,$allfiles,$codebase,$content) = @_;
1.637     raeburn  4403:     my @state = ();
1.1164    raeburn  4404:     my (%lastids,%related,%shockwave,%flashvars);
1.637     raeburn  4405:     my %javafiles = (
                   4406:                       codebase => '',
                   4407:                       code => '',
                   4408:                       archive => ''
                   4409:                     );
                   4410:     my %mediafiles = (
                   4411:                       src => '',
                   4412:                       movie => '',
                   4413:                      );
1.648     raeburn  4414:     my $p;
                   4415:     if ($content) {
                   4416:         $p = HTML::LCParser->new($content);
                   4417:     } else {
1.961     raeburn  4418:         $p = HTML::LCParser->new($fullpath);
1.648     raeburn  4419:     }
1.641     albertel 4420:     while (my $t=$p->get_token()) {
1.640     albertel 4421: 	if ($t->[0] eq 'S') {
                   4422: 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
1.886     albertel 4423: 	    push(@state, $tagname);
1.648     raeburn  4424:             if (lc($tagname) eq 'allow') {
                   4425:                 &add_filetype($allfiles,$attr->{'src'},'src');
                   4426:             }
1.640     albertel 4427: 	    if (lc($tagname) eq 'img') {
                   4428: 		&add_filetype($allfiles,$attr->{'src'},'src');
                   4429: 	    }
1.886     albertel 4430: 	    if (lc($tagname) eq 'a') {
1.1222    raeburn  4431:                 unless (($attr->{'href'} =~ /^#/) || ($attr->{'href'} eq '')) {
1.1221    raeburn  4432:                     &add_filetype($allfiles,$attr->{'href'},'href');
                   4433:                 }
1.886     albertel 4434: 	    }
1.645     raeburn  4435:             if (lc($tagname) eq 'script') {
1.1164    raeburn  4436:                 my $src;
1.645     raeburn  4437:                 if ($attr->{'archive'} =~ /\.jar$/i) {
                   4438:                     &add_filetype($allfiles,$attr->{'archive'},'archive');
                   4439:                 } else {
1.1164    raeburn  4440:                     if ($attr->{'src'} ne '') {
                   4441:                         $src = $attr->{'src'};
                   4442:                         &add_filetype($allfiles,$src,'src');
                   4443:                     }
                   4444:                 }
                   4445:                 my $text = $p->get_trimmed_text();
                   4446:                 if ($text =~ /\Qswfobject.registerObject(\E([^\)]+)\)/) {
                   4447:                     my @swfargs = split(/,/,$1);
                   4448:                     foreach my $item (@swfargs) {
                   4449:                         $item =~ s/["']//g;
                   4450:                         $item =~ s/^\s+//;
                   4451:                         $item =~ s/\s+$//;
                   4452:                     }
                   4453:                     if (($swfargs[0] ne'') && ($swfargs[2] ne '')) {
                   4454:                         if (ref($related{$swfargs[0]}) eq 'ARRAY') {
                   4455:                             push(@{$related{$swfargs[0]}},$swfargs[2]);
                   4456:                         } else {
                   4457:                             $related{$swfargs[0]} = [$swfargs[2]];
                   4458:                         }
                   4459:                     }
1.645     raeburn  4460:                 }
                   4461:             }
                   4462:             if (lc($tagname) eq 'link') {
                   4463:                 if (lc($attr->{'rel'}) eq 'stylesheet') { 
                   4464:                     &add_filetype($allfiles,$attr->{'href'},'href');
                   4465:                 }
                   4466:             }
1.640     albertel 4467: 	    if (lc($tagname) eq 'object' ||
                   4468: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
                   4469: 		foreach my $item (keys(%javafiles)) {
                   4470: 		    $javafiles{$item} = '';
                   4471: 		}
1.1164    raeburn  4472:                 if ((lc($tagname) eq 'object') && (lc($state[-2]) ne 'object')) {
                   4473:                     $lastids{lc($tagname)} = $attr->{'id'};
                   4474:                 }
1.640     albertel 4475: 	    }
                   4476: 	    if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
                   4477: 		my $name = lc($attr->{'name'});
                   4478: 		foreach my $item (keys(%javafiles)) {
                   4479: 		    if ($name eq $item) {
                   4480: 			$javafiles{$item} = $attr->{'value'};
                   4481: 			last;
                   4482: 		    }
                   4483: 		}
1.1164    raeburn  4484:                 my $pathfrom;
1.640     albertel 4485: 		foreach my $item (keys(%mediafiles)) {
                   4486: 		    if ($name eq $item) {
1.1164    raeburn  4487:                         $pathfrom = $attr->{'value'};
                   4488:                         $shockwave{$lastids{lc($state[-2])}} = $pathfrom;
                   4489: 			&add_filetype($allfiles,$pathfrom,$name);
1.640     albertel 4490: 			last;
                   4491: 		    }
                   4492: 		}
1.1164    raeburn  4493:                 if ($name eq 'flashvars') {
                   4494:                     $flashvars{$lastids{lc($state[-2])}} = $attr->{'value'};
                   4495:                 }
                   4496:                 if ($pathfrom ne '') {
                   4497:                     &embedded_dependency($allfiles,\%related,$lastids{lc($state[-2])},
                   4498:                                          $pathfrom);
                   4499:                 }
1.640     albertel 4500: 	    }
                   4501: 	    if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
                   4502: 		foreach my $item (keys(%javafiles)) {
                   4503: 		    if ($attr->{$item}) {
                   4504: 			$javafiles{$item} = $attr->{$item};
                   4505: 			last;
                   4506: 		    }
                   4507: 		}
                   4508: 		foreach my $item (keys(%mediafiles)) {
                   4509: 		    if ($attr->{$item}) {
                   4510: 			&add_filetype($allfiles,$attr->{$item},$item);
                   4511: 			last;
                   4512: 		    }
                   4513: 		}
1.1164    raeburn  4514:                 if (lc($tagname) eq 'embed') {
                   4515:                     if (($attr->{'name'} ne '') && ($attr->{'src'} ne '')) {
                   4516:                         &embedded_dependency($allfiles,\%related,$attr->{'name'},
                   4517:                                              $attr->{'src'});
                   4518:                     }
                   4519:                 }
1.640     albertel 4520: 	    }
1.1243    raeburn  4521:             if (lc($tagname) eq 'iframe') {
                   4522:                 my $src = $attr->{'src'} ;
                   4523:                 if (($src ne '') && ($src !~ m{^(/|https?://)})) {
                   4524:                     &add_filetype($allfiles,$src,'src');
                   4525:                 } elsif ($src =~ m{^/}) {
                   4526:                     if ($env{'request.course.id'}) {
                   4527:                         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   4528:                         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   4529:                         my $url = &hreflocation('',$fullpath);
                   4530:                         if ($url =~ m{^/uploaded/$cdom/$cnum/docs/(\w+/\d+)/}) {
                   4531:                             my $relpath = $1;
                   4532:                             if ($src =~ m{^/uploaded/$cdom/$cnum/docs/\Q$relpath\E/(.+)$}) {
                   4533:                                 &add_filetype($allfiles,$1,'src');
                   4534:                             }
                   4535:                         }
                   4536:                     }
                   4537:                 }
                   4538:             }
1.1164    raeburn  4539:             if ($t->[4] =~ m{/>$}) {
1.1243    raeburn  4540:                 pop(@state);
1.1164    raeburn  4541:             }
1.640     albertel 4542: 	} elsif ($t->[0] eq 'E') {
                   4543: 	    my ($tagname) = ($t->[1]);
                   4544: 	    if ($javafiles{'codebase'} ne '') {
                   4545: 		$javafiles{'codebase'} .= '/';
                   4546: 	    }  
                   4547: 	    if (lc($tagname) eq 'applet' ||
                   4548: 		lc($tagname) eq 'object' ||
                   4549: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
                   4550: 		) {
                   4551: 		foreach my $item (keys(%javafiles)) {
                   4552: 		    if ($item ne 'codebase' && $javafiles{$item} ne '') {
                   4553: 			my $file=$javafiles{'codebase'}.$javafiles{$item};
                   4554: 			&add_filetype($allfiles,$file,$item);
                   4555: 		    }
                   4556: 		}
                   4557: 	    } 
                   4558: 	    pop @state;
                   4559: 	}
                   4560:     }
1.1164    raeburn  4561:     foreach my $id (sort(keys(%flashvars))) {
                   4562:         if ($shockwave{$id} ne '') {
                   4563:             my @pairs = split(/\&/,$flashvars{$id});
                   4564:             foreach my $pair (@pairs) {
                   4565:                 my ($key,$value) = split(/\=/,$pair);
                   4566:                 if ($key eq 'thumb') {
                   4567:                     &add_filetype($allfiles,$value,$key);
                   4568:                 } elsif ($key eq 'content') {
                   4569:                     my ($path) = ($shockwave{$id} =~ m{^(.+/)[^/]+$});
                   4570:                     my ($ext) = ($value =~ /\.([^.]+)$/);
                   4571:                     if ($ext ne '') {
                   4572:                         &add_filetype($allfiles,$path.$value,$ext);
                   4573:                     }
                   4574:                 }
                   4575:             }
                   4576:         }
                   4577:     }
1.637     raeburn  4578:     return 'ok';
                   4579: }
                   4580: 
1.639     albertel 4581: sub add_filetype {
                   4582:     my ($allfiles,$file,$type)=@_;
                   4583:     if (exists($allfiles->{$file})) {
                   4584: 	unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
                   4585: 	    push(@{$allfiles->{$file}}, &escape($type));
                   4586: 	}
                   4587:     } else {
                   4588: 	@{$allfiles->{$file}} = (&escape($type));
1.637     raeburn  4589:     }
                   4590: }
                   4591: 
1.1164    raeburn  4592: sub embedded_dependency {
                   4593:     my ($allfiles,$related,$identifier,$pathfrom) = @_;
                   4594:     if ((ref($allfiles) eq 'HASH') && (ref($related) eq 'HASH')) {
                   4595:         if (($identifier ne '') &&
                   4596:             (ref($related->{$identifier}) eq 'ARRAY') &&
                   4597:             ($pathfrom ne '')) {
                   4598:             my ($path) = ($pathfrom =~ m{^(.+/)[^/]+$});
                   4599:             foreach my $dep (@{$related->{$identifier}}) {
                   4600:                 &add_filetype($allfiles,$path.$dep,'object');
                   4601:             }
                   4602:         }
                   4603:     }
                   4604:     return;
                   4605: }
                   4606: 
1.1401    raeburn  4607: sub bubblesheet_converter {
                   4608:     my ($cdom,$fullpath,$config,$format) = @_;
                   4609:     if ((&domain($cdom) ne '') &&
1.1403    raeburn  4610:         ($fullpath =~ m{^\Q$perlvar{'lonDocRoot'}/userfiles/$cdom/\E$match_courseid/scantron_orig}) &&
1.1401    raeburn  4611:         (-e $fullpath) && (ref($config) eq 'HASH') && ($format ne '')) {
1.1404    raeburn  4612:         my (%csvcols,%csvoptions);
                   4613:         if (ref($config->{'fields'}) eq 'HASH') {  
                   4614:             %csvcols = %{$config->{'fields'}};
                   4615:         }
                   4616:         if (ref($config->{'options'}) eq 'HASH') {
                   4617:             %csvoptions = %{$config->{'options'}};
                   4618:         }
1.1401    raeburn  4619:         my %csvbynum = reverse(%csvcols);
                   4620:         my %scantronconf = &get_scantron_config($format,$cdom);
                   4621:         if (keys(%scantronconf)) {
                   4622:             my %bynum = (
                   4623:                           $scantronconf{CODEstart} => 'CODEstart',
                   4624:                           $scantronconf{IDstart}   => 'IDstart',
                   4625:                           $scantronconf{PaperID}   => 'PaperID',
                   4626:                           $scantronconf{FirstName} => 'FirstName',
                   4627:                           $scantronconf{LastName}  => 'LastName',
                   4628:                           $scantronconf{Qstart}    => 'Qstart',
                   4629:                         );
                   4630:             my @ordered;
                   4631:             foreach my $item (sort { $a <=> $b } keys(%bynum)) {
1.1403    raeburn  4632:                 push(@ordered,$bynum{$item});
1.1401    raeburn  4633:             }
                   4634:             my %mapstart = (
                   4635:                               CODEstart => 'CODE',
                   4636:                               IDstart   => 'ID',
                   4637:                               PaperID   => 'PaperID',
                   4638:                               FirstName => 'FirstName',
                   4639:                               LastName  => 'LastName',
                   4640:                               Qstart    => 'FirstQuestion',
                   4641:                            );
                   4642:             my %maplength = (
                   4643:                               CODEstart => 'CODElength',
                   4644:                               IDstart   => 'IDlength',
                   4645:                               PaperID   => 'PaperIDlength',
                   4646:                               FirstName => 'FirstNamelength',
                   4647:                               LastName  => 'LastNamelength',
                   4648:             );
                   4649:             if (open(my $fh,'<',$fullpath)) {
                   4650:                 my $output;
1.1403    raeburn  4651:                 my %lettdig = &letter_to_digits();
                   4652:                 my %diglett = reverse(%lettdig);
                   4653:                 my $numletts = scalar(keys(%lettdig));
1.1404    raeburn  4654:                 my $num = 0;
1.1401    raeburn  4655:                 while (my $line=<$fh>) {
1.1404    raeburn  4656:                     $num ++;
                   4657:                     next if (($num == 1) && ($csvoptions{'hdr'} == 1));
1.1401    raeburn  4658:                     $line =~ s{[\r\n]+$}{};
                   4659:                     my %found;
                   4660:                     my @values = split(/,/,$line);
                   4661:                     my ($qstart,$record);
                   4662:                     for (my $i=0; $i<@values; $i++) {
1.1403    raeburn  4663:                         if ((($qstart ne '') && ($i > $qstart)) ||
                   4664:                             ($csvbynum{$i} eq 'FirstQuestion')) {
                   4665:                             if ($values[$i] eq '') {
                   4666:                                 $values[$i] = $scantronconf{'Qoff'};
                   4667:                             } elsif ($scantronconf{'Qon'} eq 'number') {
                   4668:                                 if ($values[$i] =~ /^[A-Ja-j]$/) {
                   4669:                                     $values[$i] = $lettdig{uc($values[$i])};
                   4670:                                 }
                   4671:                             } elsif ($scantronconf{'Qon'} eq 'letter') {
                   4672:                                 if ($values[$i] =~ /^[0-9]$/) {
                   4673:                                     $values[$i] = $diglett{$values[$i]};
                   4674:                                 }
                   4675:                             } else {
                   4676:                                 if ($values[$i] =~ /^[0-9A-Ja-j]$/) {
                   4677:                                     my $digit;
                   4678:                                     if ($values[$i] =~ /^[A-Ja-j]$/) {
                   4679:                                         $digit = $lettdig{uc($values[$i])}-1;
                   4680:                                         if ($values[$i] eq 'J') {
                   4681:                                             $digit += $numletts;
                   4682:                                         }
                   4683:                                     } elsif ($values[$i] =~ /^[0-9]$/) {
                   4684:                                         $digit = $values[$i]-1;
                   4685:                                         if ($values[$i] eq '0') {
                   4686:                                             $digit += $numletts;
                   4687:                                         }
                   4688:                                     }
                   4689:                                     my $qval='';
                   4690:                                     for (my $j=0; $j<$scantronconf{'Qlength'}; $j++) {
                   4691:                                         if ($j == $digit) {
                   4692:                                             $qval .= $scantronconf{'Qon'};
                   4693:                                         } else {
                   4694:                                             $qval .= $scantronconf{'Qoff'};
                   4695:                                         }
                   4696:                                     }
                   4697:                                     $values[$i] = $qval;
                   4698:                                 }
                   4699:                             }
                   4700:                             if (length($values[$i]) > $scantronconf{'Qlength'}) {
                   4701:                                 $values[$i] = substr($values[$i],0,$scantronconf{'Qlength'});
                   4702:                             }
                   4703:                             my $numblank = $scantronconf{'Qlength'} - length($values[$i]);
                   4704:                             if ($numblank > 0) {
                   4705:                                  $values[$i] .= ($scantronconf{'Qoff'} x $numblank);
                   4706:                             }
1.1401    raeburn  4707:                             if ($csvbynum{$i} eq 'FirstQuestion') {
                   4708:                                 $qstart = $i;
1.1403    raeburn  4709:                                 $found{$csvbynum{$i}} = $values[$i];
1.1401    raeburn  4710:                             } else {
1.1403    raeburn  4711:                                 $found{'FirstQuestion'} .= $values[$i];
                   4712:                             }
                   4713:                         } elsif (exists($csvbynum{$i})) {
1.1404    raeburn  4714:                             if ($csvoptions{'rem'}) {
                   4715:                                 $values[$i] =~ s/^\s+//;
                   4716:                             }
                   4717:                             if (($csvbynum{$i} eq 'PaperID') && ($csvoptions{'pad'})) {
1.1403    raeburn  4718:                                 while (length($values[$i]) < $scantronconf{$maplength{$csvbynum{$i}}}) {
                   4719:                                     $values[$i] = '0'.$values[$i];
1.1401    raeburn  4720:                                 }
                   4721:                             }
                   4722:                             $found{$csvbynum{$i}} = $values[$i];
                   4723:                         }
                   4724:                     }
                   4725:                     foreach my $item (@ordered) {
                   4726:                         my $currlength = 1+length($record);
                   4727:                         my $numspaces = $scantronconf{$item} - $currlength;
                   4728:                         if ($numspaces > 0) {
                   4729:                             $record .= (' ' x $numspaces);
                   4730:                         }
                   4731:                         if (($mapstart{$item} ne '') && (exists($found{$mapstart{$item}}))) {
                   4732:                             unless ($item eq 'Qstart') {
                   4733:                                 if (length($found{$mapstart{$item}}) > $scantronconf{$maplength{$item}}) {
                   4734:                                     $found{$mapstart{$item}} = substr($found{$mapstart{$item}},0,$scantronconf{$maplength{$item}});
                   4735:                                 }
                   4736:                             }
                   4737:                             $record .= $found{$mapstart{$item}};
                   4738:                         }
                   4739:                     }
                   4740:                     $output .= "$record\n";
                   4741:                 }
                   4742:                 close($fh);
                   4743:                 if ($output) {
                   4744:                     if (open(my $fh,'>',$fullpath)) {
                   4745:                         print $fh $output;
                   4746:                         close($fh);
                   4747:                     }
                   4748:                 }
                   4749:             }
                   4750:         }
                   4751:         return;
                   4752:     }
                   4753: }
                   4754: 
1.1403    raeburn  4755: sub letter_to_digits {
                   4756:     my %lettdig = (
                   4757:                     A => 1,
                   4758:                     B => 2,
                   4759:                     C => 3,
                   4760:                     D => 4,
                   4761:                     E => 5,
                   4762:                     F => 6,
                   4763:                     G => 7,
                   4764:                     H => 8,
                   4765:                     I => 9,
                   4766:                     J => 0,
                   4767:                   );
                   4768:     return %lettdig;
                   4769: }
                   4770: 
1.1401    raeburn  4771: sub get_scantron_config {
                   4772:     my ($which,$cdom) = @_;
                   4773:     my @lines = &get_scantronformat_file($cdom);
                   4774:     my %config;
                   4775:     #FIXME probably should move to XML it has already gotten a bit much now
                   4776:     foreach my $line (@lines) {
                   4777:         my ($name,$descrip)=split(/:/,$line);
                   4778:         if ($name ne $which ) { next; }
                   4779:         chomp($line);
                   4780:         my @config=split(/:/,$line);
                   4781:         $config{'name'}=$config[0];
                   4782:         $config{'description'}=$config[1];
                   4783:         $config{'CODElocation'}=$config[2];
                   4784:         $config{'CODEstart'}=$config[3];
                   4785:         $config{'CODElength'}=$config[4];
                   4786:         $config{'IDstart'}=$config[5];
                   4787:         $config{'IDlength'}=$config[6];
                   4788:         $config{'Qstart'}=$config[7];
                   4789:         $config{'Qlength'}=$config[8];
                   4790:         $config{'Qoff'}=$config[9];
                   4791:         $config{'Qon'}=$config[10];
                   4792:         $config{'PaperID'}=$config[11];
                   4793:         $config{'PaperIDlength'}=$config[12];
                   4794:         $config{'FirstName'}=$config[13];
                   4795:         $config{'FirstNamelength'}=$config[14];
                   4796:         $config{'LastName'}=$config[15];
                   4797:         $config{'LastNamelength'}=$config[16];
                   4798:         $config{'BubblesPerRow'}=$config[17];
                   4799:         last;
                   4800:     }
                   4801:     return %config;
                   4802: }
                   4803: 
                   4804: sub get_scantronformat_file {
                   4805:     my ($cdom) = @_;
                   4806:     if ($cdom eq '') {
                   4807:         $cdom= $env{'course.'.$env{'request.course.id'}.'.domain'};
                   4808:     }
                   4809:     my %domconfig = &get_dom('configuration',['scantron'],$cdom);
                   4810:     my $gottab = 0;
                   4811:     my @lines;
                   4812:     if (ref($domconfig{'scantron'}) eq 'HASH') {
                   4813:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
                   4814:             my $formatfile = &getfile($perlvar{'lonDocRoot'}.$domconfig{'scantron'}{'scantronformat'});
                   4815:             if ($formatfile ne '-1') {
                   4816:                 @lines = split("\n",$formatfile,-1);
                   4817:                 $gottab = 1;
                   4818:             }
                   4819:         }
                   4820:     }
                   4821:     if (!$gottab) {
                   4822:         my $confname = $cdom.'-domainconfig';
                   4823:         my $default = $perlvar{'lonDocRoot'}.'/res/'.$cdom.'/'.$confname.'/default.tab';
                   4824:         my $formatfile = &getfile($default);
                   4825:         if ($formatfile ne '-1') {
                   4826:             @lines = split("\n",$formatfile,-1);
                   4827:             $gottab = 1;
                   4828:         }
                   4829:     }
                   4830:     if (!$gottab) {
                   4831:         my @domains = &current_machine_domains();
                   4832:         if (grep(/^\Q$cdom\E$/,@domains)) {
                   4833:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/scantronformat.tab')) {
                   4834:                 @lines = <$fh>;
                   4835:                 close($fh);
1.1403    raeburn  4836:             }
1.1401    raeburn  4837:         } else {
                   4838:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/default_scantronformat.tab')) {
                   4839:                 @lines = <$fh>;
                   4840:                 close($fh);
                   4841:             }
                   4842:         }
                   4843:     }
                   4844:     return @lines;
                   4845: }
                   4846: 
1.493     albertel 4847: sub removeuploadedurl {
1.984     neumanie 4848:     my ($url)=@_;	
                   4849:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);    
1.613     albertel 4850:     return &removeuserfile($uname,$udom,$fname);
1.490     albertel 4851: }
                   4852: 
                   4853: sub removeuserfile {
                   4854:     my ($docuname,$docudom,$fname)=@_;
1.984     neumanie 4855:     my $home=&homeserver($docuname,$docudom);    
1.798     raeburn  4856:     my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
1.984     neumanie 4857:     if ($result eq 'ok') {	
1.798     raeburn  4858:         if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
                   4859:             my $metafile = $fname.'.meta';
                   4860:             my $metaresult = &removeuserfile($docuname,$docudom,$metafile); 
1.823     albertel 4861: 	    my $url = "/uploaded/$docudom/$docuname/$fname";
1.984     neumanie 4862:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];	   
1.821     raeburn  4863:             my $sqlresult = 
1.823     albertel 4864:                 &update_portfolio_table($docuname,$docudom,$file,
1.821     raeburn  4865:                                         'portfolio_metadata',$group,
                   4866:                                         'delete');
1.798     raeburn  4867:         }
                   4868:     }
                   4869:     return $result;
1.257     www      4870: }
1.15      www      4871: 
1.530     albertel 4872: sub mkdiruserfile {
                   4873:     my ($docuname,$docudom,$dir)=@_;
                   4874:     my $home=&homeserver($docuname,$docudom);
                   4875:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
                   4876: }
                   4877: 
1.531     albertel 4878: sub renameuserfile {
                   4879:     my ($docuname,$docudom,$old,$new)=@_;
                   4880:     my $home=&homeserver($docuname,$docudom);
1.798     raeburn  4881:     my $result = &reply("renameuserfile:$docudom:$docuname:".
                   4882:                         &escape("$old").':'.&escape("$new"),$home);
                   4883:     if ($result eq 'ok') {
                   4884:         if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
                   4885:             my $oldmeta = $old.'.meta';
                   4886:             my $newmeta = $new.'.meta';
                   4887:             my $metaresult = 
                   4888:                 &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
1.823     albertel 4889: 	    my $url = "/uploaded/$docudom/$docuname/$old";
                   4890:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821     raeburn  4891:             my $sqlresult = 
1.823     albertel 4892:                 &update_portfolio_table($docuname,$docudom,$file,
1.821     raeburn  4893:                                         'portfolio_metadata',$group,
                   4894:                                         'delete');
1.798     raeburn  4895:         }
                   4896:     }
                   4897:     return $result;
1.531     albertel 4898: }
                   4899: 
1.14      www      4900: # ------------------------------------------------------------------------- Log
                   4901: 
                   4902: sub log {
                   4903:     my ($dom,$nam,$hom,$what)=@_;
1.47      www      4904:     return critical("log:$dom:$nam:$what",$hom);
1.157     www      4905: }
                   4906: 
                   4907: # ------------------------------------------------------------------ Course Log
1.352     www      4908: #
                   4909: # This routine flushes several buffers of non-mission-critical nature
                   4910: #
1.157     www      4911: 
                   4912: sub flushcourselogs {
1.352     www      4913:     &logthis('Flushing log buffers');
                   4914: #
                   4915: # course logs
                   4916: # This is a log of all transactions in a course, which can be used
                   4917: # for data mining purposes
                   4918: #
                   4919: # It also collects the courseid database, which lists last transaction
                   4920: # times and course titles for all courseids
                   4921: #
                   4922:     my %courseidbuffer=();
1.921     raeburn  4923:     foreach my $crsid (keys(%courselogs)) {
1.352     www      4924:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1.188     www      4925: 		          &escape($courselogs{$crsid}),
                   4926: 		          $coursehombuf{$crsid}) eq 'ok') {
1.157     www      4927: 	    delete $courselogs{$crsid};
                   4928:         } else {
                   4929:             &logthis('Failed to flush log buffer for '.$crsid);
                   4930:             if (length($courselogs{$crsid})>40000) {
1.672     albertel 4931:                &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
1.157     www      4932:                         " exceeded maximum size, deleting.</font>");
                   4933:                delete $courselogs{$crsid};
                   4934:             }
1.352     www      4935:         }
1.920     raeburn  4936:         $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
1.936     raeburn  4937:             'description' => $coursedescrbuf{$crsid},
                   4938:             'inst_code'    => $courseinstcodebuf{$crsid},
                   4939:             'type'        => $coursetypebuf{$crsid},
                   4940:             'owner'       => $courseownerbuf{$crsid},
1.920     raeburn  4941:         };
1.191     harris41 4942:     }
1.352     www      4943: #
                   4944: # Write course id database (reverse lookup) to homeserver of courses 
                   4945: # Is used in pickcourse
                   4946: #
1.840     albertel 4947:     foreach my $crs_home (keys(%courseidbuffer)) {
1.918     raeburn  4948:         my $response = &courseidput(&host_domain($crs_home),
1.921     raeburn  4949:                                     $courseidbuffer{$crs_home},
                   4950:                                     $crs_home,'timeonly');
1.352     www      4951:     }
                   4952: #
                   4953: # File accesses
                   4954: # Writes to the dynamic metadata of resources to get hit counts, etc.
                   4955: #
1.449     matthew  4956:     foreach my $entry (keys(%accesshash)) {
1.458     matthew  4957:         if ($entry =~ /___count$/) {
                   4958:             my ($dom,$name);
1.807     albertel 4959:             ($dom,$name,undef)=
1.811     albertel 4960: 		($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
1.458     matthew  4961:             if (! defined($dom) || $dom eq '' || 
                   4962:                 ! defined($name) || $name eq '') {
1.620     albertel 4963:                 my $cid = $env{'request.course.id'};
                   4964:                 $dom  = $env{'request.'.$cid.'.domain'};
                   4965:                 $name = $env{'request.'.$cid.'.num'};
1.458     matthew  4966:             }
1.450     matthew  4967:             my $value = $accesshash{$entry};
                   4968:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
                   4969:             my %temphash=($url => $value);
1.449     matthew  4970:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
                   4971:             if ($result eq 'ok') {
                   4972:                 delete $accesshash{$entry};
                   4973:             }
                   4974:         } else {
1.811     albertel 4975:             my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
1.1159    www      4976:             if (($dom eq 'uploaded') || ($dom eq 'adm')) { next; }
1.450     matthew  4977:             my %temphash=($entry => $accesshash{$entry});
1.449     matthew  4978:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
                   4979:                 delete $accesshash{$entry};
                   4980:             }
1.185     www      4981:         }
1.191     harris41 4982:     }
1.352     www      4983: #
                   4984: # Roles
                   4985: # Reverse lookup of user roles for course faculty/staff and co-authorship
                   4986: #
1.800     albertel 4987:     foreach my $entry (keys(%userrolehash)) {
1.351     www      4988:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1.349     www      4989: 	    split(/\:/,$entry);
                   4990:         if (&Apache::lonnet::put('nohist_userroles',
1.351     www      4991:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1.349     www      4992:                 $rudom,$runame) eq 'ok') {
                   4993: 	    delete $userrolehash{$entry};
                   4994:         }
                   4995:     }
1.662     raeburn  4996: #
1.1334    raeburn  4997: # Reverse lookup of domain roles (dc, ad, li, sc, dh, da, au)
1.662     raeburn  4998: #
                   4999:     my %domrolebuffer = ();
1.1000    raeburn  5000:     foreach my $entry (keys(%domainrolehash)) {
1.901     albertel 5001:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
1.662     raeburn  5002:         if ($domrolebuffer{$rudom}) {
                   5003:             $domrolebuffer{$rudom}.='&'.&escape($entry).
                   5004:                       '='.&escape($domainrolehash{$entry});
                   5005:         } else {
                   5006:             $domrolebuffer{$rudom}.=&escape($entry).
                   5007:                       '='.&escape($domainrolehash{$entry});
                   5008:         }
                   5009:         delete $domainrolehash{$entry};
                   5010:     }
                   5011:     foreach my $dom (keys(%domrolebuffer)) {
1.1324    raeburn  5012: 	my %servers;
                   5013: 	if (defined(&domain($dom,'primary'))) {
                   5014: 	    my $primary=&domain($dom,'primary');
                   5015: 	    my $hostname=&hostname($primary);
                   5016: 	    $servers{$primary} = $hostname;
                   5017: 	} else { 
                   5018: 	    %servers = &get_servers($dom,'library');
                   5019: 	}
1.841     albertel 5020: 	foreach my $tryserver (keys(%servers)) {
1.1324    raeburn  5021: 	    if (&reply('domroleput:'.$dom.':'.
                   5022: 		       $domrolebuffer{$dom},$tryserver) eq 'ok') {
                   5023: 		last;
                   5024: 	    } else {  
1.841     albertel 5025: 		&logthis('Put of domain roles failed for '.$dom.' and  '.$tryserver);
                   5026: 	    }
1.662     raeburn  5027:         }
                   5028:     }
1.186     www      5029:     $dumpcount++;
1.157     www      5030: }
                   5031: 
                   5032: sub courselog {
                   5033:     my $what=shift;
1.158     www      5034:     $what=time.':'.$what;
1.620     albertel 5035:     unless ($env{'request.course.id'}) { return ''; }
                   5036:     $coursedombuf{$env{'request.course.id'}}=
                   5037:        $env{'course.'.$env{'request.course.id'}.'.domain'};
                   5038:     $coursenumbuf{$env{'request.course.id'}}=
                   5039:        $env{'course.'.$env{'request.course.id'}.'.num'};
                   5040:     $coursehombuf{$env{'request.course.id'}}=
                   5041:        $env{'course.'.$env{'request.course.id'}.'.home'};
                   5042:     $coursedescrbuf{$env{'request.course.id'}}=
                   5043:        $env{'course.'.$env{'request.course.id'}.'.description'};
                   5044:     $courseinstcodebuf{$env{'request.course.id'}}=
                   5045:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
                   5046:     $courseownerbuf{$env{'request.course.id'}}=
                   5047:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
1.741     raeburn  5048:     $coursetypebuf{$env{'request.course.id'}}=
                   5049:        $env{'course.'.$env{'request.course.id'}.'.type'};
1.620     albertel 5050:     if (defined $courselogs{$env{'request.course.id'}}) {
                   5051: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
1.157     www      5052:     } else {
1.620     albertel 5053: 	$courselogs{$env{'request.course.id'}}.=$what;
1.157     www      5054:     }
1.620     albertel 5055:     if (length($courselogs{$env{'request.course.id'}})>4048) {
1.157     www      5056: 	&flushcourselogs();
                   5057:     }
1.158     www      5058: }
                   5059: 
                   5060: sub courseacclog {
                   5061:     my $fnsymb=shift;
1.620     albertel 5062:     unless ($env{'request.course.id'}) { return ''; }
                   5063:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
1.1144    www      5064:     if ($fnsymb=~/$LONCAPA::assess_re/) {
1.187     www      5065:         $what.=':POST';
1.583     matthew  5066:         # FIXME: Probably ought to escape things....
1.800     albertel 5067: 	foreach my $key (keys(%env)) {
                   5068:             if ($key=~/^form\.(.*)/) {
1.975     raeburn  5069:                 my $formitem = $1;
                   5070:                 if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
                   5071:                     $what.=':'.$formitem.'='.$env{$key};
                   5072:                 } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
1.1432    raeburn  5073:                     if ($formitem eq 'proctorpassword') {
1.1433    raeburn  5074:                         $what.=':'.$formitem.'=' . '*' x length($env{$key});
1.1432    raeburn  5075:                     } else {
                   5076:                         $what.=':'.$formitem.'='.$env{$key};
                   5077:                     }
1.975     raeburn  5078:                 }
1.158     www      5079:             }
1.191     harris41 5080:         }
1.583     matthew  5081:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
                   5082:         # FIXME: We should not be depending on a form parameter that someone
                   5083:         # editing lonsearchcat.pm might change in the future.
1.620     albertel 5084:         if ($env{'form.phase'} eq 'course_search') {
1.583     matthew  5085:             $what.= ':POST';
                   5086:             # FIXME: Probably ought to escape things....
                   5087:             foreach my $element ('courseexp','crsfulltext','crsrelated',
                   5088:                                  'crsdiscuss') {
1.620     albertel 5089:                 $what.=':'.$element.'='.$env{'form.'.$element};
1.583     matthew  5090:             }
                   5091:         }
1.158     www      5092:     }
                   5093:     &courselog($what);
1.149     www      5094: }
                   5095: 
1.185     www      5096: sub countacc {
                   5097:     my $url=&declutter(shift);
1.458     matthew  5098:     return if (! defined($url) || $url eq '');
1.620     albertel 5099:     unless ($env{'request.course.id'}) { return ''; }
1.1158    www      5100: #
                   5101: # Mark that this url was used in this course
                   5102: #
1.620     albertel 5103:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
1.1158    www      5104: #
                   5105: # Increase the access count for this resource in this child process
                   5106: #
1.281     www      5107:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.450     matthew  5108:     $accesshash{$key}++;
1.185     www      5109: }
1.349     www      5110: 
1.361     www      5111: sub linklog {
                   5112:     my ($from,$to)=@_;
                   5113:     $from=&declutter($from);
                   5114:     $to=&declutter($to);
                   5115:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
                   5116:     $accesshash{$to.'___'.$from.'___goto'}=1;
                   5117: }
1.1160    www      5118: 
                   5119: sub statslog {
                   5120:     my ($symb,$part,$users,$av_attempts,$degdiff)=@_;
                   5121:     if ($users<2) { return; }
                   5122:     my %dynstore=&LONCAPA::lonmetadata::dynamic_metadata_storage({
                   5123:             'course'       => $env{'request.course.id'},
                   5124:             'sections'     => '"all"',
                   5125:             'num_students' => $users,
                   5126:             'part'         => $part,
                   5127:             'symb'         => $symb,
                   5128:             'mean_tries'   => $av_attempts,
                   5129:             'deg_of_diff'  => $degdiff});
                   5130:     foreach my $key (keys(%dynstore)) {
                   5131:         $accesshash{$key}=$dynstore{$key};
                   5132:     }
                   5133: }
1.361     www      5134:   
1.349     www      5135: sub userrolelog {
                   5136:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
1.1169    droeschl 5137:     if ( $trole =~ /^(ca|aa|in|cc|ep|cr|ta|co)/ ) {
1.350     www      5138:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   5139:        $userrolehash
                   5140:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1.349     www      5141:                     =$tend.':'.$tstart;
1.662     raeburn  5142:     }
1.1169    droeschl 5143:     if ($env{'request.role'} =~ /dc\./ && $trole =~ /^(au|in|cc|ep|cr|ta|co)/) {
1.898     albertel 5144:        $userrolehash
                   5145:          {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
                   5146:                     =$tend.':'.$tstart;
                   5147:     }
1.1334    raeburn  5148:     if ($trole =~ /^(dc|ad|li|au|dg|sc|dh|da)/ ) {
1.662     raeburn  5149:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   5150:        $domainrolehash
                   5151:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
                   5152:                     = $tend.':'.$tstart;
                   5153:     }
1.351     www      5154: }
                   5155: 
1.957     raeburn  5156: sub courserolelog {
                   5157:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
1.1184    raeburn  5158:     if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
                   5159:         my $cdom = $1;
                   5160:         my $cnum = $2;
                   5161:         my $sec = $3;
                   5162:         my $namespace = 'rolelog';
                   5163:         my %storehash = (
                   5164:                            role    => $trole,
                   5165:                            start   => $tstart,
                   5166:                            end     => $tend,
                   5167:                            selfenroll => $selfenroll,
                   5168:                            context    => $context,
                   5169:                         );
                   5170:         if ($trole eq 'gr') {
                   5171:             $namespace = 'groupslog';
                   5172:             $storehash{'group'} = $sec;
                   5173:         } else {
                   5174:             $storehash{'section'} = $sec;
                   5175:         }
1.1188    raeburn  5176:         &write_log('course',$namespace,\%storehash,$delflag,$username,
                   5177:                    $domain,$cnum,$cdom);
1.1184    raeburn  5178:         if (($trole ne 'st') || ($sec ne '')) {
                   5179:             &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
1.957     raeburn  5180:         }
                   5181:     }
                   5182:     return;
                   5183: }
                   5184: 
1.1184    raeburn  5185: sub domainrolelog {
                   5186:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
                   5187:     if ($area =~ m{^/($match_domain)/$}) {
                   5188:         my $cdom = $1;
                   5189:         my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
                   5190:         my $namespace = 'rolelog';
                   5191:         my %storehash = (
                   5192:                            role    => $trole,
                   5193:                            start   => $tstart,
                   5194:                            end     => $tend,
                   5195:                            context => $context,
                   5196:                         );
1.1188    raeburn  5197:         &write_log('domain',$namespace,\%storehash,$delflag,$username,
                   5198:                    $domain,$domconfiguser,$cdom);
1.1184    raeburn  5199:     }
                   5200:     return;
                   5201: 
                   5202: }
                   5203: 
                   5204: sub coauthorrolelog {
                   5205:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
                   5206:     if ($area =~ m{^/($match_domain)/($match_username)$}) {
                   5207:         my $audom = $1;
                   5208:         my $auname = $2;
                   5209:         my $namespace = 'rolelog';
                   5210:         my %storehash = (
                   5211:                            role    => $trole,
                   5212:                            start   => $tstart,
                   5213:                            end     => $tend,
                   5214:                            context => $context,
                   5215:                         );
1.1188    raeburn  5216:         &write_log('author',$namespace,\%storehash,$delflag,$username,
                   5217:                    $domain,$auname,$audom);
1.1184    raeburn  5218:     }
                   5219:     return;
                   5220: }
                   5221: 
1.351     www      5222: sub get_course_adv_roles {
1.948     raeburn  5223:     my ($cid,$codes) = @_;
1.620     albertel 5224:     $cid=$env{'request.course.id'} unless (defined($cid));
1.351     www      5225:     my %coursehash=&coursedescription($cid);
1.988     raeburn  5226:     my $crstype = &Apache::loncommon::course_type($cid);
1.470     www      5227:     my %nothide=();
1.800     albertel 5228:     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
1.937     raeburn  5229:         if ($user !~ /:/) {
                   5230: 	    $nothide{join(':',split(/[\@]/,$user))}=1;
                   5231:         } else {
                   5232:             $nothide{$user}=1;
                   5233:         }
1.470     www      5234:     }
1.1219    raeburn  5235:     my @possdoms = ($coursehash{'domain'});
                   5236:     if ($coursehash{'checkforpriv'}) {
                   5237:         push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
                   5238:     }
1.351     www      5239:     my %returnhash=();
                   5240:     my %dumphash=
                   5241:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
                   5242:     my $now=time;
1.997     raeburn  5243:     my %privileged;
1.1000    raeburn  5244:     foreach my $entry (keys(%dumphash)) {
1.800     albertel 5245: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
1.351     www      5246:         if (($tstart) && ($tstart<0)) { next; }
                   5247:         if (($tend) && ($tend<$now)) { next; }
                   5248:         if (($tstart) && ($now<$tstart)) { next; }
1.800     albertel 5249:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
1.576     albertel 5250: 	if ($username eq '' || $domain eq '') { next; }
1.1219    raeburn  5251:         if ((&privileged($username,$domain,\@possdoms)) &&
1.997     raeburn  5252:             (!$nothide{$username.':'.$domain})) { next; }
1.656     albertel 5253: 	if ($role eq 'cr') { next; }
1.948     raeburn  5254:         if ($codes) {
                   5255:             if ($section) { $role .= ':'.$section; }
                   5256:             if ($returnhash{$role}) {
                   5257:                 $returnhash{$role}.=','.$username.':'.$domain;
                   5258:             } else {
                   5259:                 $returnhash{$role}=$username.':'.$domain;
                   5260:             }
1.351     www      5261:         } else {
1.988     raeburn  5262:             my $key=&plaintext($role,$crstype);
1.973     bisitz   5263:             if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
1.948     raeburn  5264:             if ($returnhash{$key}) {
                   5265: 	        $returnhash{$key}.=','.$username.':'.$domain;
                   5266:             } else {
                   5267:                 $returnhash{$key}=$username.':'.$domain;
                   5268:             }
1.351     www      5269:         }
1.948     raeburn  5270:     }
1.400     www      5271:     return %returnhash;
                   5272: }
                   5273: 
                   5274: sub get_my_roles {
1.937     raeburn  5275:     my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
1.620     albertel 5276:     unless (defined($uname)) { $uname=$env{'user.name'}; }
                   5277:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
1.937     raeburn  5278:     my (%dumphash,%nothide);
1.1086    raeburn  5279:     if ($context eq 'userroles') {
1.1166    raeburn  5280:         %dumphash = &dump('roles',$udom,$uname);
1.858     raeburn  5281:     } else {
1.1219    raeburn  5282:         %dumphash = &dump('nohist_userroles',$udom,$uname);
1.937     raeburn  5283:         if ($hidepriv) {
                   5284:             my %coursehash=&coursedescription($udom.'_'.$uname);
                   5285:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
                   5286:                 if ($user !~ /:/) {
                   5287:                     $nothide{join(':',split(/[\@]/,$user))} = 1;
                   5288:                 } else {
                   5289:                     $nothide{$user} = 1;
                   5290:                 }
                   5291:             }
                   5292:         }
1.858     raeburn  5293:     }
1.400     www      5294:     my %returnhash=();
                   5295:     my $now=time;
1.999     raeburn  5296:     my %privileged;
1.800     albertel 5297:     foreach my $entry (keys(%dumphash)) {
1.867     raeburn  5298:         my ($role,$tend,$tstart);
                   5299:         if ($context eq 'userroles') {
1.1149    raeburn  5300:             next if ($entry =~ /^rolesdef/);
1.867     raeburn  5301: 	    ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
                   5302:         } else {
                   5303:             ($tend,$tstart)=split(/\:/,$dumphash{$entry});
                   5304:         }
1.400     www      5305:         if (($tstart) && ($tstart<0)) { next; }
1.832     raeburn  5306:         my $status = 'active';
1.939     raeburn  5307:         if (($tend) && ($tend<=$now)) {
1.832     raeburn  5308:             $status = 'previous';
                   5309:         } 
                   5310:         if (($tstart) && ($now<$tstart)) {
                   5311:             $status = 'future';
                   5312:         }
                   5313:         if (ref($types) eq 'ARRAY') {
                   5314:             if (!grep(/^\Q$status\E$/,@{$types})) {
                   5315:                 next;
                   5316:             } 
                   5317:         } else {
                   5318:             if ($status ne 'active') {
                   5319:                 next;
                   5320:             }
                   5321:         }
1.867     raeburn  5322:         my ($rolecode,$username,$domain,$section,$area);
                   5323:         if ($context eq 'userroles') {
1.1186    raeburn  5324:             ($area,$rolecode) = ($entry =~ /^(.+)_([^_]+)$/);
1.867     raeburn  5325:             (undef,$domain,$username,$section) = split(/\//,$area);
                   5326:         } else {
                   5327:             ($role,$username,$domain,$section) = split(/\:/,$entry);
                   5328:         }
1.832     raeburn  5329:         if (ref($roledoms) eq 'ARRAY') {
                   5330:             if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
                   5331:                 next;
                   5332:             }
                   5333:         }
                   5334:         if (ref($roles) eq 'ARRAY') {
                   5335:             if (!grep(/^\Q$role\E$/,@{$roles})) {
1.922     raeburn  5336:                 if ($role =~ /^cr\//) {
                   5337:                     if (!grep(/^cr$/,@{$roles})) {
                   5338:                         next;
                   5339:                     }
1.1104    raeburn  5340:                 } elsif ($role =~ /^gr\//) {
                   5341:                     if (!grep(/^gr$/,@{$roles})) {
                   5342:                         next;
                   5343:                     }
1.922     raeburn  5344:                 } else {
                   5345:                     next;
                   5346:                 }
1.832     raeburn  5347:             }
1.867     raeburn  5348:         }
1.937     raeburn  5349:         if ($hidepriv) {
1.1219    raeburn  5350:             my @privroles = ('dc','su');
1.999     raeburn  5351:             if ($context eq 'userroles') {
1.1219    raeburn  5352:                 next if (grep(/^\Q$role\E$/,@privroles));
1.999     raeburn  5353:             } else {
1.1219    raeburn  5354:                 my $possdoms = [$domain];
                   5355:                 if (ref($roledoms) eq 'ARRAY') {
                   5356:                    push(@{$possdoms},@{$roledoms}); 
1.999     raeburn  5357:                 }
1.1219    raeburn  5358:                 if (&privileged($username,$domain,$possdoms,\@privroles)) {
1.999     raeburn  5359:                     if (!$nothide{$username.':'.$domain}) {
                   5360:                         next;
                   5361:                     }
                   5362:                 }
1.937     raeburn  5363:             }
                   5364:         }
1.933     raeburn  5365:         if ($withsec) {
                   5366:             $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
                   5367:                 $tstart.':'.$tend;
                   5368:         } else {
                   5369:             $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
                   5370:         }
1.832     raeburn  5371:     }
1.373     www      5372:     return %returnhash;
1.399     www      5373: }
                   5374: 
1.1333    raeburn  5375: sub get_all_adhocroles {
                   5376:     my ($dom) = @_;
                   5377:     my @roles_by_num = ();
                   5378:     my %domdefaults = &get_domain_defaults($dom);
                   5379:     my (%description,%access_in_dom,%access_info);
                   5380:     if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
                   5381:         my $count = 0;
                   5382:         my %domcurrent = %{$domdefaults{'adhocroles'}};
                   5383:         my %ordered;
                   5384:         foreach my $role (sort(keys(%domcurrent))) {
                   5385:             my ($order,$desc,$access_in_dom);
                   5386:             if (ref($domcurrent{$role}) eq 'HASH') {
                   5387:                 $order = $domcurrent{$role}{'order'};
                   5388:                 $desc = $domcurrent{$role}{'desc'};
                   5389:                 $access_in_dom{$role} = $domcurrent{$role}{'access'};
                   5390:                 $access_info{$role} = $domcurrent{$role}{$access_in_dom{$role}};
                   5391:             }
                   5392:             if ($order eq '') {
                   5393:                 $order = $count;
                   5394:             }
                   5395:             $ordered{$order} = $role;
                   5396:             if ($desc ne '') {
                   5397:                 $description{$role} = $desc;
                   5398:             } else {
                   5399:                 $description{$role}= $role;
                   5400:             }
                   5401:             $count++;
                   5402:         }
                   5403:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
                   5404:             push(@roles_by_num,$ordered{$item});
                   5405:         }
                   5406:     }
                   5407:     return (\@roles_by_num,\%description,\%access_in_dom,\%access_info);
                   5408: }
                   5409: 
1.1332    raeburn  5410: sub get_my_adhocroles {
1.1333    raeburn  5411:     my ($cid,$checkreg) = @_;
                   5412:     my ($cdom,$cnum,%info,@possroles,$description,$roles_by_num);
                   5413:     if ($env{'request.course.id'} eq $cid) {
                   5414:         $cdom = $env{'course.'.$cid.'.domain'};
                   5415:         $cnum = $env{'course.'.$cid.'.num'};
                   5416:         $info{'internal.coursecode'} = $env{'course.'.$cid.'.internal.coursecode'};
                   5417:     } elsif ($cid =~ /^($match_domain)_($match_courseid)$/) {
                   5418:         $cdom = $1;
                   5419:         $cnum = $2;
                   5420:         %info = &Apache::lonnet::get('environment',['internal.coursecode'],
                   5421:                                      $cdom,$cnum);
                   5422:     }
                   5423:     if (($info{'internal.coursecode'} ne '') && ($checkreg)) {
                   5424:         my $user = $env{'user.name'}.':'.$env{'user.domain'};
                   5425:         my %rosterhash = &get('classlist',[$user],$cdom,$cnum);
                   5426:         if ($rosterhash{$user} ne '') {
                   5427:             my $type = (split(/:/,$rosterhash{$user}))[5];
                   5428:             return ([],{}) if ($type eq 'auto');
                   5429:         }
                   5430:     }
                   5431:     if (($cdom ne '') && ($cnum ne ''))  {
1.1334    raeburn  5432:         if (($env{"user.role.dh./$cdom/"}) || ($env{"user.role.da./$cdom/"})) {
1.1332    raeburn  5433:             my $then=$env{'user.login.time'};
                   5434:             my $update=$env{'user.update.time'};
1.1335    raeburn  5435:             if (!$update) {
                   5436:                 $update = $then;
                   5437:             }
                   5438:             my @liveroles;
1.1334    raeburn  5439:             foreach my $role ('dh','da') {
                   5440:                 if ($env{"user.role.$role./$cdom/"}) {
1.1335    raeburn  5441:                     my ($tstart,$tend)=split(/\./,$env{"user.role.$role./$cdom/"});
1.1334    raeburn  5442:                     my $limit = $update;
                   5443:                     if ($env{'request.role'} eq "$role./$cdom/") {
                   5444:                         $limit = $then;
                   5445:                     }
1.1335    raeburn  5446:                     my $activerole = 1;
                   5447:                     if ($tstart && $tstart>$limit) { $activerole = 0; }
                   5448:                     if ($tend   && $tend  <$limit) { $activerole = 0; }
                   5449:                     if ($activerole) {
                   5450:                         push(@liveroles,$role);
                   5451:                     }
1.1334    raeburn  5452:                 }
1.1332    raeburn  5453:             }
1.1335    raeburn  5454:             if (@liveroles) {
1.1332    raeburn  5455:                 if (&homeserver($cnum,$cdom) ne 'no_host') {
1.1333    raeburn  5456:                     my ($accessref,$accessinfo,%access_in_dom);
                   5457:                     ($roles_by_num,$description,$accessref,$accessinfo) = &get_all_adhocroles($cdom);
                   5458:                     if (ref($roles_by_num) eq 'ARRAY') {
                   5459:                         if (@{$roles_by_num}) {
1.1332    raeburn  5460:                             my %settings;
                   5461:                             if ($env{'request.course.id'} eq $cid) {
                   5462:                                 foreach my $envkey (keys(%env)) {
                   5463:                                     if ($envkey =~ /^\Qcourse.$cid.\E(internal\.adhoc.+)$/) {
                   5464:                                         $settings{$1} = $env{$envkey};
                   5465:                                     }
                   5466:                                 }
                   5467:                             } else {
                   5468:                                 %settings = &dump('environment',$cdom,$cnum,'internal\.adhoc');
                   5469:                             }
                   5470:                             my %setincrs;
                   5471:                             if ($settings{'internal.adhocaccess'}) {
                   5472:                                 map { $setincrs{$_} = 1; } split(/,/,$settings{'internal.adhocaccess'});
                   5473:                             }
                   5474:                             my @statuses;
                   5475:                             if ($env{'environment.inststatus'}) {
                   5476:                                 @statuses = split(/,/,$env{'environment.inststatus'});
                   5477:                             }
                   5478:                             my $user = $env{'user.name'}.':'.$env{'user.domain'};
1.1333    raeburn  5479:                             if (ref($accessref) eq 'HASH') {
                   5480:                                 %access_in_dom = %{$accessref};
                   5481:                             }
                   5482:                             foreach my $role (@{$roles_by_num}) {
1.1332    raeburn  5483:                                 my ($curraccess,@okstatus,@personnel);
                   5484:                                 if ($setincrs{$role}) {
                   5485:                                     ($curraccess,my $rest) = split(/=/,$settings{'internal.adhoc.'.$role});
                   5486:                                     if ($curraccess eq 'status') {
                   5487:                                         @okstatus = split(/\&/,$rest);
                   5488:                                     } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
                   5489:                                         @personnel = split(/\&/,$rest);
                   5490:                                     }
                   5491:                                 } else {
                   5492:                                     $curraccess = $access_in_dom{$role};
1.1333    raeburn  5493:                                     if (ref($accessinfo) eq 'HASH') {
                   5494:                                         if ($curraccess eq 'status') {
                   5495:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
                   5496:                                                 @okstatus = @{$accessinfo->{$role}};
                   5497:                                             }
                   5498:                                         } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
                   5499:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
                   5500:                                                 @personnel = @{$accessinfo->{$role}};
                   5501:                                             }
1.1332    raeburn  5502:                                         }
                   5503:                                     }
                   5504:                                 }
                   5505:                                 if ($curraccess eq 'none') {
                   5506:                                     next;
                   5507:                                 } elsif ($curraccess eq 'all') {
                   5508:                                     push(@possroles,$role);
1.1336    raeburn  5509:                                 } elsif ($curraccess eq 'dh') {
1.1335    raeburn  5510:                                     if (grep(/^dh$/,@liveroles)) {
                   5511:                                         push(@possroles,$role);
                   5512:                                     } else {
                   5513:                                         next;
                   5514:                                     }
1.1336    raeburn  5515:                                 } elsif ($curraccess eq 'da') {
1.1335    raeburn  5516:                                     if (grep(/^da$/,@liveroles)) {
                   5517:                                         push(@possroles,$role);
                   5518:                                     } else {
                   5519:                                         next;
                   5520:                                     }
1.1332    raeburn  5521:                                 } elsif ($curraccess eq 'status') {
                   5522:                                     if (@okstatus) {
                   5523:                                         if (!@statuses) {
                   5524:                                             if (grep(/^default$/,@okstatus)) {
                   5525:                                                 push(@possroles,$role);
                   5526:                                             }
                   5527:                                         } else {
                   5528:                                             foreach my $status (@okstatus) {
                   5529:                                                 if (grep(/^\Q$status\E$/,@statuses)) {
                   5530:                                                     push(@possroles,$role);
                   5531:                                                     last;
                   5532:                                                 }
                   5533:                                             }
                   5534:                                         }
                   5535:                                     }
                   5536:                                 } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
                   5537:                                     if (grep(/^\Q$user\E$/,@personnel)) {
                   5538:                                         if ($curraccess eq 'exc') {
                   5539:                                             push(@possroles,$role);
                   5540:                                         }
                   5541:                                     } elsif ($curraccess eq 'inc') {
                   5542:                                         push(@possroles,$role);
                   5543:                                     }
                   5544:                                 }
                   5545:                             }
                   5546:                         }
                   5547:                     }
                   5548:                 }
                   5549:             }
                   5550:         }
                   5551:     }
1.1333    raeburn  5552:     unless (ref($description) eq 'HASH') {
                   5553:         if (ref($roles_by_num) eq 'ARRAY') {
                   5554:             my %desc;
                   5555:             map { $desc{$_} = $_; } (@{$roles_by_num});
                   5556:             $description = \%desc;
                   5557:         } else {
                   5558:             $description = {};
                   5559:         }
                   5560:     }
                   5561:     return (\@possroles,$description);
1.1332    raeburn  5562: }
                   5563: 
1.399     www      5564: # ----------------------------------------------------- Frontpage Announcements
                   5565: #
                   5566: #
                   5567: 
                   5568: sub postannounce {
                   5569:     my ($server,$text)=@_;
1.844     albertel 5570:     unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
1.399     www      5571:     unless ($text=~/\w/) { $text=''; }
                   5572:     return &reply('setannounce:'.&escape($text),$server);
                   5573: }
                   5574: 
                   5575: sub getannounce {
1.448     albertel 5576: 
1.1359    raeburn  5577:     if (open(my $fh,"<",$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1.399     www      5578: 	my $announcement='';
1.800     albertel 5579: 	while (my $line = <$fh>) { $announcement .= $line; }
1.448     albertel 5580: 	close($fh);
1.399     www      5581: 	if ($announcement=~/\w/) { 
                   5582: 	    return 
                   5583:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1.518     albertel 5584:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
1.399     www      5585: 	} else {
                   5586: 	    return '';
                   5587: 	}
                   5588:     } else {
                   5589: 	return '';
                   5590:     }
1.351     www      5591: }
1.353     www      5592: 
                   5593: # ---------------------------------------------------------- Course ID routines
                   5594: # Deal with domain's nohist_courseid.db files
                   5595: #
                   5596: 
                   5597: sub courseidput {
1.921     raeburn  5598:     my ($domain,$storehash,$coursehome,$caller) = @_;
1.1054    raeburn  5599:     return unless (ref($storehash) eq 'HASH');
1.921     raeburn  5600:     my $outcome;
                   5601:     if ($caller eq 'timeonly') {
                   5602:         my $cids = '';
                   5603:         foreach my $item (keys(%$storehash)) {
                   5604:             $cids.=&escape($item).'&';
                   5605:         }
                   5606:         $cids=~s/\&$//;
                   5607:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
                   5608:                           $coursehome);       
                   5609:     } else {
                   5610:         my $items = '';
                   5611:         foreach my $item (keys(%$storehash)) {
                   5612:             $items.= &escape($item).'='.
                   5613:                      &freeze_escape($$storehash{$item}).'&';
                   5614:         }
                   5615:         $items=~s/\&$//;
                   5616:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
                   5617:                           $coursehome);
1.918     raeburn  5618:     }
                   5619:     if ($outcome eq 'unknown_cmd') {
                   5620:         my $what;
                   5621:         foreach my $cid (keys(%$storehash)) {
                   5622:             $what .= &escape($cid).'=';
1.921     raeburn  5623:             foreach my $item ('description','inst_code','owner','type') {
1.936     raeburn  5624:                 $what .= &escape($storehash->{$cid}{$item}).':';
1.918     raeburn  5625:             }
                   5626:             $what =~ s/\:$/&/;
                   5627:         }
                   5628:         $what =~ s/\&$//;  
                   5629:         return &reply('courseidput:'.$domain.':'.$what,$coursehome);
                   5630:     } else {
                   5631:         return $outcome;
                   5632:     }
1.353     www      5633: }
                   5634: 
                   5635: sub courseiddump {
1.921     raeburn  5636:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
1.947     raeburn  5637:         $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
1.1029    raeburn  5638:         $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
1.1247    raeburn  5639:         $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner,
1.1287    raeburn  5640:         $hasuniquecode,$reqcrsdom,$reqinstcode)=@_;
1.918     raeburn  5641:     my $as_hash = 1;
                   5642:     my %returnhash;
                   5643:     if (!$domfilter) { $domfilter=''; }
1.845     albertel 5644:     my %libserv = &all_library();
                   5645:     foreach my $tryserver (keys(%libserv)) {
                   5646:         if ( (  $hostidflag == 1 
                   5647: 	        && grep(/^\Q$tryserver\E$/,@{$hostidref}) ) 
                   5648: 	     || (!defined($hostidflag)) ) {
                   5649: 
1.918     raeburn  5650: 	    if (($domfilter eq '') ||
                   5651: 		(&host_domain($tryserver) eq $domfilter)) {
1.1180    droeschl 5652:                 my $rep;
                   5653:                 if (grep { $_ eq $tryserver } current_machine_ids()) {
                   5654:                     $rep = LONCAPA::Lond::dump_course_id_handler(
                   5655:                         join(":", (&host_domain($tryserver), $sincefilter, 
                   5656:                                 &escape($descfilter), &escape($instcodefilter), 
                   5657:                                 &escape($ownerfilter), &escape($coursefilter),
                   5658:                                 &escape($typefilter), &escape($regexp_ok), 
                   5659:                                 $as_hash, &escape($selfenrollonly), 
                   5660:                                 &escape($catfilter), $showhidden, $caller, 
                   5661:                                 &escape($cloner), &escape($cc_clone), $cloneonly, 
                   5662:                                 &escape($createdbefore), &escape($createdafter), 
1.1287    raeburn  5663:                                 &escape($creationcontext),$domcloner,$hasuniquecode,
                   5664:                                 $reqcrsdom,&escape($reqinstcode))));
1.1180    droeschl 5665:                 } else {
                   5666:                     $rep = &reply('courseiddump:'.&host_domain($tryserver).':'.
                   5667:                              $sincefilter.':'.&escape($descfilter).':'.
                   5668:                              &escape($instcodefilter).':'.&escape($ownerfilter).
                   5669:                              ':'.&escape($coursefilter).':'.&escape($typefilter).
                   5670:                              ':'.&escape($regexp_ok).':'.$as_hash.':'.
                   5671:                              &escape($selfenrollonly).':'.&escape($catfilter).':'.
                   5672:                              $showhidden.':'.$caller.':'.&escape($cloner).':'.
                   5673:                              &escape($cc_clone).':'.$cloneonly.':'.
                   5674:                              &escape($createdbefore).':'.&escape($createdafter).':'.
1.1287    raeburn  5675:                              &escape($creationcontext).':'.$domcloner.':'.$hasuniquecode.
                   5676:                              ':'.$reqcrsdom.':'.&escape($reqinstcode),$tryserver);
1.1180    droeschl 5677:                 }
                   5678:                      
1.918     raeburn  5679:                 my @pairs=split(/\&/,$rep);
                   5680:                 foreach my $item (@pairs) {
                   5681:                     my ($key,$value)=split(/\=/,$item,2);
                   5682:                     $key = &unescape($key);
                   5683:                     next if ($key =~ /^error: 2 /);
                   5684:                     my $result = &thaw_unescape($value);
                   5685:                     if (ref($result) eq 'HASH') {
                   5686:                         $returnhash{$key}=$result;
                   5687:                     } else {
1.921     raeburn  5688:                         my @responses = split(/:/,$value);
                   5689:                         my @items = ('description','inst_code','owner','type');
1.918     raeburn  5690:                         for (my $i=0; $i<@responses; $i++) {
1.921     raeburn  5691:                             $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
1.918     raeburn  5692:                         }
1.1008    raeburn  5693:                     }
1.353     www      5694:                 }
                   5695:             }
                   5696:         }
                   5697:     }
                   5698:     return %returnhash;
                   5699: }
                   5700: 
1.1055    raeburn  5701: sub courselastaccess {
                   5702:     my ($cdom,$cnum,$hostidref) = @_;
                   5703:     my %returnhash;
                   5704:     if ($cdom && $cnum) {
                   5705:         my $chome = &homeserver($cnum,$cdom);
                   5706:         if ($chome ne 'no_host') {
                   5707:             my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
                   5708:             &extract_lastaccess(\%returnhash,$rep);
                   5709:         }
                   5710:     } else {
                   5711:         if (!$cdom) { $cdom=''; }
                   5712:         my %libserv = &all_library();
                   5713:         foreach my $tryserver (keys(%libserv)) {
                   5714:             if (ref($hostidref) eq 'ARRAY') {
                   5715:                 next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
                   5716:             } 
                   5717:             if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
                   5718:                 my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
                   5719:                 &extract_lastaccess(\%returnhash,$rep);
                   5720:             }
                   5721:         }
                   5722:     }
                   5723:     return %returnhash;
                   5724: }
                   5725: 
                   5726: sub extract_lastaccess {
                   5727:     my ($returnhash,$rep) = @_;
                   5728:     if (ref($returnhash) eq 'HASH') {
                   5729:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' || 
                   5730:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
                   5731:                  $rep eq '') {
                   5732:             my @pairs=split(/\&/,$rep);
                   5733:             foreach my $item (@pairs) {
                   5734:                 my ($key,$value)=split(/\=/,$item,2);
                   5735:                 $key = &unescape($key);
                   5736:                 next if ($key =~ /^error: 2 /);
                   5737:                 $returnhash->{$key} = &thaw_unescape($value);
                   5738:             }
                   5739:         }
                   5740:     }
                   5741:     return;
                   5742: }
                   5743: 
1.658     raeburn  5744: # ---------------------------------------------------------- DC e-mail
1.662     raeburn  5745: 
                   5746: sub dcmailput {
1.685     raeburn  5747:     my ($domain,$msgid,$message,$server)=@_;
1.662     raeburn  5748:     my $status = &Apache::lonnet::critical(
1.740     www      5749:        'dcmailput:'.$domain.':'.&escape($msgid).'='.
                   5750:        &escape($message),$server);
1.662     raeburn  5751:     return $status;
                   5752: }
                   5753: 
1.658     raeburn  5754: sub dcmaildump {
                   5755:     my ($dom,$startdate,$enddate,$senders) = @_;
1.685     raeburn  5756:     my %returnhash=();
1.846     albertel 5757: 
                   5758:     if (defined(&domain($dom,'primary'))) {
1.685     raeburn  5759:         my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
                   5760:                                                          &escape($enddate).':';
                   5761: 	my @esc_senders=map { &escape($_)} @$senders;
                   5762: 	$cmd.=&escape(join('&',@esc_senders));
1.846     albertel 5763: 	foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
1.800     albertel 5764:             my ($key,$value) = split(/\=/,$line,2);
1.685     raeburn  5765:             if (($key) && ($value)) {
                   5766:                 $returnhash{&unescape($key)} = &unescape($value);
1.658     raeburn  5767:             }
                   5768:         }
                   5769:     }
                   5770:     return %returnhash;
                   5771: }
1.662     raeburn  5772: # ---------------------------------------------------------- Domain roles
                   5773: 
                   5774: sub get_domain_roles {
                   5775:     my ($dom,$roles,$startdate,$enddate)=@_;
1.1018    raeburn  5776:     if ((!defined($startdate)) || ($startdate eq '')) {
1.662     raeburn  5777:         $startdate = '.';
                   5778:     }
1.1018    raeburn  5779:     if ((!defined($enddate)) || ($enddate eq '')) {
1.662     raeburn  5780:         $enddate = '.';
                   5781:     }
1.922     raeburn  5782:     my $rolelist;
                   5783:     if (ref($roles) eq 'ARRAY') {
1.1219    raeburn  5784:         $rolelist = join('&',@{$roles});
1.922     raeburn  5785:     }
1.662     raeburn  5786:     my %personnel = ();
1.841     albertel 5787: 
                   5788:     my %servers = &get_servers($dom,'library');
                   5789:     foreach my $tryserver (keys(%servers)) {
                   5790: 	%{$personnel{$tryserver}}=();
                   5791: 	foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
                   5792: 					    &escape($startdate).':'.
                   5793: 					    &escape($enddate).':'.
                   5794: 					    &escape($rolelist), $tryserver))) {
                   5795: 	    my ($key,$value) = split(/\=/,$line,2);
                   5796: 	    if (($key) && ($value)) {
                   5797: 		$personnel{$tryserver}{&unescape($key)} = &unescape($value);
                   5798: 	    }
                   5799: 	}
1.662     raeburn  5800:     }
                   5801:     return %personnel;
                   5802: }
1.658     raeburn  5803: 
1.1332    raeburn  5804: sub get_active_domroles {
                   5805:     my ($dom,$roles) = @_;
                   5806:     return () unless (ref($roles) eq 'ARRAY');
                   5807:     my $now = time;
                   5808:     my %dompersonnel = &get_domain_roles($dom,$roles,$now,$now);
                   5809:     my %domroles;
                   5810:     foreach my $server (keys(%dompersonnel)) {
                   5811:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
                   5812:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
                   5813:             $domroles{$uname.':'.$udom} = $dompersonnel{$server}{$user};
                   5814:         }
                   5815:     }
                   5816:     return %domroles;
                   5817: }
                   5818: 
1.1057    www      5819: # ----------------------------------------------------------- Interval timing 
1.149     www      5820: 
1.1153    www      5821: {
                   5822: # Caches needed for speedup of navmaps
                   5823: # We don't want to cache this for very long at all (5 seconds at most)
                   5824: # 
                   5825: # The user for whom we cache
                   5826: my $cachedkey='';
                   5827: # The cached times for this user
                   5828: my %cachedtimes=();
                   5829: # When this was last done
1.1282    raeburn  5830: my $cachedtime='';
1.1153    www      5831: 
                   5832: sub load_all_first_access {
1.1308    raeburn  5833:     my ($uname,$udom,$ignorecache)=@_;
1.1156    www      5834:     if (($cachedkey eq $uname.':'.$udom) &&
1.1308    raeburn  5835:         (abs($cachedtime-time)<5) && (!$env{'form.markaccess'}) &&
                   5836:         (!$ignorecache)) {
1.1154    raeburn  5837:         return;
                   5838:     }
                   5839:     $cachedtime=time;
                   5840:     $cachedkey=$uname.':'.$udom;
                   5841:     %cachedtimes=&dump('firstaccesstimes',$udom,$uname);
1.1153    www      5842: }
                   5843: 
1.504     albertel 5844: sub get_first_access {
1.1308    raeburn  5845:     my ($type,$argsymb,$argmap,$ignorecache)=@_;
1.790     albertel 5846:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 5847:     if ($argsymb) { $symb=$argsymb; }
                   5848:     my ($map,$id,$res)=&decode_symb($symb);
1.1162    raeburn  5849:     if ($argmap) { $map = $argmap; }
1.926     albertel 5850:     if ($type eq 'course') {
                   5851: 	$res='course';
                   5852:     } elsif ($type eq 'map') {
1.588     albertel 5853: 	$res=&symbread($map);
                   5854:     } else {
                   5855: 	$res=$symb;
                   5856:     }
1.1308    raeburn  5857:     &load_all_first_access($uname,$udom,$ignorecache);
1.1153    www      5858:     return $cachedtimes{"$courseid\0$res"};
1.504     albertel 5859: }
                   5860: 
                   5861: sub set_first_access {
1.1162    raeburn  5862:     my ($type,$interval)=@_;
1.790     albertel 5863:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 5864:     my ($map,$id,$res)=&decode_symb($symb);
1.928     albertel 5865:     if ($type eq 'course') {
                   5866: 	$res='course';
                   5867:     } elsif ($type eq 'map') {
1.588     albertel 5868: 	$res=&symbread($map);
                   5869:     } else {
                   5870: 	$res=$symb;
                   5871:     }
1.1153    www      5872:     $cachedkey='';
1.1162    raeburn  5873:     my $firstaccess=&get_first_access($type,$symb,$map);
1.1386    raeburn  5874:     if ($firstaccess) {
                   5875:         &logthis("First access time already set ($firstaccess) when attempting ".
1.1393    raeburn  5876:                  "to set new value (type: $type, extent: $res) for $uname:$udom ".
                   5877:                  "in $courseid");
1.1386    raeburn  5878:         return 'already_set';
                   5879:     } else {
1.1162    raeburn  5880:         my $start = time;
                   5881: 	my $putres = &put('firstaccesstimes',{"$courseid\0$res"=>$start},
                   5882:                           $udom,$uname);
                   5883:         if ($putres eq 'ok') {
                   5884:             &put('timerinterval',{"$courseid\0$res"=>$interval},
                   5885:                  $udom,$uname); 
                   5886:             &appenv(
                   5887:                      {
                   5888:                         'course.'.$courseid.'.firstaccess.'.$res   => $start,
                   5889:                         'course.'.$courseid.'.timerinterval.'.$res => $interval,
                   5890:                      }
                   5891:                   );
1.1365    raeburn  5892:             if (($cachedtime) && (abs($start-$cachedtime) < 5)) {
                   5893:                 $cachedtimes{"$courseid\0$res"} = $start;
                   5894:             }
1.1386    raeburn  5895:         } elsif ($putres ne 'refused') {
                   5896:             &logthis("Result: $putres when attempting to set first access time ".
                   5897:                      "(type: $type, extent: $res) for $uname:$udom in $courseid");
1.1162    raeburn  5898:         }
                   5899:         return $putres;
1.505     albertel 5900:     }
                   5901:     return 'already_set';
1.504     albertel 5902: }
1.1153    www      5903: }
1.1282    raeburn  5904: 
1.110     www      5905: # --------------------------------------------- Set Expire Date for Spreadsheet
                   5906: 
                   5907: sub expirespread {
                   5908:     my ($uname,$udom,$stype,$usymb)=@_;
1.620     albertel 5909:     my $cid=$env{'request.course.id'}; 
1.110     www      5910:     if ($cid) {
                   5911:        my $now=time;
                   5912:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1.620     albertel 5913:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
                   5914:                             $env{'course.'.$cid.'.num'}.
1.110     www      5915: 	        	    ':nohist_expirationdates:'.
                   5916:                             &escape($key).'='.$now,
1.620     albertel 5917:                             $env{'course.'.$cid.'.home'})
1.110     www      5918:     }
                   5919:     return 'ok';
1.14      www      5920: }
                   5921: 
1.109     www      5922: # ----------------------------------------------------- Devalidate Spreadsheets
                   5923: 
                   5924: sub devalidate {
1.325     www      5925:     my ($symb,$uname,$udom)=@_;
1.620     albertel 5926:     my $cid=$env{'request.course.id'}; 
1.109     www      5927:     if ($cid) {
1.391     matthew  5928:         # delete the stored spreadsheets for
                   5929:         # - the student level sheet of this user in course's homespace
                   5930:         # - the assessment level sheet for this resource 
                   5931:         #   for this user in user's homespace
1.553     albertel 5932: 	# - current conditional state info
1.325     www      5933: 	my $key=$uname.':'.$udom.':';
1.109     www      5934:         my $status=
1.299     matthew  5935: 	    &del('nohist_calculatedsheets',
1.391     matthew  5936: 		 [$key.'studentcalc:'],
1.620     albertel 5937: 		 $env{'course.'.$cid.'.domain'},
                   5938: 		 $env{'course.'.$cid.'.num'})
1.133     albertel 5939: 		.' '.
                   5940: 	    &del('nohist_calculatedsheets_'.$cid,
1.391     matthew  5941: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
1.109     www      5942:         unless ($status eq 'ok ok') {
                   5943:            &logthis('Could not devalidate spreadsheet '.
1.325     www      5944:                     $uname.' at '.$udom.' for '.
1.109     www      5945: 		    $symb.': '.$status);
1.133     albertel 5946:         }
1.553     albertel 5947: 	&delenv('user.state.'.$cid);
1.109     www      5948:     }
                   5949: }
                   5950: 
1.265     albertel 5951: sub get_scalar {
                   5952:     my ($string,$end) = @_;
                   5953:     my $value;
                   5954:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
                   5955: 	$value = $1;
                   5956:     } elsif ($$string =~ s/^([^&]*?)&//) {
                   5957: 	$value = $1;
                   5958:     }
                   5959:     return &unescape($value);
                   5960: }
                   5961: 
                   5962: sub array2str {
                   5963:   my (@array) = @_;
                   5964:   my $result=&arrayref2str(\@array);
                   5965:   $result=~s/^__ARRAY_REF__//;
                   5966:   $result=~s/__END_ARRAY_REF__$//;
                   5967:   return $result;
                   5968: }
                   5969: 
1.204     albertel 5970: sub arrayref2str {
                   5971:   my ($arrayref) = @_;
1.265     albertel 5972:   my $result='__ARRAY_REF__';
1.204     albertel 5973:   foreach my $elem (@$arrayref) {
1.265     albertel 5974:     if(ref($elem) eq 'ARRAY') {
                   5975:       $result.=&arrayref2str($elem).'&';
                   5976:     } elsif(ref($elem) eq 'HASH') {
                   5977:       $result.=&hashref2str($elem).'&';
                   5978:     } elsif(ref($elem)) {
                   5979:       #print("Got a ref of ".(ref($elem))." skipping.");
1.204     albertel 5980:     } else {
                   5981:       $result.=&escape($elem).'&';
                   5982:     }
                   5983:   }
                   5984:   $result=~s/\&$//;
1.265     albertel 5985:   $result .= '__END_ARRAY_REF__';
1.204     albertel 5986:   return $result;
                   5987: }
                   5988: 
1.168     albertel 5989: sub hash2str {
1.204     albertel 5990:   my (%hash) = @_;
                   5991:   my $result=&hashref2str(\%hash);
1.265     albertel 5992:   $result=~s/^__HASH_REF__//;
                   5993:   $result=~s/__END_HASH_REF__$//;
1.204     albertel 5994:   return $result;
                   5995: }
                   5996: 
                   5997: sub hashref2str {
                   5998:   my ($hashref)=@_;
1.265     albertel 5999:   my $result='__HASH_REF__';
1.800     albertel 6000:   foreach my $key (sort(keys(%$hashref))) {
                   6001:     if (ref($key) eq 'ARRAY') {
                   6002:       $result.=&arrayref2str($key).'=';
                   6003:     } elsif (ref($key) eq 'HASH') {
                   6004:       $result.=&hashref2str($key).'=';
                   6005:     } elsif (ref($key)) {
1.265     albertel 6006:       $result.='=';
1.800     albertel 6007:       #print("Got a ref of ".(ref($key))." skipping.");
1.204     albertel 6008:     } else {
1.1132    raeburn  6009: 	if (defined($key)) {$result.=&escape($key).'=';} else { last; }
1.204     albertel 6010:     }
                   6011: 
1.800     albertel 6012:     if(ref($hashref->{$key}) eq 'ARRAY') {
                   6013:       $result.=&arrayref2str($hashref->{$key}).'&';
                   6014:     } elsif(ref($hashref->{$key}) eq 'HASH') {
                   6015:       $result.=&hashref2str($hashref->{$key}).'&';
                   6016:     } elsif(ref($hashref->{$key})) {
1.265     albertel 6017:        $result.='&';
1.800     albertel 6018:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
1.204     albertel 6019:     } else {
1.800     albertel 6020:       $result.=&escape($hashref->{$key}).'&';
1.204     albertel 6021:     }
                   6022:   }
1.168     albertel 6023:   $result=~s/\&$//;
1.265     albertel 6024:   $result .= '__END_HASH_REF__';
1.168     albertel 6025:   return $result;
                   6026: }
                   6027: 
                   6028: sub str2hash {
1.265     albertel 6029:     my ($string)=@_;
                   6030:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
                   6031:     return %$hash;
                   6032: }
                   6033: 
                   6034: sub str2hashref {
1.168     albertel 6035:   my ($string) = @_;
1.265     albertel 6036: 
                   6037:   my %hash;
                   6038: 
                   6039:   if($string !~ /^__HASH_REF__/) {
                   6040:       if (! ($string eq '' || !defined($string))) {
                   6041: 	  $hash{'error'}='Not hash reference';
                   6042:       }
                   6043:       return (\%hash, $string);
                   6044:   }
                   6045: 
                   6046:   $string =~ s/^__HASH_REF__//;
                   6047: 
                   6048:   while($string !~ /^__END_HASH_REF__/) {
                   6049:       #key
                   6050:       my $key='';
                   6051:       if($string =~ /^__HASH_REF__/) {
                   6052:           ($key, $string)=&str2hashref($string);
                   6053:           if(defined($key->{'error'})) {
                   6054:               $hash{'error'}='Bad data';
                   6055:               return (\%hash, $string);
                   6056:           }
                   6057:       } elsif($string =~ /^__ARRAY_REF__/) {
                   6058:           ($key, $string)=&str2arrayref($string);
                   6059:           if($key->[0] eq 'Array reference error') {
                   6060:               $hash{'error'}='Bad data';
                   6061:               return (\%hash, $string);
                   6062:           }
                   6063:       } else {
                   6064:           $string =~ s/^(.*?)=//;
1.267     albertel 6065: 	  $key=&unescape($1);
1.265     albertel 6066:       }
                   6067:       $string =~ s/^=//;
                   6068: 
                   6069:       #value
                   6070:       my $value='';
                   6071:       if($string =~ /^__HASH_REF__/) {
                   6072:           ($value, $string)=&str2hashref($string);
                   6073:           if(defined($value->{'error'})) {
                   6074:               $hash{'error'}='Bad data';
                   6075:               return (\%hash, $string);
                   6076:           }
                   6077:       } elsif($string =~ /^__ARRAY_REF__/) {
                   6078:           ($value, $string)=&str2arrayref($string);
                   6079:           if($value->[0] eq 'Array reference error') {
                   6080:               $hash{'error'}='Bad data';
                   6081:               return (\%hash, $string);
                   6082:           }
                   6083:       } else {
                   6084: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
                   6085:       }
                   6086:       $string =~ s/^&//;
                   6087: 
                   6088:       $hash{$key}=$value;
1.204     albertel 6089:   }
1.265     albertel 6090: 
                   6091:   $string =~ s/^__END_HASH_REF__//;
                   6092: 
                   6093:   return (\%hash, $string);
1.204     albertel 6094: }
                   6095: 
                   6096: sub str2array {
1.265     albertel 6097:     my ($string)=@_;
                   6098:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
                   6099:     return @$array;
                   6100: }
                   6101: 
                   6102: sub str2arrayref {
1.204     albertel 6103:   my ($string) = @_;
1.265     albertel 6104:   my @array;
                   6105: 
                   6106:   if($string !~ /^__ARRAY_REF__/) {
                   6107:       if (! ($string eq '' || !defined($string))) {
                   6108: 	  $array[0]='Array reference error';
                   6109:       }
                   6110:       return (\@array, $string);
                   6111:   }
                   6112: 
                   6113:   $string =~ s/^__ARRAY_REF__//;
                   6114: 
                   6115:   while($string !~ /^__END_ARRAY_REF__/) {
                   6116:       my $value='';
                   6117:       if($string =~ /^__HASH_REF__/) {
                   6118:           ($value, $string)=&str2hashref($string);
                   6119:           if(defined($value->{'error'})) {
                   6120:               $array[0] ='Array reference error';
                   6121:               return (\@array, $string);
                   6122:           }
                   6123:       } elsif($string =~ /^__ARRAY_REF__/) {
                   6124:           ($value, $string)=&str2arrayref($string);
                   6125:           if($value->[0] eq 'Array reference error') {
                   6126:               $array[0] ='Array reference error';
                   6127:               return (\@array, $string);
                   6128:           }
                   6129:       } else {
                   6130: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
                   6131:       }
                   6132:       $string =~ s/^&//;
                   6133: 
                   6134:       push(@array, $value);
1.191     harris41 6135:   }
1.265     albertel 6136: 
                   6137:   $string =~ s/^__END_ARRAY_REF__//;
                   6138: 
                   6139:   return (\@array, $string);
1.168     albertel 6140: }
                   6141: 
1.167     albertel 6142: # -------------------------------------------------------------------Temp Store
                   6143: 
1.168     albertel 6144: sub tmpreset {
                   6145:   my ($symb,$namespace,$domain,$stuname) = @_;
                   6146:   if (!$symb) {
                   6147:     $symb=&symbread();
1.620     albertel 6148:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 6149:   }
                   6150:   $symb=escape($symb);
                   6151: 
1.620     albertel 6152:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.168     albertel 6153:   $namespace=~s/\//\_/g;
                   6154:   $namespace=~s/\W//g;
                   6155: 
1.620     albertel 6156:   if (!$domain) { $domain=$env{'user.domain'}; }
                   6157:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 6158:   if ($domain eq 'public' && $stuname eq 'public') {
1.1434    raeburn  6159:       $stuname=&get_requestor_ip();
1.591     albertel 6160:   }
1.1117    foxr     6161:   my $path=LONCAPA::tempdir();
1.168     albertel 6162:   my %hash;
                   6163:   if (tie(%hash,'GDBM_File',
                   6164: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 6165: 	  &GDBM_WRCREAT(),0640)) {
1.1000    raeburn  6166:     foreach my $key (keys(%hash)) {
1.180     albertel 6167:       if ($key=~ /:$symb/) {
1.168     albertel 6168: 	delete($hash{$key});
                   6169:       }
                   6170:     }
                   6171:   }
                   6172: }
                   6173: 
1.167     albertel 6174: sub tmpstore {
1.168     albertel 6175:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   6176: 
                   6177:   if (!$symb) {
                   6178:     $symb=&symbread();
1.620     albertel 6179:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 6180:   }
                   6181:   $symb=escape($symb);
                   6182: 
                   6183:   if (!$namespace) {
                   6184:     # I don't think we would ever want to store this for a course.
                   6185:     # it seems this will only be used if we don't have a course.
1.620     albertel 6186:     #$namespace=$env{'request.course.id'};
1.168     albertel 6187:     #if (!$namespace) {
1.620     albertel 6188:       $namespace=$env{'request.state'};
1.168     albertel 6189:     #}
                   6190:   }
                   6191:   $namespace=~s/\//\_/g;
                   6192:   $namespace=~s/\W//g;
1.620     albertel 6193:   if (!$domain) { $domain=$env{'user.domain'}; }
                   6194:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 6195:   if ($domain eq 'public' && $stuname eq 'public') {
1.1434    raeburn  6196:       $stuname=&get_requestor_ip();
1.591     albertel 6197:   }
1.168     albertel 6198:   my $now=time;
                   6199:   my %hash;
1.1117    foxr     6200:   my $path=LONCAPA::tempdir();
1.168     albertel 6201:   if (tie(%hash,'GDBM_File',
                   6202: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 6203: 	  &GDBM_WRCREAT(),0640)) {
1.168     albertel 6204:     $hash{"version:$symb"}++;
                   6205:     my $version=$hash{"version:$symb"};
                   6206:     my $allkeys=''; 
                   6207:     foreach my $key (keys(%$storehash)) {
                   6208:       $allkeys.=$key.':';
1.591     albertel 6209:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
1.168     albertel 6210:     }
                   6211:     $hash{"$version:$symb:timestamp"}=$now;
                   6212:     $allkeys.='timestamp';
                   6213:     $hash{"$version:keys:$symb"}=$allkeys;
                   6214:     if (untie(%hash)) {
                   6215:       return 'ok';
                   6216:     } else {
                   6217:       return "error:$!";
                   6218:     }
                   6219:   } else {
                   6220:     return "error:$!";
                   6221:   }
                   6222: }
1.167     albertel 6223: 
1.168     albertel 6224: # -----------------------------------------------------------------Temp Restore
1.167     albertel 6225: 
1.168     albertel 6226: sub tmprestore {
                   6227:   my ($symb,$namespace,$domain,$stuname) = @_;
1.167     albertel 6228: 
1.168     albertel 6229:   if (!$symb) {
                   6230:     $symb=&symbread();
1.620     albertel 6231:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 6232:   }
                   6233:   $symb=escape($symb);
                   6234: 
1.620     albertel 6235:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.591     albertel 6236: 
1.620     albertel 6237:   if (!$domain) { $domain=$env{'user.domain'}; }
                   6238:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 6239:   if ($domain eq 'public' && $stuname eq 'public') {
1.1434    raeburn  6240:       $stuname=&get_requestor_ip();
1.591     albertel 6241:   }
1.168     albertel 6242:   my %returnhash;
                   6243:   $namespace=~s/\//\_/g;
                   6244:   $namespace=~s/\W//g;
                   6245:   my %hash;
1.1117    foxr     6246:   my $path=LONCAPA::tempdir();
1.168     albertel 6247:   if (tie(%hash,'GDBM_File',
                   6248: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 6249: 	  &GDBM_READER(),0640)) {
1.168     albertel 6250:     my $version=$hash{"version:$symb"};
                   6251:     $returnhash{'version'}=$version;
                   6252:     my $scope;
                   6253:     for ($scope=1;$scope<=$version;$scope++) {
                   6254:       my $vkeys=$hash{"$scope:keys:$symb"};
                   6255:       my @keys=split(/:/,$vkeys);
                   6256:       my $key;
                   6257:       $returnhash{"$scope:keys"}=$vkeys;
                   6258:       foreach $key (@keys) {
1.591     albertel 6259: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
                   6260: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
1.167     albertel 6261:       }
                   6262:     }
1.168     albertel 6263:     if (!(untie(%hash))) {
                   6264:       return "error:$!";
                   6265:     }
                   6266:   } else {
                   6267:     return "error:$!";
                   6268:   }
                   6269:   return %returnhash;
1.167     albertel 6270: }
                   6271: 
1.9       www      6272: # ----------------------------------------------------------------------- Store
                   6273: 
                   6274: sub store {
1.1269    raeburn  6275:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
1.124     www      6276:     my $home='';
                   6277: 
1.168     albertel 6278:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      6279: 
1.213     www      6280:     $symb=&symbclean($symb);
1.122     albertel 6281:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      6282: 
1.620     albertel 6283:     if (!$domain) { $domain=$env{'user.domain'}; }
                   6284:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      6285: 
                   6286:     &devalidate($symb,$stuname,$domain);
1.109     www      6287: 
                   6288:     $symb=escape($symb);
1.187     www      6289:     if (!$namespace) { 
1.620     albertel 6290:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      6291:           return ''; 
                   6292:        } 
                   6293:     }
1.620     albertel 6294:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      6295: 
1.1434    raeburn  6296:     $$storehash{'ip'}=&get_requestor_ip();
1.447     www      6297:     $$storehash{'host'}=$perlvar{'lonHostID'};
                   6298: 
1.12      www      6299:     my $namevalue='';
1.800     albertel 6300:     foreach my $key (keys(%$storehash)) {
                   6301:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 6302:     }
1.12      www      6303:     $namevalue=~s/\&$//;
1.187     www      6304:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.1269    raeburn  6305:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
1.9       www      6306: }
                   6307: 
1.47      www      6308: # -------------------------------------------------------------- Critical Store
                   6309: 
                   6310: sub cstore {
1.1269    raeburn  6311:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
1.124     www      6312:     my $home='';
                   6313: 
1.168     albertel 6314:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      6315: 
1.213     www      6316:     $symb=&symbclean($symb);
1.122     albertel 6317:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      6318: 
1.620     albertel 6319:     if (!$domain) { $domain=$env{'user.domain'}; }
                   6320:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      6321: 
                   6322:     &devalidate($symb,$stuname,$domain);
1.109     www      6323: 
                   6324:     $symb=escape($symb);
1.187     www      6325:     if (!$namespace) { 
1.620     albertel 6326:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      6327:           return ''; 
                   6328:        } 
                   6329:     }
1.620     albertel 6330:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      6331: 
1.1434    raeburn  6332:     $$storehash{'ip'}=&get_requestor_ip();
1.447     www      6333:     $$storehash{'host'}=$perlvar{'lonHostID'};
1.122     albertel 6334: 
1.47      www      6335:     my $namevalue='';
1.800     albertel 6336:     foreach my $key (keys(%$storehash)) {
                   6337:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 6338:     }
1.47      www      6339:     $namevalue=~s/\&$//;
1.187     www      6340:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188     www      6341:     return critical
1.1269    raeburn  6342:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
1.47      www      6343: }
                   6344: 
1.9       www      6345: # --------------------------------------------------------------------- Restore
                   6346: 
                   6347: sub restore {
1.124     www      6348:     my ($symb,$namespace,$domain,$stuname) = @_;
                   6349:     my $home='';
                   6350: 
1.168     albertel 6351:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      6352: 
1.122     albertel 6353:     if (!$symb) {
1.1224    raeburn  6354:         return if ($namespace eq 'courserequests');
                   6355:         unless ($symb=escape(&symbread())) { return ''; }
1.122     albertel 6356:     } else {
1.1224    raeburn  6357:         unless ($namespace eq 'courserequests') {
                   6358:             $symb=&escape(&symbclean($symb));
                   6359:         }
1.122     albertel 6360:     }
1.188     www      6361:     if (!$namespace) { 
1.620     albertel 6362:        unless ($namespace=$env{'request.course.id'}) { 
1.188     www      6363:           return ''; 
                   6364:        } 
                   6365:     }
1.620     albertel 6366:     if (!$domain) { $domain=$env{'user.domain'}; }
                   6367:     if (!$stuname) { $stuname=$env{'user.name'}; }
                   6368:     if (!$home) { $home=$env{'user.home'}; }
1.122     albertel 6369:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
                   6370: 
1.12      www      6371:     my %returnhash=();
1.800     albertel 6372:     foreach my $line (split(/\&/,$answer)) {
                   6373: 	my ($name,$value)=split(/\=/,$line);
1.591     albertel 6374:         $returnhash{&unescape($name)}=&thaw_unescape($value);
1.191     harris41 6375:     }
1.75      www      6376:     my $version;
                   6377:     for ($version=1;$version<=$returnhash{'version'};$version++) {
1.800     albertel 6378:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
                   6379:           $returnhash{$item}=$returnhash{$version.':'.$item};
1.191     harris41 6380:        }
1.75      www      6381:     }
1.13      www      6382:     return %returnhash;
1.34      www      6383: }
                   6384: 
                   6385: # ---------------------------------------------------------- Course Description
1.1118    foxr     6386: #
                   6387: #  
1.34      www      6388: 
                   6389: sub coursedescription {
1.731     albertel 6390:     my ($courseid,$args)=@_;
1.34      www      6391:     $courseid=~s/^\///;
1.49      www      6392:     $courseid=~s/\_/\//g;
1.34      www      6393:     my ($cdomain,$cnum)=split(/\//,$courseid);
1.129     albertel 6394:     my $chome=&homeserver($cnum,$cdomain);
1.302     albertel 6395:     my $normalid=$cdomain.'_'.$cnum;
                   6396:     # need to always cache even if we get errors otherwise we keep 
                   6397:     # trying and trying and trying to get the course description.
                   6398:     my %envhash=();
                   6399:     my %returnhash=();
1.731     albertel 6400:     
                   6401:     my $expiretime=600;
                   6402:     if ($env{'request.course.id'} eq $normalid) {
                   6403: 	$expiretime=120;
                   6404:     }
                   6405: 
                   6406:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
                   6407:     if (!$args->{'freshen_cache'}
                   6408: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
                   6409: 	foreach my $key (keys(%env)) {
                   6410: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
                   6411: 	    my ($setting) = $1;
                   6412: 	    $returnhash{$setting} = $env{$key};
                   6413: 	}
                   6414: 	return %returnhash;
                   6415:     }
                   6416: 
1.1118    foxr     6417:     # get the data again
                   6418: 
1.731     albertel 6419:     if (!$args->{'one_time'}) {
                   6420: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
                   6421:     }
1.811     albertel 6422: 
1.34      www      6423:     if ($chome ne 'no_host') {
1.302     albertel 6424:        %returnhash=&dump('environment',$cdomain,$cnum);
1.129     albertel 6425:        if (!exists($returnhash{'con_lost'})) {
1.1118    foxr     6426: 	   my $username = $env{'user.name'}; # Defult username
                   6427: 	   if(defined $args->{'user'}) {
                   6428: 	       $username = $args->{'user'};
                   6429: 	   }
1.129     albertel 6430:            $returnhash{'home'}= $chome;
                   6431: 	   $returnhash{'domain'} = $cdomain;
                   6432: 	   $returnhash{'num'} = $cnum;
1.741     raeburn  6433:            if (!defined($returnhash{'type'})) {
                   6434:                $returnhash{'type'} = 'Course';
                   6435:            }
1.130     albertel 6436:            while (my ($name,$value) = each %returnhash) {
1.53      www      6437:                $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129     albertel 6438:            }
1.270     www      6439:            $returnhash{'url'}=&clutter($returnhash{'url'});
1.1117    foxr     6440:            $returnhash{'fn'}=LONCAPA::tempdir() .
1.1118    foxr     6441: 	       $username.'_'.$cdomain.'_'.$cnum;
1.60      www      6442:            $envhash{'course.'.$normalid.'.home'}=$chome;
                   6443:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
                   6444:            $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34      www      6445:        }
                   6446:     }
1.731     albertel 6447:     if (!$args->{'one_time'}) {
1.949     raeburn  6448: 	&appenv(\%envhash);
1.731     albertel 6449:     }
1.302     albertel 6450:     return %returnhash;
1.461     www      6451: }
                   6452: 
1.1080    raeburn  6453: sub update_released_required {
                   6454:     my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
                   6455:     if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
                   6456:         $cid = $env{'request.course.id'};
                   6457:         $cdom = $env{'course.'.$cid.'.domain'};
                   6458:         $cnum = $env{'course.'.$cid.'.num'};
                   6459:         $chome = $env{'course.'.$cid.'.home'};
                   6460:     }
                   6461:     if ($needsrelease) {
                   6462:         my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
                   6463:         my $needsupdate;
                   6464:         if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
                   6465:             $needsupdate = 1;
                   6466:         } else {
                   6467:             my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
                   6468:             my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
                   6469:             if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
                   6470:                 $needsupdate = 1;
                   6471:             }
                   6472:         }
                   6473:         if ($needsupdate) {
                   6474:             my %needshash = (
                   6475:                              'internal.releaserequired' => $needsrelease,
                   6476:                             );
                   6477:             my $putresult = &put('environment',\%needshash,$cdom,$cnum);
                   6478:             if ($putresult eq 'ok') {
                   6479:                 &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
                   6480:                 my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
                   6481:                 if (ref($crsinfo{$cid}) eq 'HASH') {
                   6482:                     $crsinfo{$cid}{'releaserequired'} = $needsrelease;
                   6483:                     &courseidput($cdom,\%crsinfo,$chome,'notime');
                   6484:                 }
                   6485:             }
                   6486:         }
                   6487:     }
                   6488:     return;
                   6489: }
                   6490: 
1.461     www      6491: # -------------------------------------------------See if a user is privileged
                   6492: 
                   6493: sub privileged {
1.1219    raeburn  6494:     my ($username,$domain,$possdomains,$possroles)=@_;
1.1170    droeschl 6495:     my $now = time;
1.1219    raeburn  6496:     my $roles;
                   6497:     if (ref($possroles) eq 'ARRAY') {
                   6498:         $roles = $possroles; 
                   6499:     } else {
                   6500:         $roles = ['dc','su'];
                   6501:     }
                   6502:     if (ref($possdomains) eq 'ARRAY') {
                   6503:         my %privileged = &privileged_by_domain($possdomains,$roles);
                   6504:         foreach my $dom (@{$possdomains}) {
                   6505:             if (($username =~ /^$match_username$/) && ($domain =~ /^$match_domain$/) &&
                   6506:                 (ref($privileged{$dom}) eq 'HASH')) {
                   6507:                 foreach my $role (@{$roles}) {
                   6508:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
                   6509:                         if (exists($privileged{$dom}{$role}{$username.':'.$domain})) {
                   6510:                             my ($end,$start) = split(/:/,$privileged{$dom}{$role}{$username.':'.$domain});
                   6511:                             return 1 unless (($end && $end < $now) ||
                   6512:                                              ($start && $start > $now));
                   6513:                         }
                   6514:                     }
                   6515:                 }
                   6516:             }
                   6517:         }
                   6518:     } else {
                   6519:         my %rolesdump = &dump("roles", $domain, $username) or return 0;
                   6520:         my $now = time;
1.1170    droeschl 6521: 
1.1275    musolffc 6522:         for my $role (@rolesdump{grep { ! /^rolesdef_/ } keys(%rolesdump)}) {
1.1170    droeschl 6523:             my ($trole, $tend, $tstart) = split(/_/, $role);
1.1219    raeburn  6524:             if (grep(/^\Q$trole\E$/,@{$roles})) {
1.1170    droeschl 6525:                 return 1 unless ($tend && $tend < $now) 
1.1219    raeburn  6526:                         or ($tstart && $tstart > $now);
1.1170    droeschl 6527:             }
1.1219    raeburn  6528:         }
                   6529:     }
                   6530:     return 0;
                   6531: }
1.1170    droeschl 6532: 
1.1219    raeburn  6533: sub privileged_by_domain {
                   6534:     my ($domains,$roles) = @_;
                   6535:     my %privileged = ();
                   6536:     my $cachetime = 60*60*24;
                   6537:     my $now = time;
                   6538:     unless ((ref($domains) eq 'ARRAY') && (ref($roles) eq 'ARRAY')) {
                   6539:         return %privileged;
                   6540:     }
                   6541:     foreach my $dom (@{$domains}) {
                   6542:         next if (ref($privileged{$dom}) eq 'HASH');
                   6543:         my $needroles;
                   6544:         foreach my $role (@{$roles}) {
                   6545:             my ($result,$cached)=&is_cached_new('priv_'.$role,$dom);
                   6546:             if (defined($cached)) {
                   6547:                 if (ref($result) eq 'HASH') {
                   6548:                     $privileged{$dom}{$role} = $result;
                   6549:                 }
                   6550:             } else {
                   6551:                 $needroles = 1;
                   6552:             }
                   6553:         }
                   6554:         if ($needroles) {
                   6555:             my %dompersonnel = &get_domain_roles($dom,$roles);
                   6556:             $privileged{$dom} = {};
                   6557:             foreach my $server (keys(%dompersonnel)) {
                   6558:                 if (ref($dompersonnel{$server}) eq 'HASH') {
                   6559:                     foreach my $item (keys(%{$dompersonnel{$server}})) {
                   6560:                         my ($trole,$uname,$udom,$rest) = split(/:/,$item,4);
                   6561:                         my ($end,$start) = split(/:/,$dompersonnel{$server}{$item});
                   6562:                         next if ($end && $end < $now);
                   6563:                         $privileged{$dom}{$trole}{$uname.':'.$udom} = 
                   6564:                             $dompersonnel{$server}{$item};
                   6565:                     }
                   6566:                 }
                   6567:             }
                   6568:             if (ref($privileged{$dom}) eq 'HASH') {
                   6569:                 foreach my $role (@{$roles}) {
                   6570:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
                   6571:                         &do_cache_new('priv_'.$role,$dom,$privileged{$dom}{$role},$cachetime);
                   6572:                     } else {
                   6573:                         my %hash = ();
                   6574:                         &do_cache_new('priv_'.$role,$dom,\%hash,$cachetime);
                   6575:                     }
                   6576:                 }
                   6577:             }
                   6578:         }
                   6579:     }
                   6580:     return %privileged;
1.9       www      6581: }
1.1       albertel 6582: 
1.103     harris41 6583: # -------------------------------------------------------- Get user privileges
1.11      www      6584: 
                   6585: sub rolesinit {
1.1169    droeschl 6586:     my ($domain, $username) = @_;
                   6587:     my %userroles = ('user.login.time' => time);
                   6588:     my %rolesdump = &dump("roles", $domain, $username) or return \%userroles;
                   6589: 
                   6590:     # firstaccess and timerinterval are related to timed maps/resources. 
                   6591:     # also, blocking can be triggered by an activating timer
                   6592:     # it's saved in the user's %env.
                   6593:     my %firstaccess = &dump('firstaccesstimes', $domain, $username);
                   6594:     my %timerinterval = &dump('timerinterval', $domain, $username);
                   6595:     my (%coursetimerstarts, %firstaccchk, %firstaccenv, %coursetimerintervals,
                   6596:         %timerintchk, %timerintenv);
                   6597: 
1.1162    raeburn  6598:     foreach my $key (keys(%firstaccess)) {
1.1169    droeschl 6599:         my ($cid, $rest) = split(/\0/, $key);
1.1162    raeburn  6600:         $coursetimerstarts{$cid}{$rest} = $firstaccess{$key};
                   6601:     }
1.1169    droeschl 6602: 
1.1162    raeburn  6603:     foreach my $key (keys(%timerinterval)) {
                   6604:         my ($cid,$rest) = split(/\0/,$key);
                   6605:         $coursetimerintervals{$cid}{$rest} = $timerinterval{$key};
                   6606:     }
1.1169    droeschl 6607: 
1.11      www      6608:     my %allroles=();
1.1162    raeburn  6609:     my %allgroups=();
1.11      www      6610: 
1.1274    raeburn  6611:     for my $area (grep { ! /^rolesdef_/ } keys(%rolesdump)) {
1.1169    droeschl 6612:         my $role = $rolesdump{$area};
                   6613:         $area =~ s/\_\w\w$//;
                   6614: 
                   6615:         my ($trole, $tend, $tstart, $group_privs);
                   6616: 
                   6617:         if ($role =~ /^cr/) {
                   6618:         # Custom role, defined by a user 
                   6619:         # e.g., user.role.cr/msu/smith/mynewrole
                   6620:             if ($role =~ m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
                   6621:                 $trole = $1;
                   6622:                 ($tend, $tstart) = split('_', $2);
                   6623:             } else {
                   6624:                 $trole = $role;
                   6625:             }
                   6626:         } elsif ($role =~ m|^gr/|) {
                   6627:         # Role of member in a group, defined within a course/community
                   6628:         # e.g., user.role.gr/msu/04935610a19ee4a5fmsul1/leopards
                   6629:             ($trole, $tend, $tstart) = split(/_/, $role);
                   6630:             next if $tstart eq '-1';
                   6631:             ($trole, $group_privs) = split(/\//, $trole);
                   6632:             $group_privs = &unescape($group_privs);
                   6633:         } else {
                   6634:         # Just a normal role, defined in roles.tab
                   6635:             ($trole, $tend, $tstart) = split(/_/,$role);
                   6636:         }
                   6637: 
                   6638:         my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
                   6639:                  $username);
                   6640:         @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
                   6641: 
                   6642:         # role expired or not available yet?
                   6643:         $trole = '' if ($tend != 0 && $tend < $userroles{'user.login.time'}) or 
                   6644:             ($tstart != 0 && $tstart > $userroles{'user.login.time'});
                   6645: 
                   6646:         next if $area eq '' or $trole eq '';
                   6647: 
                   6648:         my $spec = "$trole.$area";
                   6649:         my ($tdummy, $tdomain, $trest) = split(/\//, $area);
                   6650: 
                   6651:         if ($trole =~ /^cr\//) {
                   6652:         # Custom role, defined by a user
                   6653:             &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
                   6654:         } elsif ($trole eq 'gr') {
                   6655:         # Role of a member in a group, defined within a course/community
                   6656:             &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
                   6657:             next;
                   6658:         } else {
                   6659:         # Normal role, defined in roles.tab
                   6660:             &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
                   6661:         }
                   6662: 
                   6663:         my $cid = $tdomain.'_'.$trest;
                   6664:         unless ($firstaccchk{$cid}) {
                   6665:             if (ref($coursetimerstarts{$cid}) eq 'HASH') {
                   6666:                 foreach my $item (keys(%{$coursetimerstarts{$cid}})) {
                   6667:                     $firstaccenv{'course.'.$cid.'.firstaccess.'.$item} = 
                   6668:                         $coursetimerstarts{$cid}{$item}; 
                   6669:                 }
                   6670:             }
                   6671:             $firstaccchk{$cid} = 1;
                   6672:         }
                   6673:         unless ($timerintchk{$cid}) {
                   6674:             if (ref($coursetimerintervals{$cid}) eq 'HASH') {
                   6675:                 foreach my $item (keys(%{$coursetimerintervals{$cid}})) {
                   6676:                     $timerintenv{'course.'.$cid.'.timerinterval.'.$item} =
                   6677:                        $coursetimerintervals{$cid}{$item};
1.1162    raeburn  6678:                 }
1.12      www      6679:             }
1.1169    droeschl 6680:             $timerintchk{$cid} = 1;
1.191     harris41 6681:         }
1.11      www      6682:     }
1.1169    droeschl 6683: 
1.1341    raeburn  6684:     @userroles{'user.author','user.adv','user.rar'} = &set_userprivs(\%userroles,
                   6685:                                                           \%allroles, \%allgroups);
1.1169    droeschl 6686:     $env{'user.adv'} = $userroles{'user.adv'};
1.1341    raeburn  6687:     $env{'user.rar'} = $userroles{'user.rar'};
1.1169    droeschl 6688: 
1.1162    raeburn  6689:     return (\%userroles,\%firstaccenv,\%timerintenv);
1.11      www      6690: }
                   6691: 
1.567     raeburn  6692: sub set_arearole {
1.1215    raeburn  6693:     my ($trole,$area,$tstart,$tend,$domain,$username,$nolog) = @_;
                   6694:     unless ($nolog) {
1.567     raeburn  6695: # log the associated role with the area
1.1215    raeburn  6696:         &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
                   6697:     }
1.743     albertel 6698:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
1.567     raeburn  6699: }
                   6700: 
                   6701: sub custom_roleprivs {
                   6702:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
                   6703:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
1.1250    raeburn  6704:     my $homsvr = &homeserver($rauthor,$rdomain);
1.838     albertel 6705:     if (&hostname($homsvr) ne '') {
1.567     raeburn  6706:         my ($rdummy,$roledef)=
                   6707:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
                   6708:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
                   6709:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
                   6710:             if (defined($syspriv)) {
1.1043    raeburn  6711:                 if ($trest =~ /^$match_community$/) {
                   6712:                     $syspriv =~ s/bre\&S//; 
                   6713:                 }
1.567     raeburn  6714:                 $$allroles{'cm./'}.=':'.$syspriv;
                   6715:                 $$allroles{$spec.'./'}.=':'.$syspriv;
                   6716:             }
                   6717:             if ($tdomain ne '') {
                   6718:                 if (defined($dompriv)) {
                   6719:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
                   6720:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
                   6721:                 }
                   6722:                 if (($trest ne '') && (defined($coursepriv))) {
1.1332    raeburn  6723:                     if ($trole =~ m{^cr/$tdomain/$tdomain\Q-domainconfig\E/([^/]+)$}) {
                   6724:                         my $rolename = $1;
                   6725:                         $coursepriv = &course_adhocrole_privs($rolename,$tdomain,$trest,$coursepriv);
                   6726:                     }
1.567     raeburn  6727:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
                   6728:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
                   6729:                 }
                   6730:             }
                   6731:         }
                   6732:     }
                   6733: }
                   6734: 
1.1332    raeburn  6735: sub course_adhocrole_privs {
                   6736:     my ($rolename,$cdom,$cnum,$coursepriv) = @_;
                   6737:     my %overrides = &get('environment',["internal.adhocpriv.$rolename"],$cdom,$cnum);
                   6738:     if ($overrides{"internal.adhocpriv.$rolename"}) {
                   6739:         my (%currprivs,%storeprivs);
                   6740:         foreach my $item (split(/:/,$coursepriv)) {
                   6741:             my ($priv,$restrict) = split(/\&/,$item);
                   6742:             $currprivs{$priv} = $restrict;
                   6743:         }
                   6744:         my (%possadd,%possremove,%full);
                   6745:         foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
                   6746:             my ($priv,$restrict)=split(/\&/,$item);
                   6747:             $full{$priv} = $restrict;
                   6748:         }
                   6749:         foreach my $item (split(/,/,$overrides{"internal.adhocpriv.$rolename"})) {
                   6750:              next if ($item eq '');
                   6751:              my ($rule,$rest) = split(/=/,$item);
                   6752:              next unless (($rule eq 'off') || ($rule eq 'on'));
                   6753:              foreach my $priv (split(/:/,$rest)) {
                   6754:                  if ($priv ne '') {
                   6755:                      if ($rule eq 'off') {
                   6756:                          $possremove{$priv} = 1;
                   6757:                      } else {
                   6758:                          $possadd{$priv} = 1;
                   6759:                      }
                   6760:                  }
                   6761:              }
                   6762:          }
                   6763:          foreach my $priv (sort(keys(%full))) {
                   6764:              if (exists($currprivs{$priv})) {
                   6765:                  unless (exists($possremove{$priv})) {
                   6766:                      $storeprivs{$priv} = $currprivs{$priv};
                   6767:                  }
                   6768:              } elsif (exists($possadd{$priv})) {
                   6769:                  $storeprivs{$priv} = $full{$priv};
                   6770:              }
                   6771:          }
                   6772:          $coursepriv = ':'.join(':',map { $_.'&'.$storeprivs{$_}; } sort(keys(%storeprivs)));
                   6773:      }
                   6774:      return $coursepriv;
                   6775: }
                   6776: 
1.678     raeburn  6777: sub group_roleprivs {
                   6778:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
                   6779:     my $access = 1;
                   6780:     my $now = time;
                   6781:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
                   6782:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
                   6783:     if ($access) {
1.811     albertel 6784:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
1.678     raeburn  6785:         $$allgroups{$course}{$group} .=':'.$group_privs;
                   6786:     }
                   6787: }
1.567     raeburn  6788: 
                   6789: sub standard_roleprivs {
                   6790:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
                   6791:     if (defined($pr{$trole.':s'})) {
                   6792:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
                   6793:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
                   6794:     }
                   6795:     if ($tdomain ne '') {
                   6796:         if (defined($pr{$trole.':d'})) {
                   6797:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   6798:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   6799:         }
                   6800:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
                   6801:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
                   6802:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
                   6803:         }
                   6804:     }
                   6805: }
                   6806: 
                   6807: sub set_userprivs {
1.1064    raeburn  6808:     my ($userroles,$allroles,$allgroups,$groups_roles) = @_; 
1.567     raeburn  6809:     my $author=0;
                   6810:     my $adv=0;
1.1341    raeburn  6811:     my $rar=0;
1.678     raeburn  6812:     my %grouproles = ();
                   6813:     if (keys(%{$allgroups}) > 0) {
1.1064    raeburn  6814:         my @groupkeys; 
1.1000    raeburn  6815:         foreach my $role (keys(%{$allroles})) {
1.1064    raeburn  6816:             push(@groupkeys,$role);
                   6817:         }
                   6818:         if (ref($groups_roles) eq 'HASH') {
                   6819:             foreach my $key (keys(%{$groups_roles})) {
                   6820:                 unless (grep(/^\Q$key\E$/,@groupkeys)) {
                   6821:                     push(@groupkeys,$key);
                   6822:                 }
                   6823:             }
                   6824:         }
                   6825:         if (@groupkeys > 0) {
                   6826:             foreach my $role (@groupkeys) {
                   6827:                 my ($trole,$area,$sec,$extendedarea);
                   6828:                 if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
                   6829:                     $trole = $1;
                   6830:                     $area = $2;
                   6831:                     $sec = $3;
                   6832:                     $extendedarea = $area.$sec;
                   6833:                     if (exists($$allgroups{$area})) {
                   6834:                         foreach my $group (keys(%{$$allgroups{$area}})) {
                   6835:                             my $spec = $trole.'.'.$extendedarea;
                   6836:                             $grouproles{$spec.'.'.$area.'/'.$group} = 
1.681     raeburn  6837:                                                 $$allgroups{$area}{$group};
1.1064    raeburn  6838:                         }
1.678     raeburn  6839:                     }
                   6840:                 }
                   6841:             }
                   6842:         }
                   6843:     }
1.800     albertel 6844:     foreach my $group (keys(%grouproles)) {
                   6845:         $$allroles{$group} = $grouproles{$group};
1.678     raeburn  6846:     }
1.800     albertel 6847:     foreach my $role (keys(%{$allroles})) {
                   6848:         my %thesepriv;
1.941     raeburn  6849:         if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
1.800     albertel 6850:         foreach my $item (split(/:/,$$allroles{$role})) {
                   6851:             if ($item ne '') {
                   6852:                 my ($privilege,$restrictions)=split(/&/,$item);
1.567     raeburn  6853:                 if ($restrictions eq '') {
                   6854:                     $thesepriv{$privilege}='F';
                   6855:                 } elsif ($thesepriv{$privilege} ne 'F') {
                   6856:                     $thesepriv{$privilege}.=$restrictions;
                   6857:                 }
                   6858:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
1.1341    raeburn  6859:                 if ($thesepriv{'rar'} eq 'F') { $rar=1; }
1.567     raeburn  6860:             }
                   6861:         }
                   6862:         my $thesestr='';
1.1104    raeburn  6863:         foreach my $priv (sort(keys(%thesepriv))) {
1.800     albertel 6864: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
                   6865: 	}
                   6866:         $userroles->{'user.priv.'.$role} = $thesestr;
1.567     raeburn  6867:     }
1.1341    raeburn  6868:     return ($author,$adv,$rar);
1.567     raeburn  6869: }
                   6870: 
1.994     raeburn  6871: sub role_status {
1.1104    raeburn  6872:     my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
1.994     raeburn  6873:     if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
1.1250    raeburn  6874:         my ($one,$two) = split(m{\./},$rolekey,2);
                   6875:         (undef,undef,$$role) = split(/\./,$one,3);
1.994     raeburn  6876:         unless (!defined($$role) || $$role eq '') {
1.1251    raeburn  6877:             $$where = '/'.$two;
1.994     raeburn  6878:             $$trolecode=$$role.'.'.$$where;
                   6879:             ($$tstart,$$tend)=split(/\./,$env{$rolekey});
                   6880:             $$tstatus='is';
1.1104    raeburn  6881:             if ($$tstart && $$tstart>$update) {
1.994     raeburn  6882:                 $$tstatus='future';
1.1034    raeburn  6883:                 if ($$tstart<$now) {
                   6884:                     if ($$tstart && $$tstart>$refresh) {
1.1002    raeburn  6885:                         if (($$where ne '') && ($$role ne '')) {
1.1064    raeburn  6886:                             my (%allroles,%allgroups,$group_privs,
                   6887:                                 %groups_roles,@rolecodes);
1.1002    raeburn  6888:                             my %userroles = (
                   6889:                                 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
                   6890:                             );
1.1064    raeburn  6891:                             @rolecodes = ('cm'); 
1.1002    raeburn  6892:                             my $spec=$$role.'.'.$$where;
                   6893:                             my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
                   6894:                             if ($$role =~ /^cr\//) {
                   6895:                                 &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
1.1064    raeburn  6896:                                 push(@rolecodes,'cr');
1.1002    raeburn  6897:                             } elsif ($$role eq 'gr') {
1.1064    raeburn  6898:                                 push(@rolecodes,$$role);
1.1002    raeburn  6899:                                 my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
                   6900:                                                     $env{'user.name'});
1.1064    raeburn  6901:                                 my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
1.1002    raeburn  6902:                                 (undef,my $group_privs) = split(/\//,$trole);
                   6903:                                 $group_privs = &unescape($group_privs);
                   6904:                                 &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
1.1064    raeburn  6905:                                 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  6906:                                 &get_groups_roles($tdomain,$trest,
                   6907:                                                   \%course_roles,\@rolecodes,
                   6908:                                                   \%groups_roles);
1.1002    raeburn  6909:                             } else {
1.1064    raeburn  6910:                                 push(@rolecodes,$$role);
1.1002    raeburn  6911:                                 &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
                   6912:                             }
1.1341    raeburn  6913:                             my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%allroles,\%allgroups,
                   6914:                                                                    \%groups_roles);
1.1064    raeburn  6915:                             &appenv(\%userroles,\@rolecodes);
1.1342    raeburn  6916:                             &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
1.1002    raeburn  6917:                         }
                   6918:                     }
1.1034    raeburn  6919:                     $$tstatus = 'is';
1.1002    raeburn  6920:                 }
1.994     raeburn  6921:             }
                   6922:             if ($$tend) {
1.1104    raeburn  6923:                 if ($$tend<$update) {
1.994     raeburn  6924:                     $$tstatus='expired';
                   6925:                 } elsif ($$tend<$now) {
                   6926:                     $$tstatus='will_not';
                   6927:                 }
                   6928:             }
                   6929:         }
                   6930:     }
                   6931: }
                   6932: 
1.1104    raeburn  6933: sub get_groups_roles {
                   6934:     my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
                   6935:     return unless((ref($cdom_courseroles) eq 'HASH') && 
                   6936:                   (ref($rolecodes) eq 'ARRAY') && 
                   6937:                   (ref($groups_roles) eq 'HASH')); 
                   6938:     if (keys(%{$cdom_courseroles}) > 0) {
                   6939:         my ($cnum) = ($rest =~ /^($match_courseid)/);
                   6940:         if ($cdom ne '' && $cnum ne '') {
                   6941:             foreach my $key (keys(%{$cdom_courseroles})) {
                   6942:                 if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
                   6943:                     my $crsrole = $1;
                   6944:                     my $crssec = $2;
                   6945:                     if ($crsrole =~ /^cr/) {
                   6946:                         unless (grep(/^cr$/,@{$rolecodes})) {
                   6947:                             push(@{$rolecodes},'cr');
                   6948:                         }
                   6949:                     } else {
                   6950:                         unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
                   6951:                             push(@{$rolecodes},$crsrole);
                   6952:                         }
                   6953:                     }
                   6954:                     my $rolekey = "$crsrole./$cdom/$cnum";
                   6955:                     if ($crssec ne '') {
                   6956:                         $rolekey .= "/$crssec";
                   6957:                     }
                   6958:                     $rolekey .= './';
                   6959:                     $groups_roles->{$rolekey} = $rolecodes;
                   6960:                 }
                   6961:             }
                   6962:         }
                   6963:     }
                   6964:     return;
                   6965: }
                   6966: 
                   6967: sub delete_env_groupprivs {
                   6968:     my ($where,$courseroles,$possroles) = @_;
                   6969:     return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
                   6970:     my ($dummy,$udom,$uname,$group) = split(/\//,$where);
                   6971:     unless (ref($courseroles->{$udom}) eq 'HASH') {
                   6972:         %{$courseroles->{$udom}} =
                   6973:             &get_my_roles('','','userroles',['active'],
                   6974:                           $possroles,[$udom],1);
                   6975:     }
                   6976:     if (ref($courseroles->{$udom}) eq 'HASH') {
                   6977:         foreach my $item (keys(%{$courseroles->{$udom}})) {
                   6978:             my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
                   6979:             my $area = '/'.$cdom.'/'.$cnum;
                   6980:             my $privkey = "user.priv.$crsrole.$area";
                   6981:             if ($crssec ne '') {
                   6982:                 $privkey .= '/'.$crssec;
                   6983:             }
                   6984:             $privkey .= ".$area/$group";
                   6985:             &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
                   6986:         }
                   6987:     }
                   6988:     return;
                   6989: }
                   6990: 
1.994     raeburn  6991: sub check_adhoc_privs {
1.1329    raeburn  6992:     my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller,$sec) = @_;
1.994     raeburn  6993:     my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
1.1329    raeburn  6994:     if ($sec) {
                   6995:         $cckey .= '/'.$sec;
                   6996:     } 
1.1185    raeburn  6997:     my $setprivs;
1.994     raeburn  6998:     if ($env{$cckey}) {
                   6999:         my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
1.1104    raeburn  7000:         &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
1.994     raeburn  7001:         unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
1.1329    raeburn  7002:             &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
1.1185    raeburn  7003:             $setprivs = 1;
1.994     raeburn  7004:         }
                   7005:     } else {
1.1330    raeburn  7006:         &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
1.1185    raeburn  7007:         $setprivs = 1;
1.994     raeburn  7008:     }
1.1185    raeburn  7009:     return $setprivs;
1.994     raeburn  7010: }
                   7011: 
                   7012: sub set_adhoc_privileges {
1.1326    raeburn  7013: # role can be cc, ca, or cr/<dom>/<dom>-domainconfig/role
1.1329    raeburn  7014:     my ($dcdom,$pickedcourse,$role,$caller,$sec) = @_;
1.994     raeburn  7015:     my $area = '/'.$dcdom.'/'.$pickedcourse;
1.1329    raeburn  7016:     if ($sec ne '') {
                   7017:         $area .= '/'.$sec;
                   7018:     }
1.994     raeburn  7019:     my $spec = $role.'.'.$area;
                   7020:     my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
1.1215    raeburn  7021:                                   $env{'user.name'},1);
1.1326    raeburn  7022:     my %rolehash = ();
1.1332    raeburn  7023:     if ($role =~ m{^\Qcr/$dcdom/$dcdom\E\-domainconfig/(\w+)$}) {
                   7024:         my $rolename = $1;
1.1326    raeburn  7025:         &custom_roleprivs(\%rolehash,$role,$dcdom,$pickedcourse,$spec,$area);
1.1332    raeburn  7026:         my %domdef = &get_domain_defaults($dcdom);
                   7027:         if (ref($domdef{'adhocroles'}) eq 'HASH') {
                   7028:             if (ref($domdef{'adhocroles'}{$rolename}) eq 'HASH') {
                   7029:                 &appenv({'request.role.desc' => $domdef{'adhocroles'}{$rolename}{'desc'},});
                   7030:             }
                   7031:         }
1.1326    raeburn  7032:     } else {
                   7033:         &standard_roleprivs(\%rolehash,$role,$dcdom,$spec,$pickedcourse,$area);
                   7034:     }
1.1341    raeburn  7035:     my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%rolehash);
1.994     raeburn  7036:     &appenv(\%userroles,[$role,'cm']);
1.1342    raeburn  7037:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
1.1402    raeburn  7038:     unless (($caller eq 'constructaccess' && $env{'request.course.id'}) ||
                   7039:             ($caller eq 'tiny')) {
1.1088    raeburn  7040:         &appenv( {'request.role'        => $spec,
                   7041:                   'request.role.domain' => $dcdom,
1.1332    raeburn  7042:                   'request.course.sec'  => $sec,
1.1088    raeburn  7043:                  }
                   7044:                );
                   7045:         my $tadv=0;
                   7046:         if (&allowed('adv') eq 'F') { $tadv=1; }
                   7047:         &appenv({'request.role.adv'    => $tadv});
                   7048:     }
1.994     raeburn  7049: }
                   7050: 
1.12      www      7051: # --------------------------------------------------------------- get interface
                   7052: 
                   7053: sub get {
1.131     albertel 7054:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      7055:    my $items='';
1.800     albertel 7056:    foreach my $item (@$storearr) {
                   7057:        $items.=&escape($item).'&';
1.191     harris41 7058:    }
1.12      www      7059:    $items=~s/\&$//;
1.620     albertel 7060:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7061:    if (!$uname) { $uname=$env{'user.name'}; }
1.131     albertel 7062:    my $uhome=&homeserver($uname,$udomain);
                   7063: 
1.133     albertel 7064:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      7065:    my @pairs=split(/\&/,$rep);
1.273     albertel 7066:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                   7067:      return @pairs;
                   7068:    }
1.15      www      7069:    my %returnhash=();
1.42      www      7070:    my $i=0;
1.800     albertel 7071:    foreach my $item (@$storearr) {
                   7072:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      7073:       $i++;
1.191     harris41 7074:    }
1.15      www      7075:    return %returnhash;
1.27      www      7076: }
                   7077: 
                   7078: # --------------------------------------------------------------- del interface
                   7079: 
                   7080: sub del {
1.133     albertel 7081:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.27      www      7082:    my $items='';
1.800     albertel 7083:    foreach my $item (@$storearr) {
                   7084:        $items.=&escape($item).'&';
1.191     harris41 7085:    }
1.984     neumanie 7086: 
1.27      www      7087:    $items=~s/\&$//;
1.620     albertel 7088:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7089:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 7090:    my $uhome=&homeserver($uname,$udomain);
                   7091:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      7092: }
                   7093: 
                   7094: # -------------------------------------------------------------- dump interface
                   7095: 
1.1180    droeschl 7096: sub unserialize {
                   7097:     my ($rep, $escapedkeys) = @_;
                   7098: 
                   7099:     return {} if $rep =~ /^error/;
                   7100: 
                   7101:     my %returnhash=();
1.1252    raeburn  7102: 	foreach my $item (split(/\&/,$rep)) {
1.1180    droeschl 7103: 	    my ($key, $value) = split(/=/, $item, 2);
                   7104: 	    $key = unescape($key) unless $escapedkeys;
                   7105: 	    next if $key =~ /^error: 2 /;
1.1252    raeburn  7106: 	    $returnhash{$key} = &thaw_unescape($value);
1.1180    droeschl 7107: 	}
                   7108:     #return %returnhash;
                   7109:     return \%returnhash;
                   7110: }        
                   7111: 
                   7112: # see Lond::dump_with_regexp
                   7113: # if $escapedkeys hash keys won't get unescaped.
1.15      www      7114: sub dump {
1.1180    droeschl 7115:     my ($namespace,$udomain,$uname,$regexp,$range,$escapedkeys)=@_;
1.755     albertel 7116:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7117:     if (!$uname) { $uname=$env{'user.name'}; }
                   7118:     my $uhome=&homeserver($uname,$udomain);
1.1167    droeschl 7119: 
1.1266    raeburn  7120:     if ($regexp) {
                   7121:         $regexp=&escape($regexp);
                   7122:     } else {
                   7123:         $regexp='.';
                   7124:     }
1.1180    droeschl 7125:     if (grep { $_ eq $uhome } current_machine_ids()) {
                   7126:         # user is hosted on this machine
1.1266    raeburn  7127:         my $reply = LONCAPA::Lond::dump_with_regexp(join(":", ($udomain,
1.1226    raeburn  7128:                     $uname, $namespace, $regexp, $range)), $perlvar{'lonVersion'});
1.1180    droeschl 7129:         return %{unserialize($reply, $escapedkeys)};
                   7130:     }
1.1166    raeburn  7131:     my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
1.755     albertel 7132:     my @pairs=split(/\&/,$rep);
                   7133:     my %returnhash=();
1.1098    foxr     7134:     if (!($rep =~ /^error/ )) {
                   7135: 	foreach my $item (@pairs) {
                   7136: 	    my ($key,$value)=split(/=/,$item,2);
1.1180    droeschl 7137:         $key = unescape($key) unless $escapedkeys;
                   7138:         #$key = &unescape($key);
1.1098    foxr     7139: 	    next if ($key =~ /^error: 2 /);
                   7140: 	    $returnhash{$key}=&thaw_unescape($value);
                   7141: 	}
1.755     albertel 7142:     }
                   7143:     return %returnhash;
1.407     www      7144: }
                   7145: 
1.1098    foxr     7146: 
1.717     albertel 7147: # --------------------------------------------------------- dumpstore interface
                   7148: 
                   7149: sub dumpstore {
                   7150:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.1180    droeschl 7151:    # same as dump but keys must be escaped. They may contain colon separated
                   7152:    # lists of values that may themself contain colons (e.g. symbs).
                   7153:    return &dump($namespace, $udomain, $uname, $regexp, $range, 1);
1.717     albertel 7154: }
                   7155: 
1.407     www      7156: # -------------------------------------------------------------- keys interface
                   7157: 
                   7158: sub getkeys {
                   7159:    my ($namespace,$udomain,$uname)=@_;
1.620     albertel 7160:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7161:    if (!$uname) { $uname=$env{'user.name'}; }
1.407     www      7162:    my $uhome=&homeserver($uname,$udomain);
                   7163:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
                   7164:    my @keyarray=();
1.800     albertel 7165:    foreach my $key (split(/\&/,$rep)) {
1.812     raeburn  7166:       next if ($key =~ /^error: 2 /);
1.800     albertel 7167:       push(@keyarray,&unescape($key));
1.407     www      7168:    }
                   7169:    return @keyarray;
1.318     matthew  7170: }
                   7171: 
1.319     matthew  7172: # --------------------------------------------------------------- currentdump
                   7173: sub currentdump {
1.328     matthew  7174:    my ($courseid,$sdom,$sname)=@_;
1.620     albertel 7175:    $courseid = $env{'request.course.id'} if (! defined($courseid));
                   7176:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
                   7177:    $sname    = $env{'user.name'}         if (! defined($sname));
1.326     matthew  7178:    my $uhome = &homeserver($sname,$sdom);
1.1180    droeschl 7179:    my $rep;
                   7180: 
                   7181:    if (grep { $_ eq $uhome } current_machine_ids()) {
                   7182:        $rep = LONCAPA::Lond::dump_profile_database(join(":", ($sdom, $sname, 
                   7183:                    $courseid)));
                   7184:    } else {
                   7185:        $rep = reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
                   7186:    }
                   7187: 
1.318     matthew  7188:    return if ($rep =~ /^(error:|no_such_host)/);
1.319     matthew  7189:    #
1.318     matthew  7190:    my %returnhash=();
1.319     matthew  7191:    #
1.1343    raeburn  7192:    if ($rep eq 'unknown_cmd') {
1.319     matthew  7193:        # an old lond will not know currentdump
                   7194:        # Do a dump and make it look like a currentdump
1.822     albertel 7195:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
1.319     matthew  7196:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
                   7197:        my %hash = @tmp;
                   7198:        @tmp=();
1.424     matthew  7199:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319     matthew  7200:    } else {
                   7201:        my @pairs=split(/\&/,$rep);
1.800     albertel 7202:        foreach my $pair (@pairs) {
                   7203:            my ($key,$value)=split(/=/,$pair,2);
1.319     matthew  7204:            my ($symb,$param) = split(/:/,$key);
                   7205:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
1.557     albertel 7206:                                                         &thaw_unescape($value);
1.319     matthew  7207:        }
1.191     harris41 7208:    }
1.12      www      7209:    return %returnhash;
1.424     matthew  7210: }
                   7211: 
                   7212: sub convert_dump_to_currentdump{
                   7213:     my %hash = %{shift()};
                   7214:     my %returnhash;
                   7215:     # Code ripped from lond, essentially.  The only difference
                   7216:     # here is the unescaping done by lonnet::dump().  Conceivably
                   7217:     # we might run in to problems with parameter names =~ /^v\./
                   7218:     while (my ($key,$value) = each(%hash)) {
                   7219:         my ($v,$symb,$param) = split(/:/,$key);
1.822     albertel 7220: 	$symb  = &unescape($symb);
                   7221: 	$param = &unescape($param);
1.424     matthew  7222:         next if ($v eq 'version' || $symb eq 'keys');
                   7223:         next if (exists($returnhash{$symb}) &&
                   7224:                  exists($returnhash{$symb}->{$param}) &&
                   7225:                  $returnhash{$symb}->{'v.'.$param} > $v);
                   7226:         $returnhash{$symb}->{$param}=$value;
                   7227:         $returnhash{$symb}->{'v.'.$param}=$v;
                   7228:     }
                   7229:     #
                   7230:     # Remove all of the keys in the hashes which keep track of
                   7231:     # the version of the parameter.
                   7232:     while (my ($symb,$param_hash) = each(%returnhash)) {
                   7233:         # use a foreach because we are going to delete from the hash.
                   7234:         foreach my $key (keys(%$param_hash)) {
                   7235:             delete($param_hash->{$key}) if ($key =~ /^v\./);
                   7236:         }
                   7237:     }
                   7238:     return \%returnhash;
1.12      www      7239: }
                   7240: 
1.627     albertel 7241: # ------------------------------------------------------ critical inc interface
                   7242: 
                   7243: sub cinc {
                   7244:     return &inc(@_,'critical');
                   7245: }
                   7246: 
1.449     matthew  7247: # --------------------------------------------------------------- inc interface
                   7248: 
                   7249: sub inc {
1.627     albertel 7250:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620     albertel 7251:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7252:     if (!$uname) { $uname=$env{'user.name'}; }
1.449     matthew  7253:     my $uhome=&homeserver($uname,$udomain);
                   7254:     my $items='';
                   7255:     if (! ref($store)) {
                   7256:         # got a single value, so use that instead
                   7257:         $items = &escape($store).'=&';
                   7258:     } elsif (ref($store) eq 'SCALAR') {
                   7259:         $items = &escape($$store).'=&';        
                   7260:     } elsif (ref($store) eq 'ARRAY') {
                   7261:         $items = join('=&',map {&escape($_);} @{$store});
                   7262:     } elsif (ref($store) eq 'HASH') {
                   7263:         while (my($key,$value) = each(%{$store})) {
                   7264:             $items.= &escape($key).'='.&escape($value).'&';
                   7265:         }
                   7266:     }
                   7267:     $items=~s/\&$//;
1.627     albertel 7268:     if ($critical) {
                   7269: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
                   7270:     } else {
                   7271: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
                   7272:     }
1.449     matthew  7273: }
                   7274: 
1.12      www      7275: # --------------------------------------------------------------- put interface
                   7276: 
                   7277: sub put {
1.134     albertel 7278:    my ($namespace,$storehash,$udomain,$uname)=@_;
1.620     albertel 7279:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7280:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 7281:    my $uhome=&homeserver($uname,$udomain);
1.12      www      7282:    my $items='';
1.800     albertel 7283:    foreach my $item (keys(%$storehash)) {
                   7284:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 7285:    }
1.12      www      7286:    $items=~s/\&$//;
1.134     albertel 7287:    return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.47      www      7288: }
                   7289: 
1.631     albertel 7290: # ------------------------------------------------------------ newput interface
                   7291: 
                   7292: sub newput {
                   7293:    my ($namespace,$storehash,$udomain,$uname)=@_;
                   7294:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7295:    if (!$uname) { $uname=$env{'user.name'}; }
                   7296:    my $uhome=&homeserver($uname,$udomain);
                   7297:    my $items='';
                   7298:    foreach my $key (keys(%$storehash)) {
                   7299:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
                   7300:    }
                   7301:    $items=~s/\&$//;
                   7302:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
                   7303: }
                   7304: 
                   7305: # ---------------------------------------------------------  putstore interface
                   7306: 
1.524     raeburn  7307: sub putstore {
1.1269    raeburn  7308:    my ($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog)=@_;
1.620     albertel 7309:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7310:    if (!$uname) { $uname=$env{'user.name'}; }
1.524     raeburn  7311:    my $uhome=&homeserver($uname,$udomain);
                   7312:    my $items='';
1.715     albertel 7313:    foreach my $key (keys(%$storehash)) {
                   7314:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524     raeburn  7315:    }
1.715     albertel 7316:    $items=~s/\&$//;
1.716     albertel 7317:    my $esc_symb=&escape($symb);
                   7318:    my $esc_v=&escape($version);
1.715     albertel 7319:    my $reply =
1.716     albertel 7320:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715     albertel 7321: 	      $uhome);
1.1269    raeburn  7322:    if (($tolog) && ($reply eq 'ok')) {
                   7323:        my $namevalue='';
                   7324:        foreach my $key (keys(%{$storehash})) {
                   7325:            $namevalue.=&escape($key).'='.&freeze_escape($storehash->{$key}).'&';
                   7326:        }
1.1434    raeburn  7327:        my $ip = &get_requestor_ip();
                   7328:        $namevalue .= 'ip='.&escape($ip).
1.1269    raeburn  7329:                      '&host='.&escape($perlvar{'lonHostID'}).
                   7330:                      '&version='.$esc_v.
                   7331:                      '&by='.&escape($env{'user.name'}.':'.$env{'user.domain'});
                   7332:        &Apache::lonnet::courselog($symb.':'.$uname.':'.$udomain.':PUTSTORE:'.$namevalue);
                   7333:    }
1.715     albertel 7334:    if ($reply eq 'unknown_cmd') {
1.716     albertel 7335:        # gfall back to way things use to be done
1.715     albertel 7336:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
                   7337: 			    $uname);
1.524     raeburn  7338:    }
1.715     albertel 7339:    return $reply;
                   7340: }
                   7341: 
                   7342: sub old_putstore {
1.716     albertel 7343:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
                   7344:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7345:     if (!$uname) { $uname=$env{'user.name'}; }
                   7346:     my $uhome=&homeserver($uname,$udomain);
                   7347:     my %newstorehash;
1.800     albertel 7348:     foreach my $item (keys(%$storehash)) {
                   7349: 	my $key = $version.':'.&escape($symb).':'.$item;
                   7350: 	$newstorehash{$key} = $storehash->{$item};
1.716     albertel 7351:     }
                   7352:     my $items='';
                   7353:     my %allitems = ();
1.800     albertel 7354:     foreach my $item (keys(%newstorehash)) {
                   7355: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
1.716     albertel 7356: 	    my $key = $1.':keys:'.$2;
                   7357: 	    $allitems{$key} .= $3.':';
                   7358: 	}
1.800     albertel 7359: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
1.716     albertel 7360:     }
1.800     albertel 7361:     foreach my $item (keys(%allitems)) {
                   7362: 	$allitems{$item} =~ s/\:$//;
                   7363: 	$items.= $item.'='.$allitems{$item}.'&';
1.716     albertel 7364:     }
                   7365:     $items=~s/\&$//;
                   7366:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524     raeburn  7367: }
                   7368: 
1.47      www      7369: # ------------------------------------------------------ critical put interface
                   7370: 
                   7371: sub cput {
1.134     albertel 7372:    my ($namespace,$storehash,$udomain,$uname)=@_;
1.620     albertel 7373:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7374:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 7375:    my $uhome=&homeserver($uname,$udomain);
1.47      www      7376:    my $items='';
1.800     albertel 7377:    foreach my $item (keys(%$storehash)) {
                   7378:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 7379:    }
1.47      www      7380:    $items=~s/\&$//;
1.134     albertel 7381:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      7382: }
                   7383: 
                   7384: # -------------------------------------------------------------- eget interface
                   7385: 
                   7386: sub eget {
1.133     albertel 7387:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      7388:    my $items='';
1.800     albertel 7389:    foreach my $item (@$storearr) {
                   7390:        $items.=&escape($item).'&';
1.191     harris41 7391:    }
1.12      www      7392:    $items=~s/\&$//;
1.620     albertel 7393:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7394:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 7395:    my $uhome=&homeserver($uname,$udomain);
                   7396:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      7397:    my @pairs=split(/\&/,$rep);
                   7398:    my %returnhash=();
1.42      www      7399:    my $i=0;
1.800     albertel 7400:    foreach my $item (@$storearr) {
                   7401:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      7402:       $i++;
1.191     harris41 7403:    }
1.12      www      7404:    return %returnhash;
                   7405: }
                   7406: 
1.667     albertel 7407: # ------------------------------------------------------------ tmpput interface
                   7408: sub tmpput {
1.802     raeburn  7409:     my ($storehash,$server,$context)=@_;
1.667     albertel 7410:     my $items='';
1.800     albertel 7411:     foreach my $item (keys(%$storehash)) {
                   7412: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.667     albertel 7413:     }
                   7414:     $items=~s/\&$//;
1.802     raeburn  7415:     if (defined($context)) {
                   7416:         $items .= ':'.&escape($context);
                   7417:     }
1.667     albertel 7418:     return &reply("tmpput:$items",$server);
                   7419: }
                   7420: 
                   7421: # ------------------------------------------------------------ tmpget interface
                   7422: sub tmpget {
1.688     albertel 7423:     my ($token,$server)=@_;
                   7424:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   7425:     my $rep=&reply("tmpget:$token",$server);
1.667     albertel 7426:     my %returnhash;
1.1328    raeburn  7427:     if ($rep =~ /^(con_lost|error|no_such_host)/i) {
                   7428:         return %returnhash;
                   7429:     }
1.667     albertel 7430:     foreach my $item (split(/\&/,$rep)) {
                   7431: 	my ($key,$value)=split(/=/,$item);
                   7432: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
                   7433:     }
                   7434:     return %returnhash;
                   7435: }
                   7436: 
1.1113    raeburn  7437: # ------------------------------------------------------------ tmpdel interface
1.688     albertel 7438: sub tmpdel {
                   7439:     my ($token,$server)=@_;
                   7440:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   7441:     return &reply("tmpdel:$token",$server);
                   7442: }
                   7443: 
1.1198    raeburn  7444: # ------------------------------------------------------------ get_timebased_id 
                   7445: 
                   7446: sub get_timebased_id {
                   7447:     my ($prefix,$keyid,$namespace,$cdom,$cnum,$idtype,$who,$locktries,
                   7448:         $maxtries) = @_;
                   7449:     my ($newid,$error,$dellock);
                   7450:     unless (($prefix =~ /^\w+$/) && ($keyid =~ /^\w+$/) && ($namespace ne '')) {  
                   7451:         return ('','ok','invalid call to get suffix');
                   7452:     }
                   7453: 
                   7454: # set defaults for any optional args for which values were not supplied
                   7455:     if ($who eq '') {
                   7456:         $who = $env{'user.name'}.':'.$env{'user.domain'};
                   7457:     }
                   7458:     if (!$locktries) {
                   7459:         $locktries = 3;
                   7460:     }
                   7461:     if (!$maxtries) {
                   7462:         $maxtries = 10;
                   7463:     }
                   7464:     
                   7465:     if (($cdom eq '') || ($cnum eq '')) {
                   7466:         if ($env{'request.course.id'}) {
                   7467:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   7468:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   7469:         }
                   7470:         if (($cdom eq '') || ($cnum eq '')) {
                   7471:             return ('','ok','call to get suffix not in course context');
                   7472:         }
                   7473:     }
                   7474: 
                   7475: # construct locking item
                   7476:     my $lockhash = {
                   7477:                       $prefix."\0".'locked_'.$keyid => $who,
                   7478:                    };
                   7479:     my $tries = 0;
                   7480: 
                   7481: # attempt to get lock on nohist_$namespace file
                   7482:     my $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
                   7483:     while (($gotlock ne 'ok') && $tries <$locktries) {
                   7484:         $tries ++;
                   7485:         sleep 1;
                   7486:         $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
                   7487:     }
                   7488: 
                   7489: # attempt to get unique identifier, based on current timestamp
                   7490:     if ($gotlock eq 'ok') {
                   7491:         my %inuse = &Apache::lonnet::dump('nohist_'.$namespace,$cdom,$cnum,$prefix);
                   7492:         my $id = time;
                   7493:         $newid = $id;
1.1268    raeburn  7494:         if ($idtype eq 'addcode') {
                   7495:             $newid .= &sixnum_code();
                   7496:         }
1.1198    raeburn  7497:         my $idtries = 0;
                   7498:         while (exists($inuse{$prefix."\0".$newid}) && $idtries < $maxtries) {
                   7499:             if ($idtype eq 'concat') {
                   7500:                 $newid = $id.$idtries;
1.1268    raeburn  7501:             } elsif ($idtype eq 'addcode') {
                   7502:                 $newid = $newid.&sixnum_code();
1.1198    raeburn  7503:             } else {
                   7504:                 $newid ++;
                   7505:             }
                   7506:             $idtries ++;
                   7507:         }
                   7508:         if (!exists($inuse{$prefix."\0".$newid})) {
                   7509:             my %new_item =  (
                   7510:                               $prefix."\0".$newid => $who,
                   7511:                             );
                   7512:             my $putresult = &Apache::lonnet::put('nohist_'.$namespace,\%new_item,
                   7513:                                                  $cdom,$cnum);
                   7514:             if ($putresult ne 'ok') {
                   7515:                 undef($newid);
                   7516:                 $error = 'error saving new item: '.$putresult;
                   7517:             }
                   7518:         } else {
1.1268    raeburn  7519:              undef($newid);
1.1198    raeburn  7520:              $error = ('error: no unique suffix available for the new item ');
                   7521:         }
                   7522: #  remove lock
                   7523:         my @del_lock = ($prefix."\0".'locked_'.$keyid);
                   7524:         $dellock = &Apache::lonnet::del('nohist_'.$namespace,\@del_lock,$cdom,$cnum);
                   7525:     } else {
                   7526:         $error = "error: could not obtain lockfile\n";
                   7527:         $dellock = 'ok';
1.1276    raeburn  7528:         if (($prefix eq 'paste') && ($namespace eq 'courseeditor') && ($keyid eq 'num')) {
                   7529:             $dellock = 'nolock';
                   7530:         }
1.1198    raeburn  7531:     }
                   7532:     return ($newid,$dellock,$error);
                   7533: }
                   7534: 
1.1268    raeburn  7535: sub sixnum_code {
                   7536:     my $code;
                   7537:     for (0..6) {
                   7538:         $code .= int( rand(9) );
                   7539:     }
                   7540:     return $code;
                   7541: }
                   7542: 
1.765     albertel 7543: # -------------------------------------------------- portfolio access checking
                   7544: 
                   7545: sub portfolio_access {
1.1270    raeburn  7546:     my ($requrl,$clientip) = @_;
1.765     albertel 7547:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
1.1270    raeburn  7548:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group,$clientip);
1.814     raeburn  7549:     if ($result) {
                   7550:         my %setters;
                   7551:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   7552:             my ($startblock,$endblock) =
                   7553:                 &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
                   7554:             if ($startblock && $endblock) {
                   7555:                 return 'B';
                   7556:             }
                   7557:         } else {
                   7558:             my ($startblock,$endblock) =
                   7559:                 &Apache::loncommon::blockcheck(\%setters,'port');
                   7560:             if ($startblock && $endblock) {
                   7561:                 return 'B';
                   7562:             }
                   7563:         }
                   7564:     }
1.765     albertel 7565:     if ($result eq 'ok') {
1.766     albertel 7566:        return 'F';
1.765     albertel 7567:     } elsif ($result =~ /^[^:]+:guest_/) {
1.766     albertel 7568:        return 'A';
1.765     albertel 7569:     }
1.766     albertel 7570:     return '';
1.765     albertel 7571: }
                   7572: 
                   7573: sub get_portfolio_access {
1.1270    raeburn  7574:     my ($udom,$unum,$file_name,$group,$clientip,$access_hash) = @_;
1.767     albertel 7575: 
                   7576:     if (!ref($access_hash)) {
                   7577: 	my $current_perms = &get_portfile_permissions($udom,$unum);
                   7578: 	my %access_controls = &get_access_controls($current_perms,$group,
                   7579: 						   $file_name);
                   7580: 	$access_hash = $access_controls{$file_name};
                   7581:     }
                   7582: 
1.1270    raeburn  7583:     my ($public,$guest,@domains,@users,@courses,@groups,@ips);
1.765     albertel 7584:     my $now = time;
                   7585:     if (ref($access_hash) eq 'HASH') {
                   7586:         foreach my $key (keys(%{$access_hash})) {
                   7587:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                   7588:             if ($start > $now) {
                   7589:                 next;
                   7590:             }
                   7591:             if ($end && $end<$now) {
                   7592:                 next;
                   7593:             }
                   7594:             if ($scope eq 'public') {
                   7595:                 $public = $key;
                   7596:                 last;
                   7597:             } elsif ($scope eq 'guest') {
                   7598:                 $guest = $key;
                   7599:             } elsif ($scope eq 'domains') {
                   7600:                 push(@domains,$key);
                   7601:             } elsif ($scope eq 'users') {
                   7602:                 push(@users,$key);
                   7603:             } elsif ($scope eq 'course') {
                   7604:                 push(@courses,$key);
                   7605:             } elsif ($scope eq 'group') {
                   7606:                 push(@groups,$key);
1.1270    raeburn  7607:             } elsif ($scope eq 'ip') {
                   7608:                 push(@ips,$key);
1.765     albertel 7609:             }
                   7610:         }
                   7611:         if ($public) {
                   7612:             return 'ok';
1.1270    raeburn  7613:         } elsif (@ips > 0) {
                   7614:             my $allowed;
                   7615:             foreach my $ipkey (@ips) {
                   7616:                 if (ref($access_hash->{$ipkey}{'ip'}) eq 'ARRAY') {
                   7617:                     if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$ipkey}{'ip'}}),$clientip)) {
                   7618:                         $allowed = 1;
                   7619:                         last; 
                   7620:                     }
                   7621:                 }
                   7622:             }
                   7623:             if ($allowed) {
                   7624:                 return 'ok';
                   7625:             }
1.765     albertel 7626:         }
                   7627:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   7628:             if ($guest) {
                   7629:                 return $guest;
                   7630:             }
                   7631:         } else {
                   7632:             if (@domains > 0) {
                   7633:                 foreach my $domkey (@domains) {
                   7634:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
                   7635:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
                   7636:                             return 'ok';
                   7637:                         }
                   7638:                     }
                   7639:                 }
                   7640:             }
                   7641:             if (@users > 0) {
                   7642:                 foreach my $userkey (@users) {
1.865     raeburn  7643:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
                   7644:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
                   7645:                             if (ref($item) eq 'HASH') {
                   7646:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
                   7647:                                     ($item->{'udom'} eq $env{'user.domain'})) {
                   7648:                                     return 'ok';
                   7649:                                 }
                   7650:                             }
                   7651:                         }
                   7652:                     } 
1.765     albertel 7653:                 }
                   7654:             }
                   7655:             my %roleshash;
                   7656:             my @courses_and_groups = @courses;
                   7657:             push(@courses_and_groups,@groups); 
                   7658:             if (@courses_and_groups > 0) {
                   7659:                 my (%allgroups,%allroles); 
                   7660:                 my ($start,$end,$role,$sec,$group);
                   7661:                 foreach my $envkey (%env) {
1.1060    raeburn  7662:                     if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 7663:                         my $cid = $2.'_'.$3; 
                   7664:                         if ($1 eq 'gr') {
                   7665:                             $group = $4;
                   7666:                             $allgroups{$cid}{$group} = $env{$envkey};
                   7667:                         } else {
                   7668:                             if ($4 eq '') {
                   7669:                                 $sec = 'none';
                   7670:                             } else {
                   7671:                                 $sec = $4;
                   7672:                             }
                   7673:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   7674:                         }
1.811     albertel 7675:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 7676:                         my $cid = $2.'_'.$3;
                   7677:                         if ($4 eq '') {
                   7678:                             $sec = 'none';
                   7679:                         } else {
                   7680:                             $sec = $4;
                   7681:                         }
                   7682:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   7683:                     }
                   7684:                 }
                   7685:                 if (keys(%allroles) == 0) {
                   7686:                     return;
                   7687:                 }
                   7688:                 foreach my $key (@courses_and_groups) {
                   7689:                     my %content = %{$$access_hash{$key}};
                   7690:                     my $cnum = $content{'number'};
                   7691:                     my $cdom = $content{'domain'};
                   7692:                     my $cid = $cdom.'_'.$cnum;
                   7693:                     if (!exists($allroles{$cid})) {
                   7694:                         next;
                   7695:                     }    
                   7696:                     foreach my $role_id (keys(%{$content{'roles'}})) {
                   7697:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
                   7698:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
                   7699:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
                   7700:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
                   7701:                         foreach my $role (keys(%{$allroles{$cid}})) {
                   7702:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
                   7703:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
                   7704:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
                   7705:                                         if (grep/^all$/,@sections) {
                   7706:                                             return 'ok';
                   7707:                                         } else {
                   7708:                                             if (grep/^$sec$/,@sections) {
                   7709:                                                 return 'ok';
                   7710:                                             }
                   7711:                                         }
                   7712:                                     }
                   7713:                                 }
                   7714:                                 if (keys(%{$allgroups{$cid}}) == 0) {
                   7715:                                     if (grep/^none$/,@groups) {
                   7716:                                         return 'ok';
                   7717:                                     }
                   7718:                                 } else {
                   7719:                                     if (grep/^all$/,@groups) {
                   7720:                                         return 'ok';
                   7721:                                     } 
                   7722:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
                   7723:                                         if (grep/^$group$/,@groups) {
                   7724:                                             return 'ok';
                   7725:                                         }
                   7726:                                     }
                   7727:                                 } 
                   7728:                             }
                   7729:                         }
                   7730:                     }
                   7731:                 }
                   7732:             }
                   7733:             if ($guest) {
                   7734:                 return $guest;
                   7735:             }
                   7736:         }
                   7737:     }
                   7738:     return;
                   7739: }
                   7740: 
                   7741: sub course_group_datechecker {
                   7742:     my ($dates,$now,$status) = @_;
                   7743:     my ($start,$end) = split(/\./,$dates);
                   7744:     if (!$start && !$end) {
                   7745:         return 'ok';
                   7746:     }
                   7747:     if (grep/^active$/,@{$status}) {
                   7748:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
                   7749:             return 'ok';
                   7750:         }
                   7751:     }
                   7752:     if (grep/^previous$/,@{$status}) {
                   7753:         if ($end > $now ) {
                   7754:             return 'ok';
                   7755:         }
                   7756:     }
                   7757:     if (grep/^future$/,@{$status}) {
                   7758:         if ($start > $now) {
                   7759:             return 'ok';
                   7760:         }
                   7761:     }
                   7762:     return; 
                   7763: }
                   7764: 
                   7765: sub parse_portfolio_url {
                   7766:     my ($url) = @_;
                   7767: 
                   7768:     my ($type,$udom,$unum,$group,$file_name);
                   7769:     
1.823     albertel 7770:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
1.765     albertel 7771: 	$type = 1;
                   7772:         $udom = $1;
                   7773:         $unum = $2;
                   7774:         $file_name = $3;
1.823     albertel 7775:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
1.765     albertel 7776: 	$type = 2;
                   7777:         $udom = $1;
                   7778:         $unum = $2;
                   7779:         $group = $3;
                   7780:         $file_name = $3.'/'.$4;
                   7781:     }
                   7782:     if (wantarray) {
                   7783: 	return ($type,$udom,$unum,$file_name,$group);
                   7784:     }
                   7785:     return $type;
                   7786: }
                   7787: 
                   7788: sub is_portfolio_url {
                   7789:     my ($url) = @_;
                   7790:     return scalar(&parse_portfolio_url($url));
                   7791: }
                   7792: 
1.798     raeburn  7793: sub is_portfolio_file {
                   7794:     my ($file) = @_;
1.820     raeburn  7795:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
1.798     raeburn  7796:         return 1;
                   7797:     }
                   7798:     return;
                   7799: }
                   7800: 
1.976     raeburn  7801: sub usertools_access {
1.1084    raeburn  7802:     my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
1.985     raeburn  7803:     my ($access,%tools);
                   7804:     if ($context eq '') {
                   7805:         $context = 'tools';
                   7806:     }
                   7807:     if ($context eq 'requestcourses') {
                   7808:         %tools = (
                   7809:                       official   => 1,
                   7810:                       unofficial => 1,
1.1006    raeburn  7811:                       community  => 1,
1.1246    raeburn  7812:                       textbook   => 1,
1.1305    raeburn  7813:                       placement  => 1,
1.1368    raeburn  7814:                       lti        => 1,
1.985     raeburn  7815:                  );
1.1183    raeburn  7816:     } elsif ($context eq 'requestauthor') {
                   7817:         %tools = (
                   7818:                       requestauthor => 1,
                   7819:                  );
1.985     raeburn  7820:     } else {
                   7821:         %tools = (
                   7822:                       aboutme   => 1,
                   7823:                       blog      => 1,
1.1177    raeburn  7824:                       webdav    => 1,
1.985     raeburn  7825:                       portfolio => 1,
                   7826:                  );
                   7827:     }
1.976     raeburn  7828:     return if (!defined($tools{$tool}));
                   7829: 
1.1242    raeburn  7830:     if (($udom eq '') || ($uname eq '')) {
1.976     raeburn  7831:         $udom = $env{'user.domain'};
                   7832:         $uname = $env{'user.name'};
                   7833:     }
                   7834: 
1.978     raeburn  7835:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
                   7836:         if ($action ne 'reload') {
1.985     raeburn  7837:             if ($context eq 'requestcourses') {
                   7838:                 return $env{'environment.canrequest.'.$tool};
1.1183    raeburn  7839:             } elsif ($context eq 'requestauthor') {
                   7840:                 return $env{'environment.canrequest.author'};
1.985     raeburn  7841:             } else {
                   7842:                 return $env{'environment.availabletools.'.$tool};
                   7843:             }
                   7844:         }
1.976     raeburn  7845:     }
                   7846: 
1.1183    raeburn  7847:     my ($toolstatus,$inststatus,$envkey);
                   7848:     if ($context eq 'requestauthor') {
                   7849:         $envkey = $context; 
                   7850:     } else {
                   7851:         $envkey = $context.'.'.$tool;
                   7852:     }
1.976     raeburn  7853: 
1.985     raeburn  7854:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
                   7855:          ($action ne 'reload')) {
1.1183    raeburn  7856:         $toolstatus = $env{'environment.'.$envkey};
1.976     raeburn  7857:         $inststatus = $env{'environment.inststatus'};
                   7858:     } else {
1.1084    raeburn  7859:         if (ref($userenvref) eq 'HASH') {
1.1183    raeburn  7860:             $toolstatus = $userenvref->{$envkey};
1.1084    raeburn  7861:             $inststatus = $userenvref->{'inststatus'};
                   7862:         } else {
1.1183    raeburn  7863:             my %userenv = &userenvironment($udom,$uname,$envkey,'inststatus');
                   7864:             $toolstatus = $userenv{$envkey};
1.1084    raeburn  7865:             $inststatus = $userenv{'inststatus'};
                   7866:         }
1.976     raeburn  7867:     }
                   7868: 
                   7869:     if ($toolstatus ne '') {
                   7870:         if ($toolstatus) {
                   7871:             $access = 1;
                   7872:         } else {
                   7873:             $access = 0;
                   7874:         }
                   7875:         return $access;
                   7876:     }
                   7877: 
1.1084    raeburn  7878:     my ($is_adv,%domdef);
                   7879:     if (ref($is_advref) eq 'HASH') {
                   7880:         $is_adv = $is_advref->{'is_adv'};
                   7881:     } else {
                   7882:         $is_adv = &is_advanced_user($udom,$uname);
                   7883:     }
                   7884:     if (ref($domdefref) eq 'HASH') {
                   7885:         %domdef = %{$domdefref};
                   7886:     } else {
                   7887:         %domdef = &get_domain_defaults($udom);
                   7888:     }
1.976     raeburn  7889:     if (ref($domdef{$tool}) eq 'HASH') {
                   7890:         if ($is_adv) {
                   7891:             if ($domdef{$tool}{'_LC_adv'} ne '') {
                   7892:                 if ($domdef{$tool}{'_LC_adv'}) { 
                   7893:                     $access = 1;
                   7894:                 } else {
                   7895:                     $access = 0;
                   7896:                 }
                   7897:                 return $access;
                   7898:             }
                   7899:         }
                   7900:         if ($inststatus ne '') {
                   7901:             my ($hasaccess,$hasnoaccess);
                   7902:             foreach my $affiliation (split(/:/,$inststatus)) {
                   7903:                 if ($domdef{$tool}{$affiliation} ne '') { 
                   7904:                     if ($domdef{$tool}{$affiliation}) {
                   7905:                         $hasaccess = 1;
                   7906:                     } else {
                   7907:                         $hasnoaccess = 1;
                   7908:                     }
                   7909:                 }
                   7910:             }
                   7911:             if ($hasaccess || $hasnoaccess) {
                   7912:                 if ($hasaccess) {
                   7913:                     $access = 1;
                   7914:                 } elsif ($hasnoaccess) {
                   7915:                     $access = 0; 
                   7916:                 }
                   7917:                 return $access;
                   7918:             }
                   7919:         } else {
                   7920:             if ($domdef{$tool}{'default'} ne '') {
                   7921:                 if ($domdef{$tool}{'default'}) {
                   7922:                     $access = 1;
                   7923:                 } elsif ($domdef{$tool}{'default'} == 0) {
                   7924:                     $access = 0;
                   7925:                 }
                   7926:                 return $access;
                   7927:             }
                   7928:         }
                   7929:     } else {
1.1177    raeburn  7930:         if (($context eq 'tools') && ($tool ne 'webdav')) {
1.985     raeburn  7931:             $access = 1;
                   7932:         } else {
                   7933:             $access = 0;
                   7934:         }
1.976     raeburn  7935:         return $access;
                   7936:     }
                   7937: }
                   7938: 
1.1050    raeburn  7939: sub is_course_owner {
                   7940:     my ($cdom,$cnum,$udom,$uname) = @_;
                   7941:     if (($udom eq '') || ($uname eq '')) {
                   7942:         $udom = $env{'user.domain'};
                   7943:         $uname = $env{'user.name'};
                   7944:     }
                   7945:     unless (($udom eq '') || ($uname eq '')) {
                   7946:         if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
                   7947:             if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
                   7948:                 return 1;
                   7949:             } else {
                   7950:                 my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
                   7951:                 if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
                   7952:                     return 1;
                   7953:                 }
                   7954:             }
                   7955:         }
                   7956:     }
                   7957:     return;
                   7958: }
                   7959: 
1.976     raeburn  7960: sub is_advanced_user {
                   7961:     my ($udom,$uname) = @_;
1.1085    raeburn  7962:     if ($udom ne '' && $uname ne '') {
                   7963:         if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
1.1128    raeburn  7964:             if (wantarray) {
                   7965:                 return ($env{'user.adv'},$env{'user.author'});
                   7966:             } else {
                   7967:                 return $env{'user.adv'};
                   7968:             }
1.1085    raeburn  7969:         }
                   7970:     }
1.976     raeburn  7971:     my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
                   7972:     my %allroles;
1.1128    raeburn  7973:     my ($is_adv,$is_author);
1.976     raeburn  7974:     foreach my $role (keys(%roleshash)) {
                   7975:         my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
                   7976:         my $area = '/'.$tdomain.'/'.$trest;
                   7977:         if ($sec ne '') {
                   7978:             $area .= '/'.$sec;
                   7979:         }
                   7980:         if (($area ne '') && ($trole ne '')) {
                   7981:             my $spec=$trole.'.'.$area;
                   7982:             if ($trole =~ /^cr\//) {
                   7983:                 &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
                   7984:             } elsif ($trole ne 'gr') {
                   7985:                 &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
                   7986:             }
1.1128    raeburn  7987:             if ($trole eq 'au') {
                   7988:                 $is_author = 1;
                   7989:             }
1.976     raeburn  7990:         }
                   7991:     }
                   7992:     foreach my $role (keys(%allroles)) {
                   7993:         last if ($is_adv);
                   7994:         foreach my $item (split(/:/,$allroles{$role})) {
                   7995:             if ($item ne '') {
                   7996:                 my ($privilege,$restrictions)=split(/&/,$item);
                   7997:                 if ($privilege eq 'adv') {
                   7998:                     $is_adv = 1;
                   7999:                     last;
                   8000:                 }
                   8001:             }
                   8002:         }
                   8003:     }
1.1128    raeburn  8004:     if (wantarray) {
                   8005:         return ($is_adv,$is_author);
                   8006:     }
1.976     raeburn  8007:     return $is_adv;
                   8008: }
1.798     raeburn  8009: 
1.1035    raeburn  8010: sub check_can_request {
1.1368    raeburn  8011:     my ($dom,$can_request,$request_domains,$uname,$udom) = @_;
1.1035    raeburn  8012:     my $canreq = 0;
1.1368    raeburn  8013:     if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
                   8014:         $uname = $env{'user.name'};
                   8015:         $udom = $env{'user.domain'};
                   8016:     }
1.1035    raeburn  8017:     my ($types,$typename) = &Apache::loncommon::course_types();
                   8018:     my @options = ('approval','validate','autolimit');
                   8019:     my $optregex = join('|',@options);
                   8020:     if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
                   8021:         foreach my $type (@{$types}) {
1.1368    raeburn  8022:             if (&usertools_access($uname,$udom,$type,undef,
                   8023:                                   'requestcourses')) {
1.1035    raeburn  8024:                 $canreq ++;
1.1036    raeburn  8025:                 if (ref($request_domains) eq 'HASH') {
1.1368    raeburn  8026:                     push(@{$request_domains->{$type}},$udom);
1.1036    raeburn  8027:                 }
1.1368    raeburn  8028:                 if ($dom eq $udom) {
1.1035    raeburn  8029:                     $can_request->{$type} = 1;
                   8030:                 }
                   8031:             }
1.1368    raeburn  8032:             if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '') &&
                   8033:                 ($env{'environment.reqcrsotherdom.'.$type} ne '')) {
1.1035    raeburn  8034:                 my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
                   8035:                 if (@curr > 0) {
1.1036    raeburn  8036:                     foreach my $item (@curr) {
                   8037:                         if (ref($request_domains) eq 'HASH') {
                   8038:                             my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
                   8039:                             if ($otherdom ne '') {
                   8040:                                 if (ref($request_domains->{$type}) eq 'ARRAY') {
                   8041:                                     unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
                   8042:                                         push(@{$request_domains->{$type}},$otherdom);
                   8043:                                     }
                   8044:                                 } else {
                   8045:                                     push(@{$request_domains->{$type}},$otherdom);
                   8046:                                 }
                   8047:                             }
                   8048:                         }
                   8049:                     }
1.1368    raeburn  8050:                     unless ($dom eq $env{'user.domain'}) {
1.1036    raeburn  8051:                         $canreq ++;
1.1035    raeburn  8052:                         if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
                   8053:                             $can_request->{$type} = 1;
                   8054:                         }
                   8055:                     }
                   8056:                 }
                   8057:             }
                   8058:         }
                   8059:     }
                   8060:     return $canreq;
                   8061: }
                   8062: 
1.341     www      8063: # ---------------------------------------------- Custom access rule evaluation
                   8064: 
                   8065: sub customaccess {
                   8066:     my ($priv,$uri)=@_;
1.807     albertel 8067:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819     www      8068:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807     albertel 8069:     $udom = &LONCAPA::clean_domain($udom);
                   8070:     $ucrs = &LONCAPA::clean_username($ucrs);
1.341     www      8071:     my $access=0;
1.800     albertel 8072:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.893     albertel 8073: 	my ($effect,$realm,$role,$type)=split(/\:/,$right);
                   8074: 	if ($type eq 'user') {
                   8075: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
1.896     albertel 8076: 		my ($tdom,$tuname)=split(m{/},$scope);
1.893     albertel 8077: 		if ($tdom) {
                   8078: 		    if ($tdom ne $env{'user.domain'}) { next; }
                   8079: 		}
1.896     albertel 8080: 		if ($tuname) {
                   8081: 		    if ($tuname ne $env{'user.name'}) { next; }
1.893     albertel 8082: 		}
                   8083: 		$access=($effect eq 'allow');
                   8084: 		last;
                   8085: 	    }
                   8086: 	} else {
                   8087: 	    if ($role) {
                   8088: 		if ($role ne $urole) { next; }
                   8089: 	    }
                   8090: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
                   8091: 		my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
                   8092: 		if ($tdom) {
                   8093: 		    if ($tdom ne $udom) { next; }
                   8094: 		}
                   8095: 		if ($tcrs) {
                   8096: 		    if ($tcrs ne $ucrs) { next; }
                   8097: 		}
                   8098: 		if ($tsec) {
                   8099: 		    if ($tsec ne $usec) { next; }
                   8100: 		}
                   8101: 		$access=($effect eq 'allow');
                   8102: 		last;
                   8103: 	    }
                   8104: 	    if ($realm eq '' && $role eq '') {
                   8105: 		$access=($effect eq 'allow');
                   8106: 	    }
1.402     bowersj2 8107: 	}
1.341     www      8108:     }
                   8109:     return $access;
                   8110: }
                   8111: 
1.103     harris41 8112: # ------------------------------------------------- Check for a user privilege
1.12      www      8113: 
                   8114: sub allowed {
1.1424    raeburn  8115:     my ($priv,$uri,$symb,$role,$clientip,$noblockcheck,$ignorecache)=@_;
1.705     albertel 8116:     my $ver_orguri=$uri;
1.439     www      8117:     $uri=&deversion($uri);
1.152     www      8118:     my $orguri=$uri;
1.52      www      8119:     $uri=&declutter($uri);
1.809     raeburn  8120: 
1.810     raeburn  8121:     if ($priv eq 'evb') {
                   8122: # Evade communication block restrictions for specified role in a course
                   8123:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
                   8124:             return $1;
                   8125:         } else {
                   8126:             return;
                   8127:         }
                   8128:     }
                   8129: 
1.620     albertel 8130:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54      www      8131: # Free bre access to adm and meta resources
1.1436    raeburn  8132:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard|viewclasslist|aboutme|ext\.tool)$})) 
1.769     albertel 8133: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
                   8134: 	&& ($priv eq 'bre')) {
1.14      www      8135: 	return 'F';
1.159     www      8136:     }
                   8137: 
1.545     banghart 8138: # Free bre access to user's own portfolio contents
1.714     raeburn  8139:     my ($space,$domain,$name,@dir)=split('/',$uri);
1.647     raeburn  8140:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
1.714     raeburn  8141: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814     raeburn  8142:         my %setters;
                   8143:         my ($startblock,$endblock) = 
                   8144:             &Apache::loncommon::blockcheck(\%setters,'port');
                   8145:         if ($startblock && $endblock) {
                   8146:             return 'B';
                   8147:         } else {
                   8148:             return 'F';
                   8149:         }
1.545     banghart 8150:     }
                   8151: 
1.762     raeburn  8152: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714     raeburn  8153:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
                   8154:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
                   8155:         if (exists($env{'request.course.id'})) {
                   8156:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   8157:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   8158:             if (($domain eq $cdom) && ($name eq $cnum)) {
                   8159:                 my $courseprivid=$env{'request.course.id'};
                   8160:                 $courseprivid=~s/\_/\//;
                   8161:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
                   8162:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
                   8163:                     return $1; 
1.762     raeburn  8164:                 } else {
                   8165:                     if ($env{'request.course.sec'}) {
                   8166:                         $courseprivid.='/'.$env{'request.course.sec'};
                   8167:                     }
                   8168:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
                   8169:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
                   8170:                         return $2;
                   8171:                     }
1.714     raeburn  8172:                 }
                   8173:             }
                   8174:         }
                   8175:     }
                   8176: 
1.159     www      8177: # Free bre to public access
                   8178: 
                   8179:     if ($priv eq 'bre') {
1.1362    raeburn  8180:         my $copyright;
                   8181:         unless ($uri =~ /ext\.tool/) {
                   8182:             $copyright=&metadata($uri,'copyright');
                   8183:         }
1.620     albertel 8184: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
1.301     www      8185:            return 'F'; 
                   8186:         }
1.238     www      8187:         if ($copyright eq 'priv') {
                   8188:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 8189: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238     www      8190: 		return '';
                   8191:             }
                   8192:         }
                   8193:         if ($copyright eq 'domain') {
                   8194:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 8195: 	    unless (($env{'user.domain'} eq $1) ||
                   8196:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238     www      8197: 		return '';
                   8198:             }
1.262     matthew  8199:         }
1.620     albertel 8200:         if ($env{'request.role'}=~ /li\.\//) {
1.262     matthew  8201:             # Library role, so allow browsing of resources in this domain.
                   8202:             return 'F';
1.238     www      8203:         }
1.341     www      8204:         if ($copyright eq 'custom') {
                   8205: 	    unless (&customaccess($priv,$uri)) { return ''; }
                   8206:         }
1.14      www      8207:     }
1.264     matthew  8208:     # Domain coordinator is trying to create a course
1.620     albertel 8209:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264     matthew  8210:         # uri is the requested domain in this case.
                   8211:         # comparison to 'request.role.domain' shows if the user has selected
1.678     raeburn  8212:         # a role of dc for the domain in question.
1.620     albertel 8213:         return 'F' if ($uri eq $env{'request.role.domain'});
1.264     matthew  8214:     }
1.29      www      8215: 
1.52      www      8216:     my $thisallowed='';
                   8217:     my $statecond=0;
                   8218:     my $courseprivid='';
                   8219: 
1.1039    raeburn  8220:     my $ownaccess;
1.1043    raeburn  8221:     # Community Coordinator or Assistant Co-author browsing resource space.
1.1039    raeburn  8222:     if (($priv eq 'bro') && ($env{'user.author'})) {
                   8223:         if ($uri eq '') {
                   8224:             $ownaccess = 1;
                   8225:         } else {
                   8226:             if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
                   8227:                 my $udom = $env{'user.domain'};
                   8228:                 my $uname = $env{'user.name'};
                   8229:                 if ($uri =~ m{^\Q$udom\E/?$}) {
                   8230:                     $ownaccess = 1;
1.1040    raeburn  8231:                 } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
1.1039    raeburn  8232:                     unless ($uri =~ m{\.\./}) {
                   8233:                         $ownaccess = 1;
                   8234:                     }
                   8235:                 } elsif (($udom ne 'public') && ($uname ne 'public')) {
                   8236:                     my $now = time;
                   8237:                     if ($uri =~ m{^([^/]+)/?$}) {
                   8238:                         my $adom = $1;
                   8239:                         foreach my $key (keys(%env)) {
1.1042    raeburn  8240:                             if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
1.1039    raeburn  8241:                                 my ($start,$end) = split('.',$env{$key});
                   8242:                                 if (($now >= $start) && (!$end || $end < $now)) {
                   8243:                                     $ownaccess = 1;
                   8244:                                     last;
                   8245:                                 }
                   8246:                             }
                   8247:                         }
                   8248:                     } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
                   8249:                         my $adom = $1;
                   8250:                         my $aname = $2;
1.1042    raeburn  8251:                         foreach my $role ('ca','aa') { 
                   8252:                             if ($env{"user.role.$role./$adom/$aname"}) {
                   8253:                                 my ($start,$end) =
                   8254:                                     split('.',$env{"user.role.$role./$adom/$aname"});
                   8255:                                 if (($now >= $start) && (!$end || $end < $now)) {
                   8256:                                     $ownaccess = 1;
                   8257:                                     last;
                   8258:                                 }
1.1039    raeburn  8259:                             }
                   8260:                         }
                   8261:                     }
                   8262:                 }
                   8263:             }
                   8264:         }
                   8265:     }
                   8266: 
1.52      www      8267: # Course
                   8268: 
1.620     albertel 8269:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.1043    raeburn  8270:         unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039    raeburn  8271:             $thisallowed.=$1;
                   8272:         }
1.52      www      8273:     }
1.29      www      8274: 
1.52      www      8275: # Domain
                   8276: 
1.620     albertel 8277:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479     albertel 8278:        =~/\Q$priv\E\&([^\:]*)/) {
1.1043    raeburn  8279:         unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039    raeburn  8280:             $thisallowed.=$1;
                   8281:         }
1.12      www      8282:     }
1.52      www      8283: 
1.1141    raeburn  8284: # User who is not author or co-author might still be able to edit
                   8285: # resource of an author in the domain (e.g., if Domain Coordinator).
                   8286:     if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
                   8287:         (&allowed('mdc',$env{'request.course.id'}))) {
                   8288:         if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
                   8289:             $thisallowed.=$1;
                   8290:         }
                   8291:     }
                   8292: 
1.52      www      8293: # Course: uri itself is a course
1.66      www      8294:     my $courseuri=$uri;
                   8295:     $courseuri=~s/\_(\d)/\/$1/;
1.83      www      8296:     $courseuri=~s/^([^\/])/\/$1/;
1.81      www      8297: 
1.620     albertel 8298:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479     albertel 8299:        =~/\Q$priv\E\&([^\:]*)/) {
1.1409    raeburn  8300:         if ($priv eq 'mip') {
                   8301:             my $rem = $1;
                   8302:             if (($uri ne '') && ($env{'request.course.id'} eq $uri) &&
                   8303:                 ($env{'course.'.$env{'request.course.id'}.'.internal.courseowner'} eq $env{'user.name'}.':'.$env{'user.domain'})) {
                   8304:                 my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   8305:                 if ($cdom ne '') {
1.1410    raeburn  8306:                     my %passwdconf = &get_passwdconf($cdom);
                   8307:                     if (ref($passwdconf{'crsownerchg'}) eq 'HASH') {
                   8308:                         if (ref($passwdconf{'crsownerchg'}{'by'}) eq 'ARRAY') {
                   8309:                             if (@{$passwdconf{'crsownerchg'}{'by'}}) {
                   8310:                                 my @inststatuses = split(':',$env{'environment.inststatus'});
                   8311:                                 unless (@inststatuses) {
                   8312:                                     @inststatuses = ('default');
                   8313:                                 }
                   8314:                                 foreach my $status (@inststatuses) {
                   8315:                                     if (grep(/^\Q$status\E$/,@{$passwdconf{'crsownerchg'}{'by'}})) {
                   8316:                                         $thisallowed.=$rem;
                   8317:                                     }
                   8318:                                 }
                   8319:                             }
                   8320:                         }
1.1409    raeburn  8321:                     }
                   8322:                 }
                   8323:             }
                   8324:         } else {
                   8325:             unless (($priv eq 'bro') && (!$ownaccess)) {
                   8326:                 $thisallowed.=$1;
                   8327:             }
1.1039    raeburn  8328:         }
1.12      www      8329:     }
1.29      www      8330: 
1.665     albertel 8331: # URI is an uploaded document for this course, default permissions don't matter
1.611     albertel 8332: # not allowing 'edit' access (editupload) to uploaded course docs
1.492     albertel 8333:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665     albertel 8334: 	$thisallowed='';
1.671     raeburn  8335:         my ($match)=&is_on_map($uri);
                   8336:         if ($match) {
                   8337:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
                   8338:                   =~/\Q$priv\E\&([^\:]*)/) {
1.1281    raeburn  8339:                 my $value = $1;
1.1402    raeburn  8340:                 my $deeplinkblock = &deeplink_check($priv,$symb,$uri);
                   8341:                 if ($deeplinkblock) {
                   8342:                     $thisallowed='D';
                   8343:                 } elsif ($noblockcheck) {
1.1281    raeburn  8344:                     $thisallowed.=$value;
1.1162    raeburn  8345:                 } else {
1.1424    raeburn  8346:                     my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
1.1281    raeburn  8347:                     if (@blockers > 0) {
                   8348:                         $thisallowed = 'B';
                   8349:                     } else {
                   8350:                         $thisallowed.=$value;
                   8351:                     }
1.1162    raeburn  8352:                 }
1.671     raeburn  8353:             }
                   8354:         } else {
1.705     albertel 8355:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671     raeburn  8356:             if ($refuri) {
                   8357:                 if ($refuri =~ m|^/adm/|) {
1.669     raeburn  8358:                     $thisallowed='F';
1.671     raeburn  8359:                 } else {
                   8360:                     $refuri=&declutter($refuri);
                   8361:                     my ($match) = &is_on_map($refuri);
                   8362:                     if ($match) {
1.1402    raeburn  8363:                         my $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
                   8364:                         if ($deeplinkblock) {
                   8365:                             $thisallowed='D';
                   8366:                         } elsif ($noblockcheck) {
1.1281    raeburn  8367:                             $thisallowed='F';
1.1162    raeburn  8368:                         } else {
1.1426    raeburn  8369:                             my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
1.1281    raeburn  8370:                             if (@blockers > 0) {
                   8371:                                 $thisallowed = 'B';
                   8372:                             } else {
                   8373:                                 $thisallowed='F';
                   8374:                             }
1.1162    raeburn  8375:                         }
1.671     raeburn  8376:                     }
1.669     raeburn  8377:                 }
1.671     raeburn  8378:             }
                   8379:         }
1.314     www      8380:     }
1.492     albertel 8381: 
1.766     albertel 8382:     if ($priv eq 'bre'
                   8383: 	&& $thisallowed ne 'F' 
                   8384: 	&& $thisallowed ne '2'
                   8385: 	&& &is_portfolio_url($uri)) {
1.1270    raeburn  8386: 	$thisallowed = &portfolio_access($uri,$clientip);
1.766     albertel 8387:     }
1.1250    raeburn  8388: 
1.52      www      8389: # Full access at system, domain or course-wide level? Exit.
1.29      www      8390:     if ($thisallowed=~/F/) {
                   8391: 	return 'F';
                   8392:     }
                   8393: 
1.52      www      8394: # If this is generating or modifying users, exit with special codes
1.29      www      8395: 
1.643     www      8396:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
                   8397: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642     albertel 8398: 	    my ($audom,$auname)=split('/',$uri);
1.643     www      8399: # no author name given, so this just checks on the general right to make a co-author in this domain
                   8400: 	    unless ($auname) { return $thisallowed; }
                   8401: # an author name is given, so we are about to actually make a co-author for a certain account
1.642     albertel 8402: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
                   8403: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
                   8404: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
                   8405: 	}
1.52      www      8406: 	return $thisallowed;
                   8407:     }
                   8408: #
1.103     harris41 8409: # Gathered so far: system, domain and course wide privileges
1.52      www      8410: #
                   8411: # Course: See if uri or referer is an individual resource that is part of 
                   8412: # the course
                   8413: 
1.620     albertel 8414:     if ($env{'request.course.id'}) {
1.232     www      8415: 
1.1409    raeburn  8416: # If this is modifying password (internal auth) domains must match for user and user's role.
                   8417: 
                   8418:         if ($priv eq 'mip') {
                   8419:             if ($env{'user.domain'} eq $env{'request.role.domain'}) {
                   8420:                 return $thisallowed;
                   8421:             } else {
                   8422:                 return '';
                   8423:             }
                   8424:         }
                   8425: 
1.620     albertel 8426:        $courseprivid=$env{'request.course.id'};
                   8427:        if ($env{'request.course.sec'}) {
                   8428:           $courseprivid.='/'.$env{'request.course.sec'};
1.52      www      8429:        }
                   8430:        $courseprivid=~s/\_/\//;
                   8431:        my $checkreferer=1;
1.232     www      8432:        my ($match,$cond)=&is_on_map($uri);
                   8433:        if ($match) {
                   8434:            $statecond=$cond;
1.620     albertel 8435:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 8436:                =~/\Q$priv\E\&([^\:]*)/) {
1.1162    raeburn  8437:                my $value = $1;
                   8438:                if ($priv eq 'bre') {
1.1281    raeburn  8439:                    if ($noblockcheck) {
                   8440:                        $thisallowed.=$value;
1.1162    raeburn  8441:                    } else {
1.1424    raeburn  8442:                        my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
1.1281    raeburn  8443:                        if (@blockers > 0) {
                   8444:                            $thisallowed = 'B';
                   8445:                        } else {
                   8446:                            $thisallowed.=$value;
                   8447:                        }
1.1162    raeburn  8448:                    }
                   8449:                } else {
                   8450:                    $thisallowed.=$value;
                   8451:                }
1.52      www      8452:                $checkreferer=0;
                   8453:            }
1.29      www      8454:        }
1.1424    raeburn  8455: 
1.148     www      8456:        if ($checkreferer) {
1.620     albertel 8457: 	  my $refuri=$env{'httpref.'.$orguri};
1.148     www      8458:             unless ($refuri) {
1.800     albertel 8459:                 foreach my $key (keys(%env)) {
                   8460: 		    if ($key=~/^httpref\..*\*/) {
                   8461: 			my $pattern=$key;
1.156     www      8462:                         $pattern=~s/^httpref\.\/res\///;
1.148     www      8463:                         $pattern=~s/\*/\[\^\/\]\+/g;
                   8464:                         $pattern=~s/\//\\\//g;
1.152     www      8465:                         if ($orguri=~/$pattern/) {
1.800     albertel 8466: 			    $refuri=$env{$key};
1.148     www      8467:                         }
                   8468:                     }
1.191     harris41 8469:                 }
1.148     www      8470:             }
1.232     www      8471: 
1.148     www      8472:          if ($refuri) { 
1.152     www      8473: 	  $refuri=&declutter($refuri);
1.232     www      8474:           my ($match,$cond)=&is_on_map($refuri);
                   8475:             if ($match) {
                   8476:               my $refstatecond=$cond;
1.620     albertel 8477:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 8478:                   =~/\Q$priv\E\&([^\:]*)/) {
1.1162    raeburn  8479:                   my $value = $1;
                   8480:                   if ($priv eq 'bre') {
1.1402    raeburn  8481:                       my $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
                   8482:                       if ($deeplinkblock) {
                   8483:                           $thisallowed = 'D';
                   8484:                       } elsif ($noblockcheck) {
1.1281    raeburn  8485:                           $thisallowed.=$value;
1.1162    raeburn  8486:                       } else {
1.1426    raeburn  8487:                           my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
1.1281    raeburn  8488:                           if (@blockers > 0) {
                   8489:                               $thisallowed = 'B';
                   8490:                           } else {
                   8491:                               $thisallowed.=$value;
                   8492:                           }
1.1162    raeburn  8493:                       }
                   8494:                   } else {
                   8495:                       $thisallowed.=$value;
                   8496:                   }
1.53      www      8497:                   $uri=$refuri;
                   8498:                   $statecond=$refstatecond;
1.52      www      8499:               }
                   8500:           }
1.148     www      8501:         }
1.29      www      8502:        }
1.52      www      8503:    }
1.29      www      8504: 
1.52      www      8505: #
1.103     harris41 8506: # Gathered now: all privileges that could apply, and condition number
1.52      www      8507: # 
                   8508: #
                   8509: # Full or no access?
                   8510: #
1.29      www      8511: 
1.52      www      8512:     if ($thisallowed=~/F/) {
                   8513: 	return 'F';
                   8514:     }
1.29      www      8515: 
1.52      www      8516:     unless ($thisallowed) {
                   8517:         return '';
                   8518:     }
1.29      www      8519: 
1.52      www      8520: # Restrictions exist, deal with them
                   8521: #
                   8522: #   C:according to course preferences
                   8523: #   R:according to resource settings
                   8524: #   L:unless locked
                   8525: #   X:according to user session state
                   8526: #
                   8527: 
                   8528: # Possibly locked functionality, check all courses
1.54      www      8529: # Locks might take effect only after 10 minutes cache expiration for other
                   8530: # courses, and 2 minutes for current course
1.52      www      8531: 
                   8532:     my $envkey;
                   8533:     if ($thisallowed=~/L/) {
1.1000    raeburn  8534:         foreach $envkey (keys(%env)) {
1.54      www      8535:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
                   8536:                my $courseid=$2;
                   8537:                my $roleid=$1.'.'.$2;
1.92      www      8538:                $courseid=~s/^\///;
1.54      www      8539:                my $expiretime=600;
1.620     albertel 8540:                if ($env{'request.role'} eq $roleid) {
1.54      www      8541: 		  $expiretime=120;
                   8542:                }
                   8543: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
                   8544:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620     albertel 8545:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731     albertel 8546: 		   &coursedescription($courseid,{'freshen_cache' => 1});
1.54      www      8547:                }
1.620     albertel 8548:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   8549:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
                   8550: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
                   8551:                        &log($env{'user.domain'},$env{'user.name'},
                   8552:                             $env{'user.home'},
1.57      www      8553:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52      www      8554:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 8555:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      8556: 		       return '';
                   8557:                    }
                   8558:                }
1.620     albertel 8559:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   8560:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
                   8561: 		   if ($env{'priv.'.$priv.'.lock.expire'}>time) {
                   8562:                        &log($env{'user.domain'},$env{'user.name'},
                   8563:                             $env{'user.home'},
1.57      www      8564:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52      www      8565:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 8566:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      8567: 		       return '';
                   8568:                    }
                   8569:                }
                   8570: 	   }
1.29      www      8571:        }
1.52      www      8572:     }
1.1424    raeburn  8573: 
1.52      www      8574: #
                   8575: # Rest of the restrictions depend on selected course
                   8576: #
                   8577: 
1.620     albertel 8578:     unless ($env{'request.course.id'}) {
1.766     albertel 8579: 	if ($thisallowed eq 'A') {
                   8580: 	    return 'A';
1.814     raeburn  8581:         } elsif ($thisallowed eq 'B') {
                   8582:             return 'B';
1.766     albertel 8583: 	} else {
                   8584: 	    return '1';
                   8585: 	}
1.52      www      8586:     }
1.29      www      8587: 
1.52      www      8588: #
                   8589: # Now user is definitely in a course
                   8590: #
1.53      www      8591: 
                   8592: 
                   8593: # Course preferences
                   8594: 
                   8595:    if ($thisallowed=~/C/) {
1.620     albertel 8596:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
                   8597:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
                   8598:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479     albertel 8599: 	   =~/\Q$rolecode\E/) {
1.1304    raeburn  8600: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
1.689     albertel 8601: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   8602: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
                   8603: 			$env{'request.course.id'});
                   8604: 	   }
1.237     www      8605:            return '';
                   8606:        }
                   8607: 
1.620     albertel 8608:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479     albertel 8609: 	   =~/\Q$unamedom\E/) {
1.1304    raeburn  8610: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
1.689     albertel 8611: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
                   8612: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
                   8613: 			$env{'request.course.id'});
                   8614: 	   }
1.54      www      8615:            return '';
                   8616:        }
1.53      www      8617:    }
                   8618: 
                   8619: # Resource preferences
                   8620: 
                   8621:    if ($thisallowed=~/R/) {
1.620     albertel 8622:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479     albertel 8623:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.1103    raeburn  8624: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
1.689     albertel 8625: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   8626: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
                   8627: 	   }
                   8628: 	   return '';
1.54      www      8629:        }
1.53      www      8630:    }
1.30      www      8631: 
1.246     www      8632: # Restricted by state or randomout?
1.30      www      8633: 
1.52      www      8634:    if ($thisallowed=~/X/) {
1.620     albertel 8635:       if ($env{'acc.randomout'}) {
1.579     albertel 8636: 	 if (!$symb) { $symb=&symbread($uri,1); }
1.620     albertel 8637:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
1.248     www      8638:             return ''; 
                   8639:          }
1.247     www      8640:       }
                   8641:       if (&condval($statecond)) {
1.52      www      8642: 	 return '2';
                   8643:       } else {
                   8644:          return '';
                   8645:       }
                   8646:    }
1.30      www      8647: 
1.766     albertel 8648:     if ($thisallowed eq 'A') {
                   8649: 	return 'A';
1.814     raeburn  8650:     } elsif ($thisallowed eq 'B') {
                   8651:         return 'B';
1.1402    raeburn  8652:     } elsif ($thisallowed eq 'D') {
                   8653:         return 'D';
1.766     albertel 8654:     }
1.52      www      8655:    return 'F';
1.232     www      8656: }
1.1162    raeburn  8657: 
1.1192    raeburn  8658: # ------------------------------------------- Check construction space access
                   8659: 
                   8660: sub constructaccess {
                   8661:     my ($url,$setpriv)=@_;
                   8662: 
                   8663: # We do not allow editing of previous versions of files
                   8664:     if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
                   8665: 
                   8666: # Get username and domain from URL
                   8667:     my ($ownername,$ownerdomain,$ownerhome);
                   8668: 
                   8669:     ($ownerdomain,$ownername) =
1.1325    raeburn  8670:         ($url=~ m{^(?:\Q$perlvar{'lonDocRoot'}\E|)(?:/daxepage|/daxeopen)?/priv/($match_domain)/($match_username)(?:/|$)});
1.1192    raeburn  8671: 
                   8672: # The URL does not really point to any authorspace, forget it
                   8673:     unless (($ownername) && ($ownerdomain)) { return ''; }
                   8674: 
                   8675: # Now we need to see if the user has access to the authorspace of
                   8676: # $ownername at $ownerdomain
                   8677: 
                   8678:     if (($ownername eq $env{'user.name'}) && ($ownerdomain eq $env{'user.domain'})) {
                   8679: # Real author for this?
                   8680:        $ownerhome = $env{'user.home'};
                   8681:        if (exists($env{'user.priv.au./'.$ownerdomain.'/./'})) {
                   8682:           return ($ownername,$ownerdomain,$ownerhome);
                   8683:        }
                   8684:     } else {
                   8685: # Co-author for this?
                   8686:         if (exists($env{'user.priv.ca./'.$ownerdomain.'/'.$ownername.'./'}) ||
                   8687:             exists($env{'user.priv.aa./'.$ownerdomain.'/'.$ownername.'./'}) ) {
                   8688:             $ownerhome = &homeserver($ownername,$ownerdomain);
                   8689:             return ($ownername,$ownerdomain,$ownerhome);
                   8690:         }
1.1312    raeburn  8691:         if ($env{'request.course.id'}) {
                   8692:             if (($ownername eq $env{'course.'.$env{'request.course.id'}.'.num'}) &&
                   8693:                 ($ownerdomain eq $env{'course.'.$env{'request.course.id'}.'.domain'})) {
                   8694:                 if (&allowed('mdc',$env{'request.course.id'})) {
                   8695:                     $ownerhome = $env{'course.'.$env{'request.course.id'}.'.home'};
                   8696:                     return ($ownername,$ownerdomain,$ownerhome);
                   8697:                 }
                   8698:             }
                   8699:         }
1.1192    raeburn  8700:     }
                   8701: 
                   8702: # We don't have any access right now. If we are not possibly going to do anything about this,
                   8703: # we might as well leave
                   8704:    unless ($setpriv) { return ''; }
                   8705: 
                   8706: # Backdoor access?
                   8707:     my $allowed=&allowed('eco',$ownerdomain);
                   8708: # Nope
                   8709:     unless ($allowed) { return ''; }
                   8710: # Looks like we may have access, but could be locked by the owner of the construction space
                   8711:     if ($allowed eq 'U') {
                   8712:         my %blocked=&get('environment',['domcoord.author'],
                   8713:                          $ownerdomain,$ownername);
                   8714: # Is blocked by owner
                   8715:         if ($blocked{'domcoord.author'} eq 'blocked') { return ''; }
                   8716:     }
                   8717:     if (($allowed eq 'F') || ($allowed eq 'U')) {
                   8718: # Grant temporary access
                   8719:         my $then=$env{'user.login.time'};
1.1209    raeburn  8720:         my $update=$env{'user.update.time'};
1.1192    raeburn  8721:         if (!$update) { $update = $then; }
                   8722:         my $refresh=$env{'user.refresh.time'};
                   8723:         if (!$refresh) { $refresh = $update; }
                   8724:         my $now = time;
                   8725:         &check_adhoc_privs($ownerdomain,$ownername,$update,$refresh,
                   8726:                            $now,'ca','constructaccess');
                   8727:         $ownerhome = &homeserver($ownername,$ownerdomain);
                   8728:         return($ownername,$ownerdomain,$ownerhome);
                   8729:     }
                   8730: # No business here
                   8731:     return '';
                   8732: }
                   8733: 
1.1282    raeburn  8734: # ----------------------------------------------------------- Content Blocking
                   8735: 
                   8736: {
                   8737: # Caches for faster Course Contents display where content blocking
                   8738: # is in operation (i.e., interval param set) for timed quiz.
                   8739: #
                   8740: # User for whom data are being temporarily cached.
                   8741: my $cacheduser='';
1.1424    raeburn  8742: # Course for which data are being temporarily cached.
                   8743: my $cachedcid='';
1.1282    raeburn  8744: # Cached blockers for this user (a hash of blocking items). 
                   8745: my %cachedblockers=();
                   8746: # When the data were last cached.
                   8747: my $cachedlast='';
                   8748: 
                   8749: sub load_all_blockers {
1.1427    raeburn  8750:     my ($uname,$udom)=@_;
1.1282    raeburn  8751:     if (($uname ne '') && ($udom ne '')) { 
                   8752:         if (($cacheduser eq $uname.':'.$udom) &&
1.1424    raeburn  8753:             ($cachedcid eq $env{'request.course.id'}) &&
1.1427    raeburn  8754:             (abs($cachedlast-time)<5)) {
1.1282    raeburn  8755:             return;
                   8756:         }
                   8757:     }
                   8758:     $cachedlast=time;
                   8759:     $cacheduser=$uname.':'.$udom;
1.1424    raeburn  8760:     $cachedcid=$env{'request.course.id'};
1.1427    raeburn  8761:     %cachedblockers = &get_commblock_resources();
1.1424    raeburn  8762:     return;
1.1282    raeburn  8763: }
                   8764: 
1.1162    raeburn  8765: sub get_comm_blocks {
                   8766:     my ($cdom,$cnum) = @_;
                   8767:     if ($cdom eq '' || $cnum eq '') {
                   8768:         return unless ($env{'request.course.id'});
                   8769:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   8770:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   8771:     }
                   8772:     my %commblocks;
                   8773:     my $hashid=$cdom.'_'.$cnum;
                   8774:     my ($blocksref,$cached)=&is_cached_new('comm_block',$hashid);
                   8775:     if ((defined($cached)) && (ref($blocksref) eq 'HASH')) {
                   8776:         %commblocks = %{$blocksref};
                   8777:     } else {
                   8778:         %commblocks = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
                   8779:         my $cachetime = 600;
                   8780:         &do_cache_new('comm_block',$hashid,\%commblocks,$cachetime);
                   8781:     }
                   8782:     return %commblocks;
                   8783: }
                   8784: 
1.1282    raeburn  8785: sub get_commblock_resources {
                   8786:     my ($blocks) = @_;
                   8787:     my %blockers = ();
                   8788:     return %blockers unless ($env{'request.course.id'});
                   8789:     return %blockers if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
1.1162    raeburn  8790:     my %commblocks;
                   8791:     if (ref($blocks) eq 'HASH') {
                   8792:         %commblocks = %{$blocks};
                   8793:     } else {
                   8794:         %commblocks = &get_comm_blocks();
                   8795:     }
1.1282    raeburn  8796:     return %blockers unless (keys(%commblocks) > 0); 
                   8797:     my $navmap = Apache::lonnavmaps::navmap->new();
                   8798:     return %blockers unless (ref($navmap));
1.1162    raeburn  8799:     my $now = time;
                   8800:     foreach my $block (keys(%commblocks)) {
                   8801:         if ($block =~ /^(\d+)____(\d+)$/) {
                   8802:             my ($start,$end) = ($1,$2);
                   8803:             if ($start <= $now && $end >= $now) {
                   8804:                 if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
                   8805:                     if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
                   8806:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
1.1282    raeburn  8807:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
                   8808:                                 $blockers{$block}{maps} = $commblocks{$block}{'blocks'}{'docs'}{'maps'}; 
1.1162    raeburn  8809:                             }
                   8810:                         }
                   8811:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
1.1282    raeburn  8812:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
                   8813:                                 $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
1.1162    raeburn  8814:                             }
                   8815:                         }
                   8816:                     }
                   8817:                 }
                   8818:             }
                   8819:         } elsif ($block =~ /^firstaccess____(.+)$/) {
                   8820:             my $item = $1;
1.1163    raeburn  8821:             my @to_test;
1.1162    raeburn  8822:             if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
                   8823:                 if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
1.1282    raeburn  8824:                     my @interval;
                   8825:                     my $type = 'map';
                   8826:                     if ($item eq 'course') {
                   8827:                         $type = 'course';
                   8828:                         @interval=&EXT("resource.0.interval");
                   8829:                     } else {
                   8830:                         if ($item =~ /___\d+___/) {
                   8831:                             $type = 'resource';
                   8832:                             @interval=&EXT("resource.0.interval",$item);
                   8833:                             if (ref($navmap)) {                        
                   8834:                                 my $res = $navmap->getBySymb($item); 
                   8835:                                 push(@to_test,$res);
                   8836:                             }
1.1162    raeburn  8837:                         } else {
1.1282    raeburn  8838:                             my $mapsymb = &symbread($item,1);
                   8839:                             if ($mapsymb) {
                   8840:                                 if (ref($navmap)) {
                   8841:                                     my $mapres = $navmap->getBySymb($mapsymb);
1.1428    raeburn  8842:                                     if (ref($mapres)) {
                   8843:                                         my $first = $mapres->map_start();
                   8844:                                         my $finish = $mapres->map_finish();
                   8845:                                         my $it = $navmap->getIterator($first,$finish,undef,0,0);
1.1429    raeburn  8846:                                         if (ref($it)) {
                   8847:                                             my $res;
                   8848:                                             while ($res = $it->next(undef,1)) {
                   8849:                                                 next unless (ref($res));
                   8850:                                                 my $symb = $res->symb();
                   8851:                                                 next if (($symb eq $mapsymb) || ($symb eq ''));
                   8852:                                                 @interval=&EXT("resource.0.interval",$symb);
                   8853:                                                 if ($interval[1] eq 'map') {
                   8854:                                                     if ($res->answerable()) {
                   8855:                                                         push(@to_test,$res);
                   8856:                                                         last;
                   8857:                                                     }
1.1428    raeburn  8858:                                                 }
1.1162    raeburn  8859:                                             }
                   8860:                                         }
                   8861:                                     }
                   8862:                                 }
                   8863:                             }
                   8864:                         }
1.1282    raeburn  8865:                     }
1.1310    raeburn  8866:                     if ($interval[0] =~ /^(\d+)/) {
1.1308    raeburn  8867:                         my $timelimit = $1; 
1.1282    raeburn  8868:                         my $first_access;
                   8869:                         if ($type eq 'resource') {
                   8870:                             $first_access=&get_first_access($interval[1],$item);
                   8871:                         } elsif ($type eq 'map') {
                   8872:                             $first_access=&get_first_access($interval[1],undef,$item);
                   8873:                         } else {
                   8874:                             $first_access=&get_first_access($interval[1]);
                   8875:                         }
                   8876:                         if ($first_access) {
1.1292    raeburn  8877:                             my $timesup = $first_access+$timelimit;
1.1282    raeburn  8878:                             if ($timesup > $now) {
                   8879:                                 my $activeblock;
                   8880:                                 foreach my $res (@to_test) {
1.1283    raeburn  8881:                                     if ($res->answerable()) {
1.1282    raeburn  8882:                                         $activeblock = 1;
                   8883:                                         last;
                   8884:                                     }
                   8885:                                 }
                   8886:                                 if ($activeblock) {
                   8887:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
                   8888:                                          if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
                   8889:                                              $blockers{$block}{'maps'} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
                   8890:                                          }
                   8891:                                     }
                   8892:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
                   8893:                                         if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
                   8894:                                             $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
1.1163    raeburn  8895:                                         }
1.1162    raeburn  8896:                                     }
                   8897:                                 }
                   8898:                             }
                   8899:                         }
                   8900:                     }
                   8901:                 }
                   8902:             }
                   8903:         }
                   8904:     }
1.1282    raeburn  8905:     return %blockers;
1.1162    raeburn  8906: }
                   8907: 
1.1282    raeburn  8908: sub has_comm_blocking {
1.1427    raeburn  8909:     my ($priv,$symb,$uri,$ignoresymbdb,$noenccheck,$blocked,$blocks) = @_;
1.1282    raeburn  8910:     my @blockers;
                   8911:     return unless ($env{'request.course.id'});
                   8912:     return unless ($priv eq 'bre');
                   8913:     return if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
                   8914:     return if ($env{'request.state'} eq 'construct');
1.1427    raeburn  8915:     my %blockinfo;
                   8916:     if (ref($blocks) eq 'HASH') {
                   8917:         %blockinfo = &get_commblock_resources($blocks);
                   8918:     } else {
                   8919:         &load_all_blockers($env{'user.name'},$env{'user.domain'});
                   8920:         %blockinfo = %cachedblockers;
                   8921:     }
                   8922:     return unless (keys(%blockinfo) > 0);
1.1282    raeburn  8923:     my (%possibles,@symbs);
                   8924:     if (!$symb) {
1.1427    raeburn  8925:         $symb = &symbread($uri,1,1,1,\%possibles,$ignoresymbdb,$noenccheck);
1.1162    raeburn  8926:     }
1.1282    raeburn  8927:     if ($symb) {
                   8928:         @symbs = ($symb);
                   8929:     } elsif (keys(%possibles)) { 
                   8930:         @symbs = keys(%possibles);
                   8931:     }
                   8932:     my $noblock;
                   8933:     foreach my $symb (@symbs) {
                   8934:         last if ($noblock);
                   8935:         my ($map,$resid,$resurl)=&decode_symb($symb);
1.1427    raeburn  8936:         foreach my $block (keys(%blockinfo)) {
1.1282    raeburn  8937:             if ($block =~ /^firstaccess____(.+)$/) {
                   8938:                 my $item = $1;
1.1424    raeburn  8939:                 unless ($blocked) {
                   8940:                     if (($item eq $map) || ($item eq $symb)) {
                   8941:                         $noblock = 1;
                   8942:                         last;
                   8943:                     }
1.1282    raeburn  8944:                 }
                   8945:             }
1.1427    raeburn  8946:             if (ref($blockinfo{$block}) eq 'HASH') {
                   8947:                 if (ref($blockinfo{$block}{'resources'}) eq 'HASH') {
                   8948:                     if ($blockinfo{$block}{'resources'}{$symb}) {
1.1282    raeburn  8949:                         unless (grep(/^\Q$block\E$/,@blockers)) {
                   8950:                             push(@blockers,$block);
                   8951:                         }
                   8952:                     }
                   8953:                 }
1.1427    raeburn  8954:                 if (ref($blockinfo{$block}{'maps'}) eq 'HASH') {
                   8955:                     if ($blockinfo{$block}{'maps'}{$map}) {
1.1424    raeburn  8956:                         unless (grep(/^\Q$block\E$/,@blockers)) {
                   8957:                             push(@blockers,$block);
                   8958:                         }
1.1282    raeburn  8959:                     }
                   8960:                 }
1.1162    raeburn  8961:             }
                   8962:         }
                   8963:     }
1.1424    raeburn  8964:     unless ($noblock) { 
                   8965:         return @blockers;
                   8966:     }
                   8967:     return;
1.1282    raeburn  8968: }
1.1162    raeburn  8969: }
                   8970: 
1.1402    raeburn  8971: sub deeplink_check {
                   8972:     my ($priv,$symb,$uri) = @_;
                   8973:     return unless ($env{'request.course.id'});
                   8974:     return unless ($priv eq 'bre');
                   8975:     return if ($env{'request.state'} eq 'construct');
                   8976:     return if ($env{'request.role.adv'});
                   8977:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   8978:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   8979:     my (%possibles,@symbs);
                   8980:     if (!$symb) {
                   8981:         $symb = &symbread($uri,1,1,1,\%possibles);
                   8982:     }
                   8983:     if ($symb) {
                   8984:         @symbs = ($symb);
                   8985:     } elsif (keys(%possibles)) {
                   8986:         @symbs = keys(%possibles);
                   8987:     }
                   8988: 
                   8989:     my ($login,$switchrole,$allow);
                   8990:     if ($env{'request.deeplink.login'} =~ m{^\Q/tiny/$cdom/\E(\w+)$}) {
                   8991:         my $key = $1;
                   8992:         my $tinyurl;
                   8993:         my ($result,$cached)=&Apache::lonnet::is_cached_new('tiny',$cdom."\0".$key);
                   8994:         if (defined($cached)) {
                   8995:              $tinyurl = $result;
                   8996:         } else {
                   8997:              my $configuname = &Apache::lonnet::get_domainconfiguser($cdom);
                   8998:              my %currtiny = &Apache::lonnet::get('tiny',[$key],$cdom,$configuname);
                   8999:              if ($currtiny{$key} ne '') {
                   9000:                  $tinyurl = $currtiny{$key};
                   9001:                  &Apache::lonnet::do_cache_new('tiny',$cdom."\0".$key,$currtiny{$key},600);
                   9002:              }
                   9003:         }
                   9004:         if ($tinyurl ne '') {
                   9005:             my ($cnumreq,$posslogin) = split(/\&/,$tinyurl);
                   9006:             if ($cnumreq eq $cnum) {
                   9007:                 $login = $posslogin;
                   9008:             } else {
                   9009:                 $switchrole = 1;
                   9010:             }
                   9011:         }
                   9012:     }
                   9013:     foreach my $symb (@symbs) {
                   9014:         last if ($allow);
                   9015:         my $deeplink = &EXT("resource.0.deeplink",$symb);
                   9016:         if ($deeplink eq '') {
                   9017:             $allow = 1;
                   9018:         } else {
                   9019:             my ($listed,$scope,$access) = split(/,/,$deeplink);
                   9020:             if ($access eq 'any') {
                   9021:                 $allow = 1;
                   9022:             } elsif ($login) {
                   9023:                 if ($access eq 'only') {
                   9024:                     if ($scope eq 'res') {
                   9025:                         if ($symb eq $login) {
                   9026:                             $allow = 1;
                   9027:                         }
                   9028:                     } elsif ($scope eq 'map') {
                   9029: #FIXME Compare map for $env{'request.deeplink.login'} with map for $symb
                   9030:                     } elsif ($scope eq 'rec') {
                   9031: #FIXME Recurse up for $env{'request.deeplink.login'} with map for $symb
                   9032:                     }
                   9033:                 } else {
                   9034:                     my ($acctype,$item) = split(/:/,$access);
                   9035:                     if (($acctype eq 'lti') && ($env{'user.linkprotector'})) {
                   9036:                         if (grep(/^\Q$item\E$/,split(/,/,$env{'user.linkprotector'}))) {
                   9037:                             my %tinyurls = &get('tiny',[$symb],$cdom,$cnum);
                   9038:                             if (grep(/\Q$tinyurls{$symb}\E$/,split(/,/,$env{'user.linkproturis'}))) {
                   9039:                                 $allow = 1;
                   9040:                             }
                   9041:                         }
                   9042:                     } elsif (($acctype eq 'key') && ($env{'user.deeplinkkey'})) {
                   9043:                         if (grep(/^\Q$item\E$/,split(/,/,$env{'user.deeplinkkey'}))) {
                   9044:                             my %tinyurls = &get('tiny',[$symb],$cdom,$cnum);
                   9045:                             if (grep(/\Q$tinyurls{$symb}\E$/,split(/,/,$env{'user.keyedlinkuri'}))) {
                   9046:                                 $allow = 1;
                   9047:                             }
                   9048:                         }
                   9049:                     }
                   9050:                 }
                   9051:             }
                   9052:         }
                   9053:     }
                   9054:     return if ($allow);
                   9055:     return 1;
                   9056: }
                   9057: 
1.1282    raeburn  9058: # -------------------------------- Deversion and split uri into path an filename   
                   9059: 
1.1133    foxr     9060: #
1.1282    raeburn  9061: #   Removes the version from a URI and
1.1133    foxr     9062: #   splits it in to its filename and path to the filename.
                   9063: #   Seems like File::Basename could have done this more clearly.
                   9064: #   Parameters:
                   9065: #      $uri   - input URI
                   9066: #   Returns:
                   9067: #     Two element list consisting of 
                   9068: #     $pathname  - the URI up to and excluding the trailing /
                   9069: #     $filename  - The part of the URI following the last /
                   9070: #  NOTE:
                   9071: #    Another realization of this is simply:
                   9072: #    use File::Basename;
                   9073: #    ...
                   9074: #    $uri = shift;
                   9075: #    $filename = basename($uri);
                   9076: #    $path     = dirname($uri);
                   9077: #    return ($filename, $path);
                   9078: #
                   9079: #     The implementation below is probably faster however.
                   9080: #
1.710     albertel 9081: sub split_uri_for_cond {
                   9082:     my $uri=&deversion(&declutter(shift));
                   9083:     my @uriparts=split(/\//,$uri);
                   9084:     my $filename=pop(@uriparts);
                   9085:     my $pathname=join('/',@uriparts);
                   9086:     return ($pathname,$filename);
                   9087: }
1.232     www      9088: # --------------------------------------------------- Is a resource on the map?
                   9089: 
                   9090: sub is_on_map {
1.710     albertel 9091:     my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289     bowersj2 9092:     #Trying to find the conditional for the file
1.620     albertel 9093:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289     bowersj2 9094: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
1.232     www      9095:     if ($match) {
1.289     bowersj2 9096: 	return (1,$1);
                   9097:     } else {
1.434     www      9098: 	return (0,0);
1.289     bowersj2 9099:     }
1.12      www      9100: }
                   9101: 
1.427     www      9102: # --------------------------------------------------------- Get symb from alias
                   9103: 
                   9104: sub get_symb_from_alias {
                   9105:     my $symb=shift;
                   9106:     my ($map,$resid,$url)=&decode_symb($symb);
                   9107: # Already is a symb
                   9108:     if ($url) { return $symb; }
                   9109: # Must be an alias
                   9110:     my $aliassymb='';
                   9111:     my %bighash;
1.620     albertel 9112:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427     www      9113:                             &GDBM_READER(),0640)) {
                   9114:         my $rid=$bighash{'mapalias_'.$symb};
                   9115: 	if ($rid) {
                   9116: 	    my ($mapid,$resid)=split(/\./,$rid);
1.429     albertel 9117: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
                   9118: 				    $resid,$bighash{'src_'.$rid});
1.427     www      9119: 	}
                   9120:         untie %bighash;
                   9121:     }
                   9122:     return $aliassymb;
                   9123: }
                   9124: 
1.12      www      9125: # ----------------------------------------------------------------- Define Role
                   9126: 
                   9127: sub definerole {
                   9128:   if (allowed('mcr','/')) {
1.1326    raeburn  9129:     my ($rolename,$sysrole,$domrole,$courole,$uname,$udom)=@_;
1.800     albertel 9130:     foreach my $role (split(':',$sysrole)) {
                   9131: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 9132:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
                   9133:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
                   9134: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      9135:                return "refused:s:$crole&$cqual"; 
                   9136:             }
                   9137:         }
1.191     harris41 9138:     }
1.800     albertel 9139:     foreach my $role (split(':',$domrole)) {
                   9140: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 9141:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
                   9142:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
                   9143: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
1.21      www      9144:                return "refused:d:$crole&$cqual"; 
                   9145:             }
                   9146:         }
1.191     harris41 9147:     }
1.800     albertel 9148:     foreach my $role (split(':',$courole)) {
                   9149: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 9150:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
                   9151:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
                   9152: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      9153:                return "refused:c:$crole&$cqual"; 
                   9154:             }
                   9155:         }
1.191     harris41 9156:     }
1.1326    raeburn  9157:     my $uhome;
                   9158:     if (($uname ne '') && ($udom ne '')) {
                   9159:         $uhome = &homeserver($uname,$udom);
                   9160:         return $uhome if ($uhome eq 'no_host');
                   9161:     } else {
                   9162:         $uname = $env{'user.name'};
                   9163:         $udom = $env{'user.domain'};
                   9164:         $uhome = $env{'user.home'};
                   9165:     }
1.620     albertel 9166:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.1326    raeburn  9167:                 "$udom:$uname:rolesdef_$rolename=".
1.21      www      9168:                 escape($sysrole.'_'.$domrole.'_'.$courole);
1.1326    raeburn  9169:     return reply($command,$uhome);
1.12      www      9170:   } else {
                   9171:     return 'refused';
                   9172:   }
1.105     harris41 9173: }
                   9174: 
                   9175: # ---------------- Make a metadata query against the network of library servers
                   9176: 
                   9177: sub metadata_query {
1.1237    raeburn  9178:     my ($query,$custom,$customshow,$server_array,$domains_hash)=@_;
1.120     harris41 9179:     my %rhash;
1.845     albertel 9180:     my %libserv = &all_library();
1.244     matthew  9181:     my @server_list = (defined($server_array) ? @$server_array
                   9182:                                               : keys(%libserv) );
                   9183:     for my $server (@server_list) {
1.1237    raeburn  9184:         my $domains = ''; 
                   9185:         if (ref($domains_hash) eq 'HASH') {
                   9186:             $domains = $domains_hash->{$server}; 
                   9187:         }
1.118     harris41 9188: 	unless ($custom or $customshow) {
1.1237    raeburn  9189: 	    my $reply=&reply("querysend:".&escape($query).':::'.&escape($domains),$server);
1.118     harris41 9190: 	    $rhash{$server}=$reply;
                   9191: 	}
                   9192: 	else {
                   9193: 	    my $reply=&reply("querysend:".&escape($query).':'.
1.1237    raeburn  9194: 			     &escape($custom).':'.&escape($customshow).':'.&escape($domains),
1.118     harris41 9195: 			     $server);
                   9196: 	    $rhash{$server}=$reply;
                   9197: 	}
1.112     harris41 9198:     }
1.118     harris41 9199:     return \%rhash;
1.240     www      9200: }
                   9201: 
                   9202: # ----------------------------------------- Send log queries and wait for reply
                   9203: 
                   9204: sub log_query {
                   9205:     my ($uname,$udom,$query,%filters)=@_;
                   9206:     my $uhome=&homeserver($uname,$udom);
                   9207:     if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838     albertel 9208:     my $uhost=&hostname($uhome);
1.800     albertel 9209:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240     www      9210:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
                   9211:                        $uhome);
1.479     albertel 9212:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242     www      9213:     return get_query_reply($queryid);
                   9214: }
                   9215: 
1.818     raeburn  9216: # -------------------------- Update MySQL table for portfolio file
                   9217: 
                   9218: sub update_portfolio_table {
1.821     raeburn  9219:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.970     raeburn  9220:     if ($group ne '') {
                   9221:         $file_name =~s /^\Q$group\E//;
                   9222:     }
1.818     raeburn  9223:     my $homeserver = &homeserver($uname,$udom);
                   9224:     my $queryid=
1.821     raeburn  9225:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
                   9226:                ':'.&escape($file_name).':'.$action,$homeserver);
1.818     raeburn  9227:     my $reply = &get_query_reply($queryid);
                   9228:     return $reply;
                   9229: }
                   9230: 
1.899     raeburn  9231: # -------------------------- Update MySQL allusers table
                   9232: 
                   9233: sub update_allusers_table {
                   9234:     my ($uname,$udom,$names) = @_;
                   9235:     my $homeserver = &homeserver($uname,$udom);
                   9236:     my $queryid=
                   9237:         &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
                   9238:                'lastname='.&escape($names->{'lastname'}).'%%'.
                   9239:                'firstname='.&escape($names->{'firstname'}).'%%'.
                   9240:                'middlename='.&escape($names->{'middlename'}).'%%'.
                   9241:                'generation='.&escape($names->{'generation'}).'%%'.
                   9242:                'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
                   9243:                'id='.&escape($names->{'id'}),$homeserver);
1.1075    raeburn  9244:     return;
1.899     raeburn  9245: }
                   9246: 
1.508     raeburn  9247: # ------- Request retrieval of institutional classlists for course(s)
1.506     raeburn  9248: 
                   9249: sub fetch_enrollment_query {
1.511     raeburn  9250:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.1317    raeburn  9251:     my ($homeserver,$sleep,$loopmax);
1.547     raeburn  9252:     my $maxtries = 1;
1.508     raeburn  9253:     if ($context eq 'automated') {
                   9254:         $homeserver = $perlvar{'lonHostID'};
1.1317    raeburn  9255:         $sleep = 2;
                   9256:         $loopmax = 100;
1.547     raeburn  9257:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508     raeburn  9258:     } else {
                   9259:         $homeserver = &homeserver($cnum,$dom);
                   9260:     }
1.838     albertel 9261:     my $host=&hostname($homeserver);
1.506     raeburn  9262:     my $cmd = '';
1.1000    raeburn  9263:     foreach my $affiliate (keys(%{$affiliatesref})) {
1.800     albertel 9264:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506     raeburn  9265:     }
                   9266:     $cmd =~ s/%%$//;
                   9267:     $cmd = &escape($cmd);
                   9268:     my $query = 'fetchenrollment';
1.620     albertel 9269:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526     raeburn  9270:     unless ($queryid=~/^\Q$host\E\_/) { 
                   9271:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
                   9272:         return 'error: '.$queryid;
                   9273:     }
1.1317    raeburn  9274:     my $reply = &get_query_reply($queryid,$sleep,$loopmax);
1.547     raeburn  9275:     my $tries = 1;
                   9276:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
1.1317    raeburn  9277:         $reply = &get_query_reply($queryid,$sleep,$loopmax);
1.547     raeburn  9278:         $tries ++;
                   9279:     }
1.526     raeburn  9280:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620     albertel 9281:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526     raeburn  9282:     } else {
1.901     albertel 9283:         my @responses = split(/:/,$reply);
1.1322    raeburn  9284:         if (grep { $_ eq $homeserver } &current_machine_ids()) {
1.800     albertel 9285:             foreach my $line (@responses) {
                   9286:                 my ($key,$value) = split(/=/,$line,2);
1.515     raeburn  9287:                 $$replyref{$key} = $value;
                   9288:             }
                   9289:         } else {
1.1117    foxr     9290:             my $pathname = LONCAPA::tempdir();
1.800     albertel 9291:             foreach my $line (@responses) {
                   9292:                 my ($key,$value) = split(/=/,$line);
1.506     raeburn  9293:                 $$replyref{$key} = $value;
                   9294:                 if ($value > 0) {
1.800     albertel 9295:                     foreach my $item (@{$$affiliatesref{$key}}) {
                   9296:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506     raeburn  9297:                         my $destname = $pathname.'/'.$filename;
                   9298:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526     raeburn  9299:                         if ($xml_classlist =~ /^error/) {
                   9300:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
                   9301:                         } else {
1.1359    raeburn  9302:                             if ( open(FILE,">",$destname) ) {
1.506     raeburn  9303:                                 print FILE &unescape($xml_classlist);
                   9304:                                 close(FILE);
1.526     raeburn  9305:                             } else {
                   9306:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506     raeburn  9307:                             }
                   9308:                         }
                   9309:                     }
                   9310:                 }
                   9311:             }
                   9312:         }
                   9313:         return 'ok';
                   9314:     }
                   9315:     return 'error';
                   9316: }
                   9317: 
1.242     www      9318: sub get_query_reply {
1.1317    raeburn  9319:     my ($queryid,$sleep,$loopmax) = @_;;
                   9320:     if (($sleep eq '') || ($sleep !~ /^\d+\.?\d*$/)) {
                   9321:         $sleep = 0.2;
                   9322:     }
                   9323:     if (($loopmax eq '') || ($loopmax =~ /\D/)) {
                   9324:         $loopmax = 100;
                   9325:     }
1.1117    foxr     9326:     my $replyfile=LONCAPA::tempdir().$queryid;
1.240     www      9327:     my $reply='';
1.1317    raeburn  9328:     for (1..$loopmax) {
                   9329: 	sleep($sleep);
1.240     www      9330:         if (-e $replyfile.'.end') {
1.1359    raeburn  9331: 	    if (open(my $fh,"<",$replyfile)) {
1.904     albertel 9332: 		$reply = join('',<$fh>);
                   9333: 		close($fh);
1.240     www      9334: 	   } else { return 'error: reply_file_error'; }
1.242     www      9335:            return &unescape($reply);
                   9336: 	}
1.240     www      9337:     }
1.242     www      9338:     return 'timeout:'.$queryid;
1.240     www      9339: }
                   9340: 
                   9341: sub courselog_query {
1.241     www      9342: #
                   9343: # possible filters:
                   9344: # url: url or symb
                   9345: # username
                   9346: # domain
                   9347: # action: view, submit, grade
                   9348: # start: timestamp
                   9349: # end: timestamp
                   9350: #
1.240     www      9351:     my (%filters)=@_;
1.620     albertel 9352:     unless ($env{'request.course.id'}) { return 'no_course'; }
1.241     www      9353:     if ($filters{'url'}) {
                   9354: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
                   9355:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
                   9356:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
                   9357:     }
1.620     albertel 9358:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   9359:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240     www      9360:     return &log_query($cname,$cdom,'courselog',%filters);
                   9361: }
                   9362: 
                   9363: sub userlog_query {
1.858     raeburn  9364: #
                   9365: # possible filters:
                   9366: # action: log check role
                   9367: # start: timestamp
                   9368: # end: timestamp
                   9369: #
1.240     www      9370:     my ($uname,$udom,%filters)=@_;
                   9371:     return &log_query($uname,$udom,'userlog',%filters);
1.12      www      9372: }
                   9373: 
1.506     raeburn  9374: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
                   9375: 
                   9376: sub auto_run {
1.508     raeburn  9377:     my ($cnum,$cdom) = @_;
1.876     raeburn  9378:     my $response = 0;
                   9379:     my $settings;
                   9380:     my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
                   9381:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
                   9382:         $settings = $domconfig{'autoenroll'};
                   9383:         if ($settings->{'run'} eq '1') {
                   9384:             $response = 1;
                   9385:         }
                   9386:     } else {
1.934     raeburn  9387:         my $homeserver;
                   9388:         if (&is_course($cdom,$cnum)) {
                   9389:             $homeserver = &homeserver($cnum,$cdom);
                   9390:         } else {
                   9391:             $homeserver = &domain($cdom,'primary');
                   9392:         }
                   9393:         if ($homeserver ne 'no_host') {
                   9394:             $response = &reply('autorun:'.$cdom,$homeserver);
                   9395:         }
1.876     raeburn  9396:     }
1.506     raeburn  9397:     return $response;
                   9398: }
1.776     albertel 9399: 
1.506     raeburn  9400: sub auto_get_sections {
1.508     raeburn  9401:     my ($cnum,$cdom,$inst_coursecode) = @_;
1.1007    raeburn  9402:     my $homeserver;
                   9403:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) { 
                   9404:         $homeserver = &homeserver($cnum,$cdom);
                   9405:     }
                   9406:     if (!defined($homeserver)) { 
                   9407:         if ($cdom =~ /^$match_domain$/) {
                   9408:             $homeserver = &domain($cdom,'primary');
                   9409:         }
                   9410:     }
                   9411:     my @secs;
                   9412:     if (defined($homeserver)) {
                   9413:         my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
                   9414:         unless ($response eq 'refused') {
                   9415:             @secs = split(/:/,$response);
                   9416:         }
1.506     raeburn  9417:     }
                   9418:     return @secs;
                   9419: }
1.776     albertel 9420: 
1.506     raeburn  9421: sub auto_new_course {
1.1099    raeburn  9422:     my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
1.508     raeburn  9423:     my $homeserver = &homeserver($cnum,$cdom);
1.1099    raeburn  9424:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
1.506     raeburn  9425:     return $response;
                   9426: }
1.776     albertel 9427: 
1.506     raeburn  9428: sub auto_validate_courseID {
1.508     raeburn  9429:     my ($cnum,$cdom,$inst_course_id) = @_;
                   9430:     my $homeserver = &homeserver($cnum,$cdom);
1.511     raeburn  9431:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506     raeburn  9432:     return $response;
                   9433: }
1.776     albertel 9434: 
1.1007    raeburn  9435: sub auto_validate_instcode {
1.1020    raeburn  9436:     my ($cnum,$cdom,$instcode,$owner) = @_;
1.1007    raeburn  9437:     my ($homeserver,$response);
                   9438:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
                   9439:         $homeserver = &homeserver($cnum,$cdom);
                   9440:     }
                   9441:     if (!defined($homeserver)) {
                   9442:         if ($cdom =~ /^$match_domain$/) {
                   9443:             $homeserver = &domain($cdom,'primary');
                   9444:         }
                   9445:     }
1.1065    raeburn  9446:     $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
                   9447:                         &escape($instcode).':'.&escape($owner),$homeserver));
1.1216    raeburn  9448:     my ($outcome,$description,$defaultcredits) = map { &unescape($_); } split('&',$response,3);
                   9449:     return ($outcome,$description,$defaultcredits);
1.1007    raeburn  9450: }
                   9451: 
1.1444    raeburn  9452: sub auto_validate_inst_crosslist {
                   9453:     my ($cnum,$cdom,$instcode,$inst_xlist,$coowner) = @_;
                   9454:     my ($homeserver,$response);
                   9455:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
                   9456:         $homeserver = &homeserver($cnum,$cdom);
                   9457:     }
                   9458:     if (!defined($homeserver)) {
                   9459:         if ($cdom =~ /^$match_domain$/) {
                   9460:             $homeserver = &domain($cdom,'primary');
                   9461:         }
                   9462:     }
                   9463:     unless (($homeserver eq '') || ($homeserver eq 'no_host')) {
                   9464:         $response=&reply('autovalidateinstcrosslist:'.$cdom.':'.
                   9465:                          &escape($instcode).':'.&escape($inst_xlist).':'.
1.1445    raeburn  9466:                          &escape($coowner),$homeserver);
1.1444    raeburn  9467:     }
                   9468:     return $response;
                   9469: }
                   9470: 
1.506     raeburn  9471: sub auto_create_password {
1.873     raeburn  9472:     my ($cnum,$cdom,$authparam,$udom) = @_;
                   9473:     my ($homeserver,$response);
1.506     raeburn  9474:     my $create_passwd = 0;
                   9475:     my $authchk = '';
1.873     raeburn  9476:     if ($udom =~ /^$match_domain$/) {
                   9477:         $homeserver = &domain($udom,'primary');
                   9478:     }
                   9479:     if ($homeserver eq '') {
                   9480:         if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
                   9481:             $homeserver = &homeserver($cnum,$cdom);
                   9482:         }
                   9483:     }
                   9484:     if ($homeserver eq '') {
                   9485:         $authchk = 'nodomain';
1.506     raeburn  9486:     } else {
1.873     raeburn  9487:         $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
                   9488:         if ($response eq 'refused') {
                   9489:             $authchk = 'refused';
                   9490:         } else {
1.901     albertel 9491:             ($authparam,$create_passwd,$authchk) = split(/:/,$response);
1.873     raeburn  9492:         }
1.506     raeburn  9493:     }
                   9494:     return ($authparam,$create_passwd,$authchk);
                   9495: }
                   9496: 
1.706     raeburn  9497: sub auto_photo_permission {
                   9498:     my ($cnum,$cdom,$students) = @_;
                   9499:     my $homeserver = &homeserver($cnum,$cdom);
1.707     albertel 9500:     my ($outcome,$perm_reqd,$conditions) = 
                   9501: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709     albertel 9502:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   9503: 	return (undef,undef);
                   9504:     }
1.706     raeburn  9505:     return ($outcome,$perm_reqd,$conditions);
                   9506: }
                   9507: 
                   9508: sub auto_checkphotos {
                   9509:     my ($uname,$udom,$pid) = @_;
                   9510:     my $homeserver = &homeserver($uname,$udom);
                   9511:     my ($result,$resulttype);
                   9512:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707     albertel 9513: 				   &escape($uname).':'.&escape($pid),
                   9514: 				   $homeserver));
1.709     albertel 9515:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   9516: 	return (undef,undef);
                   9517:     }
1.706     raeburn  9518:     if ($outcome) {
                   9519:         ($result,$resulttype) = split(/:/,$outcome);
                   9520:     } 
                   9521:     return ($result,$resulttype);
                   9522: }
                   9523: 
                   9524: sub auto_photochoice {
                   9525:     my ($cnum,$cdom) = @_;
                   9526:     my $homeserver = &homeserver($cnum,$cdom);
                   9527:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707     albertel 9528: 						       &escape($cdom),
                   9529: 						       $homeserver)));
1.709     albertel 9530:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   9531: 	return (undef,undef);
                   9532:     }
1.706     raeburn  9533:     return ($update,$comment);
                   9534: }
                   9535: 
                   9536: sub auto_photoupdate {
                   9537:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
                   9538:     my $homeserver = &homeserver($cnum,$dom);
1.838     albertel 9539:     my $host=&hostname($homeserver);
1.706     raeburn  9540:     my $cmd = '';
                   9541:     my $maxtries = 1;
1.800     albertel 9542:     foreach my $affiliate (keys(%{$affiliatesref})) {
                   9543:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706     raeburn  9544:     }
                   9545:     $cmd =~ s/%%$//;
                   9546:     $cmd = &escape($cmd);
                   9547:     my $query = 'institutionalphotos';
                   9548:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
                   9549:     unless ($queryid=~/^\Q$host\E\_/) {
                   9550:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
                   9551:         return 'error: '.$queryid;
                   9552:     }
                   9553:     my $reply = &get_query_reply($queryid);
                   9554:     my $tries = 1;
                   9555:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   9556:         $reply = &get_query_reply($queryid);
                   9557:         $tries ++;
                   9558:     }
                   9559:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   9560:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
                   9561:     } else {
                   9562:         my @responses = split(/:/,$reply);
                   9563:         my $outcome = shift(@responses); 
                   9564:         foreach my $item (@responses) {
                   9565:             my ($key,$value) = split(/=/,$item);
                   9566:             $$photo{$key} = $value;
                   9567:         }
                   9568:         return $outcome;
                   9569:     }
                   9570:     return 'error';
                   9571: }
                   9572: 
1.521     raeburn  9573: sub auto_instcode_format {
1.793     albertel 9574:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
                   9575: 	$cat_order) = @_;
1.521     raeburn  9576:     my $courses = '';
1.772     raeburn  9577:     my @homeservers;
1.521     raeburn  9578:     if ($caller eq 'global') {
1.841     albertel 9579: 	my %servers = &get_servers($codedom,'library');
                   9580: 	foreach my $tryserver (keys(%servers)) {
                   9581: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   9582: 		push(@homeservers,$tryserver);
                   9583: 	    }
1.584     raeburn  9584:         }
1.1022    raeburn  9585:     } elsif ($caller eq 'requests') {
                   9586:         if ($codedom =~ /^$match_domain$/) {
                   9587:             my $chome = &domain($codedom,'primary');
                   9588:             unless ($chome eq 'no_host') {
                   9589:                 push(@homeservers,$chome);
                   9590:             }
                   9591:         }
1.521     raeburn  9592:     } else {
1.772     raeburn  9593:         push(@homeservers,&homeserver($caller,$codedom));
1.521     raeburn  9594:     }
1.793     albertel 9595:     foreach my $code (keys(%{$instcodes})) {
                   9596:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521     raeburn  9597:     }
                   9598:     chop($courses);
1.772     raeburn  9599:     my $ok_response = 0;
                   9600:     my $response;
                   9601:     while (@homeservers > 0 && $ok_response == 0) {
                   9602:         my $server = shift(@homeservers); 
                   9603:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
                   9604:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
                   9605:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
1.901     albertel 9606: 		split(/:/,$response);
1.772     raeburn  9607:             %{$codes} = (%{$codes},&str2hash($codes_str));
                   9608:             push(@{$codetitles},&str2array($codetitles_str));
                   9609:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
                   9610:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
                   9611:             $ok_response = 1;
                   9612:         }
                   9613:     }
                   9614:     if ($ok_response) {
1.521     raeburn  9615:         return 'ok';
1.772     raeburn  9616:     } else {
                   9617:         return $response;
1.521     raeburn  9618:     }
                   9619: }
                   9620: 
1.792     raeburn  9621: sub auto_instcode_defaults {
                   9622:     my ($domain,$returnhash,$code_order) = @_;
                   9623:     my @homeservers;
1.841     albertel 9624: 
                   9625:     my %servers = &get_servers($domain,'library');
                   9626:     foreach my $tryserver (keys(%servers)) {
                   9627: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   9628: 	    push(@homeservers,$tryserver);
                   9629: 	}
1.792     raeburn  9630:     }
1.841     albertel 9631: 
1.792     raeburn  9632:     my $response;
1.841     albertel 9633:     foreach my $server (@homeservers) {
1.792     raeburn  9634:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841     albertel 9635:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
                   9636: 	
                   9637: 	foreach my $pair (split(/\&/,$response)) {
                   9638: 	    my ($name,$value)=split(/\=/,$pair);
                   9639: 	    if ($name eq 'code_order') {
                   9640: 		@{$code_order} = split(/\&/,&unescape($value));
                   9641: 	    } else {
                   9642: 		$returnhash->{&unescape($name)}=&unescape($value);
                   9643: 	    }
                   9644: 	}
                   9645: 	return 'ok';
1.792     raeburn  9646:     }
1.841     albertel 9647: 
                   9648:     return $response;
1.1003    raeburn  9649: }
                   9650: 
                   9651: sub auto_possible_instcodes {
1.1007    raeburn  9652:     my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
                   9653:     unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
                   9654:             (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
                   9655:         return;
                   9656:     }
1.1003    raeburn  9657:     my (@homeservers,$uhome);
                   9658:     if (defined(&domain($domain,'primary'))) {
                   9659:         $uhome=&domain($domain,'primary');
                   9660:         push(@homeservers,&domain($domain,'primary'));
                   9661:     } else {
                   9662:         my %servers = &get_servers($domain,'library');
                   9663:         foreach my $tryserver (keys(%servers)) {
                   9664:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   9665:                 push(@homeservers,$tryserver);
                   9666:             }
                   9667:         }
                   9668:     }
                   9669:     my $response;
                   9670:     foreach my $server (@homeservers) {
                   9671:         $response=&reply('autopossibleinstcodes:'.$domain,$server);
                   9672:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
1.1007    raeburn  9673:         my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) = 
                   9674:             split(':',$response);
                   9675:         @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
                   9676:         @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
1.1003    raeburn  9677:         foreach my $item (split('&',$cat_title)) {   
1.1005    raeburn  9678:             my ($name,$value)=split('=',$item);
                   9679:             $cat_titles->{&unescape($name)}=&thaw_unescape($value);
1.1003    raeburn  9680:         }
                   9681:         foreach my $item (split('&',$cat_order)) {
1.1005    raeburn  9682:             my ($name,$value)=split('=',$item);
                   9683:             $cat_orders->{&unescape($name)}=&thaw_unescape($value);
1.1003    raeburn  9684:         }
                   9685:         return 'ok';
                   9686:     }
                   9687:     return $response;
                   9688: }
1.792     raeburn  9689: 
1.1010    raeburn  9690: sub auto_courserequest_checks {
                   9691:     my ($dom) = @_;
1.1020    raeburn  9692:     my ($homeserver,%validations);
                   9693:     if ($dom =~ /^$match_domain$/) {
                   9694:         $homeserver = &domain($dom,'primary');
                   9695:     }
                   9696:     unless ($homeserver eq 'no_host') {
                   9697:         my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
                   9698:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
                   9699:             my @items = split(/&/,$response);
                   9700:             foreach my $item (@items) {
                   9701:                 my ($key,$value) = split('=',$item);
                   9702:                 $validations{&unescape($key)} = &thaw_unescape($value);
                   9703:             }
                   9704:         }
                   9705:     }
1.1010    raeburn  9706:     return %validations; 
                   9707: }
                   9708: 
1.1020    raeburn  9709: sub auto_courserequest_validation {
1.1255    raeburn  9710:     my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist,$custominfo) = @_;
1.1020    raeburn  9711:     my ($homeserver,$response);
                   9712:     if ($dom =~ /^$match_domain$/) {
                   9713:         $homeserver = &domain($dom,'primary');
                   9714:     }
1.1255    raeburn  9715:     unless ($homeserver eq 'no_host') {
                   9716:         my $customdata;
                   9717:         if (ref($custominfo) eq 'HASH') {
                   9718:             $customdata = &freeze_escape($custominfo);
                   9719:         }
1.1020    raeburn  9720:         $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
1.1021    raeburn  9721:                                     ':'.&escape($crstype).':'.&escape($inststatuslist).
1.1255    raeburn  9722:                                     ':'.&escape($instcode).':'.&escape($instseclist).':'.
                   9723:                                     $customdata,$homeserver));
1.1020    raeburn  9724:     }
                   9725:     return $response;
                   9726: }
                   9727: 
1.777     albertel 9728: sub auto_validate_class_sec {
1.918     raeburn  9729:     my ($cdom,$cnum,$owners,$inst_class) = @_;
1.773     raeburn  9730:     my $homeserver = &homeserver($cnum,$cdom);
1.918     raeburn  9731:     my $ownerlist;
                   9732:     if (ref($owners) eq 'ARRAY') {
                   9733:         $ownerlist = join(',',@{$owners});
                   9734:     } else {
                   9735:         $ownerlist = $owners;
                   9736:     }
1.773     raeburn  9737:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.918     raeburn  9738:                         &escape($ownerlist).':'.$cdom,$homeserver);
1.773     raeburn  9739:     return $response;
                   9740: }
                   9741: 
1.1367    raeburn  9742: sub auto_validate_instclasses {
                   9743:     my ($cdom,$cnum,$owners,$classesref) = @_;
                   9744:     my ($homeserver,%validations);
                   9745:     $homeserver = &homeserver($cnum,$cdom);
                   9746:     unless ($homeserver eq 'no_host') {
                   9747:         my $ownerlist;
                   9748:         if (ref($owners) eq 'ARRAY') {
                   9749:             $ownerlist = join(',',@{$owners});
                   9750:         } else {
                   9751:             $ownerlist = $owners;
                   9752:         }
                   9753:         if (ref($classesref) eq 'HASH') {
                   9754:             my $classes = &freeze_escape($classesref);
                   9755:             my $response=&reply('autovalidateinstclasses:'.&escape($ownerlist).
                   9756:                                 ':'.$cdom.':'.$classes,$homeserver);
                   9757:             unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
                   9758:                 my @items = split(/&/,$response);
                   9759:                 foreach my $item (@items) {
                   9760:                     my ($key,$value) = split('=',$item);
                   9761:                     $validations{&unescape($key)} = &thaw_unescape($value);
                   9762:                 }
                   9763:             }
                   9764:         }
                   9765:     }
                   9766:     return %validations;
                   9767: }
                   9768: 
1.1248    raeburn  9769: sub auto_crsreq_update {
                   9770:     my ($cdom,$cnum,$crstype,$action,$ownername,$ownerdomain,$fullname,$title,
1.1257    raeburn  9771:         $code,$accessstart,$accessend,$inbound) = @_;
1.1248    raeburn  9772:     my ($homeserver,%crsreqresponse);
                   9773:     if ($cdom =~ /^$match_domain$/) {
                   9774:         $homeserver = &domain($cdom,'primary');
                   9775:     }
                   9776:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
                   9777:         my $info;
                   9778:         if (ref($inbound) eq 'HASH') {
                   9779:             $info = &freeze_escape($inbound);
                   9780:         }
                   9781:         my $response=&reply('autocrsrequpdate:'.$cdom.':'.$cnum.':'.&escape($crstype).
                   9782:                             ':'.&escape($action).':'.&escape($ownername).':'.
                   9783:                             &escape($ownerdomain).':'.&escape($fullname).':'.
1.1257    raeburn  9784:                             &escape($title).':'.&escape($code).':'.
                   9785:                             &escape($accessstart).':'.&escape($accessend).':'.$info,
                   9786:                             $homeserver);
1.1248    raeburn  9787:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
                   9788:             my @items = split(/&/,$response);
                   9789:             foreach my $item (@items) {
                   9790:                 my ($key,$value) = split('=',$item);
                   9791:                 $crsreqresponse{&unescape($key)} = &thaw_unescape($value);
                   9792:             }
                   9793:         }
                   9794:     }
                   9795:     return \%crsreqresponse;
                   9796: }
                   9797: 
1.1305    raeburn  9798: sub auto_export_grades {
1.1309    raeburn  9799:     my ($cdom,$cnum,$inforef,$gradesref) = @_;
                   9800:     my ($homeserver,%exportresponse);
                   9801:     if ($cdom =~ /^$match_domain$/) {
                   9802:         $homeserver = &domain($cdom,'primary');
                   9803:     }
                   9804:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
                   9805:         my $info;
                   9806:         if (ref($inforef) eq 'HASH') {
                   9807:             $info = &freeze_escape($inforef);
                   9808:         }
                   9809:         if (ref($gradesref) eq 'HASH') {
                   9810:             my $grades = &freeze_escape($gradesref);
                   9811:             my $response=&reply('encrypt:autoexportgrades:'.$cdom.':'.$cnum.':'.
                   9812:                                 $info.':'.$grades,$homeserver);
                   9813:             unless ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/) {
                   9814:                 my @items = split(/&/,$response);
                   9815:                 foreach my $item (@items) {
                   9816:                     my ($key,$value) = split('=',$item);
                   9817:                     $exportresponse{&unescape($key)} = &thaw_unescape($value);
                   9818:                 }
                   9819:             }
                   9820:         }
                   9821:     }
                   9822:     return \%exportresponse;
1.1305    raeburn  9823: }
                   9824: 
1.1287    raeburn  9825: sub check_instcode_cloning {
                   9826:     my ($codedefaults,$code_order,$cloner,$clonefromcode,$clonetocode) = @_;
                   9827:     unless ((ref($codedefaults) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
                   9828:         return;
                   9829:     }
                   9830:     my $canclone;
                   9831:     if (@{$code_order} > 0) {
                   9832:         my $instcoderegexp ='^';
                   9833:         my @clonecodes = split(/\&/,$cloner);
                   9834:         foreach my $item (@{$code_order}) {
                   9835:             if (grep(/^\Q$item\E=/,@clonecodes)) {
                   9836:                 foreach my $pair (@clonecodes) {
                   9837:                     my ($key,$val) = split(/\=/,$pair,2);
                   9838:                     $val = &unescape($val);
                   9839:                     if ($key eq $item) {
                   9840:                         $instcoderegexp .= '('.$val.')';
                   9841:                         last;
                   9842:                     }
                   9843:                 }
                   9844:             } else {
                   9845:                 $instcoderegexp .= $codedefaults->{$item};
                   9846:             }
                   9847:         }
                   9848:         $instcoderegexp .= '$';
                   9849:         my (@from,@to);
                   9850:         eval {
                   9851:                (@from) = ($clonefromcode =~ /$instcoderegexp/);
                   9852:                (@to) = ($clonetocode =~ /$instcoderegexp/);
                   9853:         };
                   9854:         if ((@from > 0) && (@to > 0)) {
                   9855:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
                   9856:             if (!@diffs) {
                   9857:                 $canclone = 1;
                   9858:             }
                   9859:         }
                   9860:     }
                   9861:     return $canclone;
                   9862: }
                   9863: 
                   9864: sub default_instcode_cloning {
                   9865:     my ($clonedom,$domdefclone,$clonefromcode,$clonetocode,$codedefaultsref,$codeorderref) = @_;
                   9866:     my (%codedefaults,@code_order,$canclone);
                   9867:     if ((ref($codedefaultsref) eq 'HASH') && (ref($codeorderref) eq 'ARRAY')) {
                   9868:         %codedefaults = %{$codedefaultsref};
                   9869:         @code_order = @{$codeorderref};
                   9870:     } elsif ($clonedom) {
                   9871:         &auto_instcode_defaults($clonedom,\%codedefaults,\@code_order);
                   9872:     }
                   9873:     if (($domdefclone) && (@code_order)) {
                   9874:         my @clonecodes = split(/\+/,$domdefclone);
                   9875:         my $instcoderegexp ='^';
                   9876:         foreach my $item (@code_order) {
                   9877:             if (grep(/^\Q$item\E$/,@clonecodes)) {
                   9878:                 $instcoderegexp .= '('.$codedefaults{$item}.')';
                   9879:             } else {
                   9880:                 $instcoderegexp .= $codedefaults{$item};
                   9881:             }
                   9882:         }
                   9883:         $instcoderegexp .= '$';
                   9884:         my (@from,@to);
                   9885:         eval {
                   9886:             (@from) = ($clonefromcode =~ /$instcoderegexp/);
                   9887:             (@to) = ($clonetocode =~ /$instcoderegexp/);
                   9888:         };
                   9889:         if ((@from > 0) && (@to > 0)) {
                   9890:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
                   9891:             if (!@diffs) {
                   9892:                 $canclone = 1;
                   9893:             }
                   9894:         }
                   9895:     }
                   9896:     return $canclone;
                   9897: }
                   9898: 
1.679     raeburn  9899: # ------------------------------------------------------- Course Group routines
                   9900: 
                   9901: sub get_coursegroups {
1.809     raeburn  9902:     my ($cdom,$cnum,$group,$namespace) = @_;
                   9903:     return(&dump($namespace,$cdom,$cnum,$group));
1.805     raeburn  9904: }
                   9905: 
1.679     raeburn  9906: sub modify_coursegroup {
                   9907:     my ($cdom,$cnum,$groupsettings) = @_;
                   9908:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
                   9909: }
                   9910: 
1.809     raeburn  9911: sub toggle_coursegroup_status {
                   9912:     my ($cdom,$cnum,$group,$action) = @_;
                   9913:     my ($from_namespace,$to_namespace);
                   9914:     if ($action eq 'delete') {
                   9915:         $from_namespace = 'coursegroups';
                   9916:         $to_namespace = 'deleted_groups';
                   9917:     } else {
                   9918:         $from_namespace = 'deleted_groups';
                   9919:         $to_namespace = 'coursegroups';
                   9920:     }
                   9921:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805     raeburn  9922:     if (my $tmp = &error(%curr_group)) {
                   9923:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
                   9924:         return ('read error',$tmp);
                   9925:     } else {
                   9926:         my %savedsettings = %curr_group; 
1.809     raeburn  9927:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805     raeburn  9928:         my $deloutcome;
                   9929:         if ($result eq 'ok') {
1.809     raeburn  9930:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805     raeburn  9931:         } else {
                   9932:             return ('write error',$result);
                   9933:         }
                   9934:         if ($deloutcome eq 'ok') {
                   9935:             return 'ok';
                   9936:         } else {
                   9937:             return ('delete error',$deloutcome);
                   9938:         }
                   9939:     }
                   9940: }
                   9941: 
1.679     raeburn  9942: sub modify_group_roles {
1.957     raeburn  9943:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
1.679     raeburn  9944:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
                   9945:     my $role = 'gr/'.&escape($userprivs);
                   9946:     my ($uname,$udom) = split(/:/,$user);
1.957     raeburn  9947:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
1.684     raeburn  9948:     if ($result eq 'ok') {
                   9949:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
                   9950:     }
1.679     raeburn  9951:     return $result;
                   9952: }
                   9953: 
                   9954: sub modify_coursegroup_membership {
                   9955:     my ($cdom,$cnum,$membership) = @_;
                   9956:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
                   9957:     return $result;
                   9958: }
                   9959: 
1.682     raeburn  9960: sub get_active_groups {
                   9961:     my ($udom,$uname,$cdom,$cnum) = @_;
                   9962:     my $now = time;
                   9963:     my %groups = ();
                   9964:     foreach my $key (keys(%env)) {
1.811     albertel 9965:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682     raeburn  9966:             my ($start,$end) = split(/\./,$env{$key});
                   9967:             if (($end!=0) && ($end<$now)) { next; }
                   9968:             if (($start!=0) && ($start>$now)) { next; }
                   9969:             if ($1 eq $cdom && $2 eq $cnum) {
                   9970:                 $groups{$3} = $env{$key} ;
                   9971:             }
                   9972:         }
                   9973:     }
                   9974:     return %groups;
                   9975: }
                   9976: 
1.683     raeburn  9977: sub get_group_membership {
                   9978:     my ($cdom,$cnum,$group) = @_;
                   9979:     return(&dump('groupmembership',$cdom,$cnum,$group));
                   9980: }
                   9981: 
                   9982: sub get_users_groups {
                   9983:     my ($udom,$uname,$courseid) = @_;
1.733     raeburn  9984:     my @usersgroups;
1.683     raeburn  9985:     my $cachetime=1800;
                   9986: 
                   9987:     my $hashid="$udom:$uname:$courseid";
1.733     raeburn  9988:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
                   9989:     if (defined($cached)) {
1.734     albertel 9990:         @usersgroups = split(/:/,$grouplist);
1.733     raeburn  9991:     } else {  
                   9992:         $grouplist = '';
1.816     raeburn  9993:         my $courseurl = &courseid_to_courseurl($courseid);
1.1166    raeburn  9994:         my %roleshash = &dump('roles',$udom,$uname,$courseurl);
1.817     raeburn  9995:         my $access_end = $env{'course.'.$courseid.
                   9996:                               '.default_enrollment_end_date'};
                   9997:         my $now = time;
                   9998:         foreach my $key (keys(%roleshash)) {
                   9999:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
                   10000:                 my $group = $1;
                   10001:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
                   10002:                     my $start = $2;
                   10003:                     my $end = $1;
                   10004:                     if ($start == -1) { next; } # deleted from group
                   10005:                     if (($start!=0) && ($start>$now)) { next; }
                   10006:                     if (($end!=0) && ($end<$now)) {
                   10007:                         if ($access_end && $access_end < $now) {
                   10008:                             if ($access_end - $end < 86400) {
                   10009:                                 push(@usersgroups,$group);
1.733     raeburn  10010:                             }
                   10011:                         }
1.817     raeburn  10012:                         next;
1.733     raeburn  10013:                     }
1.817     raeburn  10014:                     push(@usersgroups,$group);
1.683     raeburn  10015:                 }
                   10016:             }
                   10017:         }
1.817     raeburn  10018:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
                   10019:         $grouplist = join(':',@usersgroups);
                   10020:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683     raeburn  10021:     }
1.733     raeburn  10022:     return @usersgroups;
1.683     raeburn  10023: }
                   10024: 
                   10025: sub devalidate_getgroups_cache {
                   10026:     my ($udom,$uname,$cdom,$cnum)=@_;
                   10027:     my $courseid = $cdom.'_'.$cnum;
1.807     albertel 10028: 
1.683     raeburn  10029:     my $hashid="$udom:$uname:$courseid";
                   10030:     &devalidate_cache_new('getgroups',$hashid);
                   10031: }
                   10032: 
1.12      www      10033: # ------------------------------------------------------------------ Plain Text
                   10034: 
                   10035: sub plaintext {
1.988     raeburn  10036:     my ($short,$type,$cid,$forcedefault) = @_;
1.1046    raeburn  10037:     if ($short =~ m{^cr/}) {
1.758     albertel 10038: 	return (split('/',$short))[-1];
                   10039:     }
1.742     raeburn  10040:     if (!defined($cid)) {
                   10041:         $cid = $env{'request.course.id'};
                   10042:     }
                   10043:     my %rolenames = (
1.1008    raeburn  10044:                       Course    => 'std',
                   10045:                       Community => 'alt1',
1.1305    raeburn  10046:                       Placement => 'std',
1.742     raeburn  10047:                     );
1.1037    raeburn  10048:     if ($cid ne '') {
                   10049:         if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
                   10050:             unless ($forcedefault) {
                   10051:                 my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'}; 
                   10052:                 &Apache::lonlocal::mt_escape(\$roletext);
                   10053:                 return &Apache::lonlocal::mt($roletext);
                   10054:             }
                   10055:         }
                   10056:     }
                   10057:     if ((defined($type)) && (defined($rolenames{$type})) &&
                   10058:         (defined($rolenames{$type})) && 
                   10059:         (defined($prp{$short}{$rolenames{$type}}))) {
1.742     raeburn  10060:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
1.1037    raeburn  10061:     } elsif ($cid ne '') {
                   10062:         my $crstype = $env{'course.'.$cid.'.type'};
                   10063:         if (($crstype ne '') && (defined($rolenames{$crstype})) &&
                   10064:             (defined($prp{$short}{$rolenames{$crstype}}))) {
                   10065:             return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
                   10066:         }
1.742     raeburn  10067:     }
1.1037    raeburn  10068:     return &Apache::lonlocal::mt($prp{$short}{'std'});
1.12      www      10069: }
                   10070: 
                   10071: # ----------------------------------------------------------------- Assign Role
                   10072: 
                   10073: sub assignrole {
1.957     raeburn  10074:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
                   10075:         $context)=@_;
1.21      www      10076:     my $mrole;
                   10077:     if ($role =~ /^cr\//) {
1.393     www      10078:         my $cwosec=$url;
1.811     albertel 10079:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.393     www      10080: 	unless (&allowed('ccr',$cwosec)) {
1.1026    raeburn  10081:            my $refused = 1;
                   10082:            if ($context eq 'requestcourses') {
                   10083:                if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
                   10084:                    if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                   10085:                        if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
                   10086:                            my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
                   10087:                            my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   10088:                            if ($crsenv{'internal.courseowner'} eq
                   10089:                                $env{'user.name'}.':'.$env{'user.domain'}) {
                   10090:                                $refused = '';
                   10091:                            }
                   10092:                        }
                   10093:                    }
                   10094:                }
                   10095:            }
                   10096:            if ($refused) {
                   10097:                &logthis('Refused custom assignrole: '.
                   10098:                         $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
                   10099:                         ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
                   10100:                return 'refused';
                   10101:            }
1.104     www      10102:         }
1.21      www      10103:         $mrole='cr';
1.678     raeburn  10104:     } elsif ($role =~ /^gr\//) {
                   10105:         my $cwogrp=$url;
1.811     albertel 10106:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.678     raeburn  10107:         unless (&allowed('mdg',$cwogrp)) {
                   10108:             &logthis('Refused group assignrole: '.
                   10109:               $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
                   10110:                     $env{'user.name'}.' at '.$env{'user.domain'});
                   10111:             return 'refused';
                   10112:         }
                   10113:         $mrole='gr';
1.21      www      10114:     } else {
1.82      www      10115:         my $cwosec=$url;
1.811     albertel 10116:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.932     raeburn  10117:         if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
                   10118:             my $refused;
                   10119:             if (($env{'request.course.sec'}  ne '') && ($role eq 'st')) {
                   10120:                 if (!(&allowed('c'.$role,$url))) {
                   10121:                     $refused = 1;
                   10122:                 }
                   10123:             } else {
                   10124:                 $refused = 1;
                   10125:             }
1.947     raeburn  10126:             if ($refused) {
1.1045    raeburn  10127:                 my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
1.1377    raeburn  10128:                 if (!$selfenroll && (($context eq 'course') || ($context eq 'ltienroll' && $env{'request.lti.login'}))) {
1.1045    raeburn  10129:                     my %crsenv;
                   10130:                     if ($role eq 'cc' || $role eq 'co') {
                   10131:                         %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   10132:                         if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
                   10133:                             if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
                   10134:                                 if ($crsenv{'internal.courseowner'} eq 
                   10135:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
                   10136:                                     $refused = '';
                   10137:                                 }
                   10138:                             }
                   10139:                         } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) { 
                   10140:                             if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
                   10141:                                 if ($crsenv{'internal.courseowner'} eq 
                   10142:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
                   10143:                                     $refused = '';
                   10144:                                 }
                   10145:                             }
                   10146:                         }
                   10147:                     }
1.1368    raeburn  10148:                 } elsif (($selfenroll == 1) && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
                   10149:                     if ($role eq 'st') {
                   10150:                         $refused = '';
1.1377    raeburn  10151:                     } elsif (($context eq 'ltienroll') && ($env{'request.lti.login'})) {
1.1368    raeburn  10152:                         $refused = '';
                   10153:                     }
1.1017    raeburn  10154:                 } elsif ($context eq 'requestcourses') {
1.1041    raeburn  10155:                     my @possroles = ('st','ta','ep','in','cc','co');
1.1026    raeburn  10156:                     if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
1.1041    raeburn  10157:                         my $wrongcc;
                   10158:                         if ($cnum =~ /^$match_community$/) {
                   10159:                             $wrongcc = 1 if ($role eq 'cc');
                   10160:                         } else {
                   10161:                             $wrongcc = 1 if ($role eq 'co');
                   10162:                         }
                   10163:                         unless ($wrongcc) {
                   10164:                             my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   10165:                             if ($crsenv{'internal.courseowner'} eq 
                   10166:                                  $env{'user.name'}.':'.$env{'user.domain'}) {
                   10167:                                 $refused = '';
                   10168:                             }
1.1017    raeburn  10169:                         }
                   10170:                     }
1.1183    raeburn  10171:                 } elsif ($context eq 'requestauthor') {
                   10172:                     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) && 
                   10173:                         ($url eq '/'.$udom.'/') && ($role eq 'au')) {
                   10174:                         if ($env{'environment.requestauthor'} eq 'automatic') {
                   10175:                             $refused = '';
                   10176:                         } else {
                   10177:                             my %domdefaults = &get_domain_defaults($udom);
                   10178:                             if (ref($domdefaults{'requestauthor'}) eq 'HASH') {
                   10179:                                 my $checkbystatus;
                   10180:                                 if ($env{'user.adv'}) { 
                   10181:                                     my $disposition = $domdefaults{'requestauthor'}{'_LC_adv'};
                   10182:                                     if ($disposition eq 'automatic') {
                   10183:                                         $refused = '';
                   10184:                                     } elsif ($disposition eq '') {
                   10185:                                         $checkbystatus = 1;
                   10186:                                     } 
                   10187:                                 } else {
                   10188:                                     $checkbystatus = 1;
                   10189:                                 }
                   10190:                                 if ($checkbystatus) {
                   10191:                                     if ($env{'environment.inststatus'}) {
                   10192:                                         my @inststatuses = split(/,/,$env{'environment.inststatus'});
                   10193:                                         foreach my $type (@inststatuses) {
                   10194:                                             if (($type ne '') &&
                   10195:                                                 ($domdefaults{'requestauthor'}{$type} eq 'automatic')) {
                   10196:                                                 $refused = '';
                   10197:                                             }
                   10198:                                         }
                   10199:                                     } elsif ($domdefaults{'requestauthor'}{'default'} eq 'automatic') {
                   10200:                                         $refused = '';
                   10201:                                     }
                   10202:                                 }
                   10203:                             }
                   10204:                         }
                   10205:                     }
1.1017    raeburn  10206:                 }
                   10207:                 if ($refused) {
1.947     raeburn  10208:                     &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
                   10209:                              ' '.$role.' '.$end.' '.$start.' by '.
                   10210: 	  	             $env{'user.name'}.' at '.$env{'user.domain'});
                   10211:                     return 'refused';
                   10212:                 }
1.932     raeburn  10213:             }
1.1131    raeburn  10214:         } elsif ($role eq 'au') {
                   10215:             if ($url ne '/'.$udom.'/') {
                   10216:                 &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
                   10217:                          ' to assign author role for '.$uname.':'.$udom.
                   10218:                          ' in domain: '.$url.' refused (wrong domain).');
                   10219:                 return 'refused';
                   10220:             }
1.104     www      10221:         }
1.21      www      10222:         $mrole=$role;
                   10223:     }
1.620     albertel 10224:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21      www      10225:                 "$udom:$uname:$url".'_'."$mrole=$role";
1.81      www      10226:     if ($end) { $command.='_'.$end; }
1.21      www      10227:     if ($start) {
                   10228: 	if ($end) { 
1.81      www      10229:            $command.='_'.$start; 
1.21      www      10230:         } else {
1.81      www      10231:            $command.='_0_'.$start;
1.21      www      10232:         }
                   10233:     }
1.739     raeburn  10234:     my $origstart = $start;
                   10235:     my $origend = $end;
1.957     raeburn  10236:     my $delflag;
1.357     www      10237: # actually delete
                   10238:     if ($deleteflag) {
1.373     www      10239: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357     www      10240: # modify command to delete the role
1.620     albertel 10241:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357     www      10242:                 "$udom:$uname:$url".'_'."$mrole";
1.620     albertel 10243: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
1.357     www      10244: # set start and finish to negative values for userrolelog
                   10245:            $start=-1;
                   10246:            $end=-1;
1.957     raeburn  10247:            $delflag = 1;
1.357     www      10248:         }
                   10249:     }
                   10250: # send command
1.349     www      10251:     my $answer=&reply($command,&homeserver($uname,$udom));
1.357     www      10252: # log new user role if status is ok
1.349     www      10253:     if ($answer eq 'ok') {
1.663     raeburn  10254: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
1.1184    raeburn  10255:         if (($role eq 'cc') || ($role eq 'in') ||
                   10256:             ($role eq 'ep') || ($role eq 'ad') ||
                   10257:             ($role eq 'ta') || ($role eq 'st') ||
                   10258:             ($role=~/^cr/) || ($role eq 'gr') ||
                   10259:             ($role eq 'co')) {
1.1200    raeburn  10260: # for course roles, perform group memberships changes triggered by role change.
                   10261:             unless ($role =~ /^gr/) {
                   10262:                 &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
                   10263:                                                  $origstart,$selfenroll,$context);
                   10264:             }
1.1184    raeburn  10265:             &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
                   10266:                            $selfenroll,$context);
                   10267:         } elsif (($role eq 'li') || ($role eq 'dg') || ($role eq 'sc') ||
1.1334    raeburn  10268:                  ($role eq 'au') || ($role eq 'dc') || ($role eq 'dh') ||
                   10269:                  ($role eq 'da')) {
1.1184    raeburn  10270:             &domainrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
                   10271:                            $context);
                   10272:         } elsif (($role eq 'ca') || ($role eq 'aa')) {
                   10273:             &coauthorrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
                   10274:                              $context); 
                   10275:         }
1.1053    raeburn  10276:         if ($role eq 'cc') {
                   10277:             &autoupdate_coowners($url,$end,$start,$uname,$udom);
                   10278:         }
1.349     www      10279:     }
                   10280:     return $answer;
1.169     harris41 10281: }
                   10282: 
1.1053    raeburn  10283: sub autoupdate_coowners {
                   10284:     my ($url,$end,$start,$uname,$udom) = @_;
                   10285:     my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
                   10286:     if (($cdom ne '') && ($cnum ne '')) {
                   10287:         my $now = time;
                   10288:         my %domdesign = &Apache::loncommon::get_domainconf($cdom);
                   10289:         if ($domdesign{$cdom.'.autoassign.co-owners'}) {
                   10290:             my %coursehash = &coursedescription($cdom.'_'.$cnum);
                   10291:             my $instcode = $coursehash{'internal.coursecode'};
1.1444    raeburn  10292:             my $xlists = $coursehash{'internal.crosslistings'};
1.1053    raeburn  10293:             if ($instcode ne '') {
1.1056    raeburn  10294:                 if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
                   10295:                     unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
1.1053    raeburn  10296:                         my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
1.1056    raeburn  10297:                         my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
1.1444    raeburn  10298:                         unless ($result eq 'valid') {
                   10299:                             if ($xlists ne '') {
                   10300:                                 foreach my $xlist (split(',',$xlists)) {
                   10301:                                     my ($inst_crosslist,$lcsec) = split(':',$xlist);
                   10302:                                     $result =
1.1446    raeburn  10303:                                         &auto_validate_inst_crosslist($cnum,$cdom,$instcode,
                   10304:                                                                       $inst_crosslist,$uname.':'.$udom);
                   10305:                                     last if ($result eq 'valid');
1.1444    raeburn  10306:                                 }
                   10307:                             }
                   10308:                         }
1.1056    raeburn  10309:                         if ($result eq 'valid') {
                   10310:                             if ($coursehash{'internal.co-owners'}) {
1.1053    raeburn  10311:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
                   10312:                                     push(@newcoowners,$coowner);
                   10313:                                 }
                   10314:                                 unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
                   10315:                                     push(@newcoowners,$uname.':'.$udom);
                   10316:                                 }
                   10317:                                 @newcoowners = sort(@newcoowners);
                   10318:                             } else {
                   10319:                                 push(@newcoowners,$uname.':'.$udom);
                   10320:                             }
1.1444    raeburn  10321:                         } elsif ($coursehash{'internal.co-owners'}) {
                   10322:                             foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
                   10323:                                 unless ($coowner eq $uname.':'.$udom) {
                   10324:                                     push(@newcoowners,$coowner);
1.1053    raeburn  10325:                                 }
1.1444    raeburn  10326:                             }
                   10327:                             unless (@newcoowners > 0) {
                   10328:                                 $delcoowners = 1;
                   10329:                                 $coowners = '';
1.1053    raeburn  10330:                             }
                   10331:                         }
                   10332:                         if (@newcoowners || $delcoowners) {
                   10333:                             &store_coowners($cdom,$cnum,$coursehash{'home'},
                   10334:                                             $delcoowners,@newcoowners);
                   10335:                         }
                   10336:                     }
                   10337:                 }
                   10338:             }
                   10339:         }
                   10340:     }
                   10341: }
                   10342: 
                   10343: sub store_coowners {
                   10344:     my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
                   10345:     my $cid = $cdom.'_'.$cnum;
                   10346:     my ($coowners,$delresult,$putresult);
                   10347:     if (@newcoowners) {
                   10348:         $coowners = join(',',@newcoowners);
                   10349:         my %coownershash = (
                   10350:                             'internal.co-owners' => $coowners,
                   10351:                            );
                   10352:         $putresult = &put('environment',\%coownershash,$cdom,$cnum);
                   10353:         if ($putresult eq 'ok') {
                   10354:             if ($env{'course.'.$cid.'.num'} eq $cnum) {
                   10355:                 &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
                   10356:             }
                   10357:         }
                   10358:     }
                   10359:     if ($delcoowners) {
                   10360:         $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
                   10361:         if ($delresult eq 'ok') {
                   10362:             if ($env{'course.'.$cid.'.internal.co-owners'}) {
                   10363:                 &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
                   10364:             }
                   10365:         }
                   10366:     }
                   10367:     if (($putresult eq 'ok') || ($delresult eq 'ok')) {
                   10368:         my %crsinfo =
                   10369:             &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
                   10370:         if (ref($crsinfo{$cid}) eq 'HASH') {
                   10371:             $crsinfo{$cid}{'co-owners'} = \@newcoowners;
                   10372:             my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
                   10373:         }
                   10374:     }
                   10375: }
                   10376: 
1.169     harris41 10377: # -------------------------------------------------- Modify user authentication
1.197     www      10378: # Overrides without validation
                   10379: 
1.169     harris41 10380: sub modifyuserauth {
                   10381:     my ($udom,$uname,$umode,$upass)=@_;
                   10382:     my $uhome=&homeserver($uname,$udom);
1.1409    raeburn  10383:     my $allowed;
                   10384:     if (&allowed('mau',$udom)) {
                   10385:         $allowed = 1;
                   10386:     } elsif (($umode eq 'internal') && ($udom eq $env{'user.domain'}) &&
                   10387:              ($env{'request.course.id'}) && (&allowed('mip',$env{'request.course.id'})) &&
                   10388:              (!$env{'course.'.$env{'request.course.id'}.'.internal.nopasswdchg'})) {
                   10389:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   10390:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   10391:         if (($cdom ne '') && ($cnum ne '')) {
                   10392:             my $is_owner = &is_course_owner($cdom,$cnum);
                   10393:             if ($is_owner) {
                   10394:                 $allowed = 1;
                   10395:             }
                   10396:         }
                   10397:     }
                   10398:     unless ($allowed) { return 'refused'; }
1.197     www      10399:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620     albertel 10400:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   10401:              ' in domain '.$env{'request.role.domain'});  
1.169     harris41 10402:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
                   10403: 		     &escape($upass),$uhome);
1.1434    raeburn  10404:     my $ip = &get_requestor_ip();
1.620     albertel 10405:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197     www      10406:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
1.1434    raeburn  10407:          '(Remote '.$ip.'): '.$reply);
1.197     www      10408:     &log($udom,,$uname,$uhome,
1.620     albertel 10409:         'Authentication changed by '.$env{'user.domain'}.', '.
                   10410:                                      $env{'user.name'}.', '.$umode.
1.1435    raeburn  10411:          '(Remote '.$ip.'): '.$reply);
1.169     harris41 10412:     unless ($reply eq 'ok') {
1.197     www      10413:         &logthis('Authentication mode error: '.$reply);
1.169     harris41 10414: 	return 'error: '.$reply;
                   10415:     }   
1.170     harris41 10416:     return 'ok';
1.80      www      10417: }
                   10418: 
1.81      www      10419: # --------------------------------------------------------------- Modify a user
1.80      www      10420: 
1.81      www      10421: sub modifyuser {
1.206     matthew  10422:     my ($udom,    $uname, $uid,
                   10423:         $umode,   $upass, $first,
                   10424:         $middle,  $last,  $gene,
1.1058    raeburn  10425:         $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
1.807     albertel 10426:     $udom= &LONCAPA::clean_domain($udom);
                   10427:     $uname=&LONCAPA::clean_username($uname);
1.1059    raeburn  10428:     my $showcandelete = 'none';
                   10429:     if (ref($candelete) eq 'ARRAY') {
                   10430:         if (@{$candelete} > 0) {
                   10431:             $showcandelete = join(', ',@{$candelete});
                   10432:         }
                   10433:     }
1.81      www      10434:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80      www      10435:              $umode.', '.$first.', '.$middle.', '.
1.1059    raeburn  10436: 	     $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
1.206     matthew  10437:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
                   10438:                                      ' desiredhome not specified'). 
1.620     albertel 10439:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   10440:              ' in domain '.$env{'request.role.domain'});
1.230     stredwic 10441:     my $uhome=&homeserver($uname,$udom,'true');
1.1075    raeburn  10442:     my $newuser;
                   10443:     if ($uhome eq 'no_host') {
                   10444:         $newuser = 1;
1.1368    raeburn  10445:         unless (($umode && ($upass ne '')) || ($umode eq 'localauth') ||
                   10446:                 ($umode eq 'lti')) {
                   10447:             return 'error: more information needed to create new user';
                   10448:         }
1.1075    raeburn  10449:     }
1.80      www      10450: # ----------------------------------------------------------------- Create User
1.406     albertel 10451:     if (($uhome eq 'no_host') && 
1.1368    raeburn  10452: 	(($umode && $upass) || ($umode eq 'localauth') || ($umode eq 'lti'))) {
1.80      www      10453:         my $unhome='';
1.844     albertel 10454:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
1.209     matthew  10455:             $unhome = $desiredhome;
1.620     albertel 10456: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
                   10457: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209     matthew  10458:         } else { # load balancing routine for determining $unhome
1.81      www      10459:             my $loadm=10000000;
1.841     albertel 10460: 	    my %servers = &get_servers($udom,'library');
                   10461: 	    foreach my $tryserver (keys(%servers)) {
                   10462: 		my $answer=reply('load',$tryserver);
                   10463: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
                   10464: 		    $loadm=$answer;
                   10465: 		    $unhome=$tryserver;
                   10466: 		}
1.80      www      10467: 	    }
                   10468:         }
                   10469:         if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206     matthew  10470: 	    return 'error: unable to find a home server for '.$uname.
                   10471:                    ' in domain '.$udom;
1.80      www      10472:         }
                   10473:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
                   10474:                          &escape($upass),$unhome);
                   10475: 	unless ($reply eq 'ok') {
                   10476:             return 'error: '.$reply;
                   10477:         }   
1.230     stredwic 10478:         $uhome=&homeserver($uname,$udom,'true');
1.80      www      10479:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386     matthew  10480: 	    return 'error: unable verify users home machine.';
1.80      www      10481:         }
1.209     matthew  10482:     }   # End of creation of new user
1.80      www      10483: # ---------------------------------------------------------------------- Add ID
                   10484:     if ($uid) {
                   10485:        $uid=~tr/A-Z/a-z/;
                   10486:        my %uidhash=&idrget($udom,$uname);
1.196     www      10487:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
                   10488:          && (!$forceid)) {
1.80      www      10489: 	  unless ($uid eq $uidhash{$uname}) {
1.386     matthew  10490: 	      return 'error: user id "'.$uid.'" does not match '.
                   10491:                   'current user id "'.$uidhash{$uname}.'".';
1.80      www      10492:           }
                   10493:        } else {
1.1300    raeburn  10494: 	  &idput($udom,{$uname => $uid},$uhome,'ids');
1.80      www      10495:        }
                   10496:     }
                   10497: # -------------------------------------------------------------- Add names, etc
1.313     matthew  10498:     my @tmp=&get('environment',
1.899     raeburn  10499: 		   ['firstname','middlename','lastname','generation','id',
1.963     raeburn  10500:                     'permanentemail','inststatus'],
1.134     albertel 10501: 		   $udom,$uname);
1.1075    raeburn  10502:     my (%names,%oldnames);
1.313     matthew  10503:     if ($tmp[0] =~ m/^error:.*/) { 
                   10504:         %names=(); 
                   10505:     } else {
                   10506:         %names = @tmp;
1.1075    raeburn  10507:         %oldnames = %names;
1.313     matthew  10508:     }
1.388     www      10509: #
1.1058    raeburn  10510: # If name, email and/or uid are blank (e.g., because an uploaded file
                   10511: # of users did not contain them), do not overwrite existing values
                   10512: # unless field is in $candelete array ref.  
                   10513: #
                   10514: 
                   10515:     my @fields = ('firstname','middlename','lastname','generation',
                   10516:                   'permanentemail','id');
                   10517:     my %newvalues;
                   10518:     if (ref($candelete) eq 'ARRAY') {
                   10519:         foreach my $field (@fields) {
                   10520:             if (grep(/^\Q$field\E$/,@{$candelete})) {
                   10521:                 if ($field eq 'firstname') {
                   10522:                     $names{$field} = $first;
                   10523:                 } elsif ($field eq 'middlename') {
                   10524:                     $names{$field} = $middle;
                   10525:                 } elsif ($field eq 'lastname') {
                   10526:                     $names{$field} = $last;
                   10527:                 } elsif ($field eq 'generation') { 
                   10528:                     $names{$field} = $gene;
                   10529:                 } elsif ($field eq 'permanentemail') {
                   10530:                     $names{$field} = $email;
                   10531:                 } elsif ($field eq 'id') {
                   10532:                     $names{$field}  = $uid;
                   10533:                 }
                   10534:             }
                   10535:         }
                   10536:     }
1.388     www      10537:     if ($first)  { $names{'firstname'}  = $first; }
1.385     matthew  10538:     if (defined($middle)) { $names{'middlename'} = $middle; }
1.388     www      10539:     if ($last)   { $names{'lastname'}   = $last; }
1.385     matthew  10540:     if (defined($gene))   { $names{'generation'} = $gene; }
1.592     www      10541:     if ($email) {
                   10542:        $email=~s/[^\w\@\.\-\,]//gs;
1.963     raeburn  10543:        if ($email=~/\@/) { $names{'permanentemail'} = $email; }
1.592     www      10544:     }
1.899     raeburn  10545:     if ($uid) { $names{'id'}  = $uid; }
1.989     raeburn  10546:     if (defined($inststatus)) {
                   10547:         $names{'inststatus'} = '';
                   10548:         my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
                   10549:         if (ref($usertypes) eq 'HASH') {
                   10550:             my @okstatuses; 
                   10551:             foreach my $item (split(/:/,$inststatus)) {
                   10552:                 if (defined($usertypes->{$item})) {
                   10553:                     push(@okstatuses,$item);  
                   10554:                 }
                   10555:             }
                   10556:             if (@okstatuses) {
                   10557:                 $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
                   10558:             }
                   10559:         }
                   10560:     }
1.1075    raeburn  10561:     my $logmsg = $udom.', '.$uname.', '.$uid.', '.
1.963     raeburn  10562:                  $umode.', '.$first.', '.$middle.', '.
1.1075    raeburn  10563:                  $last.', '.$gene.', '.$email.', '.$inststatus;
1.963     raeburn  10564:     if ($env{'user.name'} ne '' && $env{'user.domain'}) {
                   10565:         $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
                   10566:     } else {
                   10567:         $logmsg .= ' during self creation';
                   10568:     }
1.1075    raeburn  10569:     my $changed;
                   10570:     if ($newuser) {
                   10571:         $changed = 1;
                   10572:     } else {
                   10573:         foreach my $field (@fields) {
                   10574:             if ($names{$field} ne $oldnames{$field}) {
                   10575:                 $changed = 1;
                   10576:                 last;
                   10577:             }
                   10578:         }
                   10579:     }
                   10580:     unless ($changed) {
                   10581:         $logmsg = 'No changes in user information needed for: '.$logmsg;
                   10582:         &logthis($logmsg);
                   10583:         return 'ok';
                   10584:     }
                   10585:     my $reply = &put('environment', \%names, $udom,$uname);
                   10586:     if ($reply ne 'ok') { 
                   10587:         return 'error: '.$reply;
                   10588:     }
1.1087    raeburn  10589:     if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
                   10590:         &Apache::lonnet::devalidate_cache_new('emailscache',$uname.':'.$udom);
                   10591:     }
1.1075    raeburn  10592:     my $sqlresult = &update_allusers_table($uname,$udom,\%names);
                   10593:     &devalidate_cache_new('namescache',$uname.':'.$udom);
                   10594:     $logmsg = 'Success modifying user '.$logmsg;
1.963     raeburn  10595:     &logthis($logmsg);
1.134     albertel 10596:     return 'ok';
1.80      www      10597: }
                   10598: 
1.81      www      10599: # -------------------------------------------------------------- Modify student
1.80      www      10600: 
1.81      www      10601: sub modifystudent {
                   10602:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.957     raeburn  10603:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
1.1314    raeburn  10604:         $selfenroll,$context,$inststatus,$credits,$instsec)=@_;
1.455     albertel 10605:     if (!$cid) {
1.620     albertel 10606: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 10607: 	    return 'not_in_class';
                   10608: 	}
1.80      www      10609:     }
                   10610: # --------------------------------------------------------------- Make the user
1.81      www      10611:     my $reply=&modifyuser
1.209     matthew  10612: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.990     raeburn  10613:          $desiredhome,$email,$inststatus);
1.80      www      10614:     unless ($reply eq 'ok') { return $reply; }
1.297     matthew  10615:     # This will cause &modify_student_enrollment to get the uid from the
1.1235    raeburn  10616:     # student's environment
1.297     matthew  10617:     $uid = undef if (!$forceid);
1.455     albertel 10618:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.1216    raeburn  10619:                                         $gene,$usec,$end,$start,$type,$locktype,
1.1314    raeburn  10620:                                         $cid,$selfenroll,$context,$credits,$instsec);
1.297     matthew  10621:     return $reply;
                   10622: }
                   10623: 
                   10624: sub modify_student_enrollment {
1.1216    raeburn  10625:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,
1.1314    raeburn  10626:         $locktype,$cid,$selfenroll,$context,$credits,$instsec) = @_;
1.455     albertel 10627:     my ($cdom,$cnum,$chome);
                   10628:     if (!$cid) {
1.620     albertel 10629: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 10630: 	    return 'not_in_class';
                   10631: 	}
1.620     albertel 10632: 	$cdom=$env{'course.'.$cid.'.domain'};
                   10633: 	$cnum=$env{'course.'.$cid.'.num'};
1.455     albertel 10634:     } else {
                   10635: 	($cdom,$cnum)=split(/_/,$cid);
                   10636:     }
1.620     albertel 10637:     $chome=$env{'course.'.$cid.'.home'};
1.455     albertel 10638:     if (!$chome) {
1.457     raeburn  10639: 	$chome=&homeserver($cnum,$cdom);
1.297     matthew  10640:     }
1.455     albertel 10641:     if (!$chome) { return 'unknown_course'; }
1.297     matthew  10642:     # Make sure the user exists
1.81      www      10643:     my $uhome=&homeserver($uname,$udom);
                   10644:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   10645: 	return 'error: no such user';
                   10646:     }
1.297     matthew  10647:     # Get student data if we were not given enough information
                   10648:     if (!defined($first)  || $first  eq '' || 
                   10649:         !defined($last)   || $last   eq '' || 
                   10650:         !defined($uid)    || $uid    eq '' || 
                   10651:         !defined($middle) || $middle eq '' || 
                   10652:         !defined($gene)   || $gene   eq '') {
1.294     matthew  10653:         # They did not supply us with enough data to enroll the student, so
                   10654:         # we need to pick up more information.
1.297     matthew  10655:         my %tmp = &get('environment',
1.294     matthew  10656:                        ['firstname','middlename','lastname', 'generation','id']
1.297     matthew  10657:                        ,$udom,$uname);
                   10658: 
1.800     albertel 10659:         #foreach my $key (keys(%tmp)) {
                   10660:         #    &logthis("key $key = ".$tmp{$key});
1.455     albertel 10661:         #}
1.294     matthew  10662:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
                   10663:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
                   10664:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
1.297     matthew  10665:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
1.294     matthew  10666:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
                   10667:     }
1.556     albertel 10668:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.1148    raeburn  10669:     my $user = "$uname:$udom";
                   10670:     my %old_entry = &Apache::lonnet::get('classlist',[$user],$cdom,$cnum);
1.487     albertel 10671:     my $reply=cput('classlist',
1.1148    raeburn  10672: 		   {$user => 
1.1314    raeburn  10673: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype,$credits,$instsec) },
1.487     albertel 10674: 		   $cdom,$cnum);
1.1148    raeburn  10675:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
                   10676:         &devalidate_getsection_cache($udom,$uname,$cid);
                   10677:     } else { 
1.81      www      10678: 	return 'error: '.$reply;
                   10679:     }
1.297     matthew  10680:     # Add student role to user
1.83      www      10681:     my $uurl='/'.$cid;
1.81      www      10682:     $uurl=~s/\_/\//g;
                   10683:     if ($usec) {
                   10684: 	$uurl.='/'.$usec;
                   10685:     }
1.1148    raeburn  10686:     my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
                   10687:                              $selfenroll,$context);
                   10688:     if ($result ne 'ok') {
                   10689:         if ($old_entry{$user} ne '') {
                   10690:             $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
                   10691:         } else {
                   10692:             $reply = &del('classlist',[$user],$cdom,$cnum);
                   10693:         }
                   10694:     }
                   10695:     return $result; 
1.21      www      10696: }
                   10697: 
1.556     albertel 10698: sub format_name {
                   10699:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
                   10700:     my $name;
                   10701:     if ($first ne 'lastname') {
                   10702: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
                   10703:     } else {
                   10704: 	if ($lastname=~/\S/) {
                   10705: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
                   10706: 	    $name=~s/\s+,/,/;
                   10707: 	} else {
                   10708: 	    $name.= $firstname.' '.$middlename.' '.$generation;
                   10709: 	}
                   10710:     }
                   10711:     $name=~s/^\s+//;
                   10712:     $name=~s/\s+$//;
                   10713:     $name=~s/\s+/ /g;
                   10714:     return $name;
                   10715: }
                   10716: 
1.84      www      10717: # ------------------------------------------------- Write to course preferences
                   10718: 
                   10719: sub writecoursepref {
                   10720:     my ($courseid,%prefs)=@_;
                   10721:     $courseid=~s/^\///;
                   10722:     $courseid=~s/\_/\//g;
                   10723:     my ($cdomain,$cnum)=split(/\//,$courseid);
                   10724:     my $chome=homeserver($cnum,$cdomain);
                   10725:     if (($chome eq '') || ($chome eq 'no_host')) { 
                   10726: 	return 'error: no such course';
                   10727:     }
                   10728:     my $cstring='';
1.800     albertel 10729:     foreach my $pref (keys(%prefs)) {
                   10730: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191     harris41 10731:     }
1.84      www      10732:     $cstring=~s/\&$//;
                   10733:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
                   10734: }
                   10735: 
                   10736: # ---------------------------------------------------------- Make/modify course
                   10737: 
                   10738: sub createcourse {
1.741     raeburn  10739:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
1.1423    raeburn  10740:         $course_owner,$crstype,$cnum,$context,$category,$callercontext)=@_;
1.84      www      10741:     $url=&declutter($url);
                   10742:     my $cid='';
1.1028    raeburn  10743:     if ($context eq 'requestcourses') {
                   10744:         my $can_create = 0;
                   10745:         my ($ownername,$ownerdom) = split(':',$course_owner);
                   10746:         if ($udom eq $ownerdom) {
1.1423    raeburn  10747:             my $reload;
                   10748:             if (($callercontext eq 'auto') &&
                   10749:                ($ownerdom eq $env{'user.domain'}) && ($ownername eq $env{'user.name'})) {
                   10750:                 $reload = 'reload';
                   10751:             }
                   10752:             if (&usertools_access($ownername,$ownerdom,$category,$reload,
1.1028    raeburn  10753:                                   $context)) {
                   10754:                 $can_create = 1;
                   10755:             }
                   10756:         } else {
                   10757:             my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
                   10758:                                            $category);
                   10759:             if ($userenv{'reqcrsotherdom.'.$category} ne '') {
                   10760:                 my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
                   10761:                 if (@curr > 0) {
                   10762:                     my @options = qw(approval validate autolimit);
                   10763:                     my $optregex = join('|',@options);
                   10764:                     if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
                   10765:                         $can_create = 1;
                   10766:                     }
                   10767:                 }
                   10768:             }
                   10769:         }
                   10770:         if ($can_create) {
                   10771:             unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
                   10772:                 unless (&allowed('ccc',$udom)) {
                   10773:                     return 'refused'; 
                   10774:                 }
1.1017    raeburn  10775:             }
                   10776:         } else {
                   10777:             return 'refused';
                   10778:         }
1.1028    raeburn  10779:     } elsif (!&allowed('ccc',$udom)) {
                   10780:         return 'refused';
1.84      www      10781:     }
1.1011    raeburn  10782: # --------------------------------------------------------------- Get Unique ID
                   10783:     my $uname;
                   10784:     if ($cnum =~ /^$match_courseid$/) {
                   10785:         my $chome=&homeserver($cnum,$udom,'true');
                   10786:         if (($chome eq '') || ($chome eq 'no_host')) {
                   10787:             $uname = $cnum;
                   10788:         } else {
1.1038    raeburn  10789:             $uname = &generate_coursenum($udom,$crstype);
1.1011    raeburn  10790:         }
                   10791:     } else {
1.1038    raeburn  10792:         $uname = &generate_coursenum($udom,$crstype);
1.1011    raeburn  10793:     }
                   10794:     return $uname if ($uname =~ /^error/);
                   10795: # -------------------------------------------------- Check supplied server name
1.1052    raeburn  10796:     if (!defined($course_server)) {
                   10797:         if (defined(&domain($udom,'primary'))) {
                   10798:             $course_server = &domain($udom,'primary');
                   10799:         } else {
                   10800:             $course_server = $env{'user.home'}; 
                   10801:         }
                   10802:     }
                   10803:     my %host_servers =
                   10804:         &Apache::lonnet::get_servers($udom,'library');
                   10805:     unless ($host_servers{$course_server}) {
                   10806:         return 'error: invalid home server for course: '.$course_server;
1.264     matthew  10807:     }
1.84      www      10808: # ------------------------------------------------------------- Make the course
                   10809:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264     matthew  10810:                       $course_server);
1.84      www      10811:     unless ($reply eq 'ok') { return 'error: '.$reply; }
1.1011    raeburn  10812:     my $uhome=&homeserver($uname,$udom,'true');
1.84      www      10813:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   10814: 	return 'error: no such course';
                   10815:     }
1.271     www      10816: # ----------------------------------------------------------------- Course made
1.516     raeburn  10817: # log existence
1.1029    raeburn  10818:     my $now = time;
1.918     raeburn  10819:     my $newcourse = {
                   10820:                     $udom.'_'.$uname => {
1.921     raeburn  10821:                                      description => $description,
                   10822:                                      inst_code   => $inst_code,
                   10823:                                      owner       => $course_owner,
                   10824:                                      type        => $crstype,
1.1029    raeburn  10825:                                      creator     => $env{'user.name'}.':'.
                   10826:                                                     $env{'user.domain'},
                   10827:                                      created     => $now,
                   10828:                                      context     => $context,
1.918     raeburn  10829:                                                 },
                   10830:                     };
1.921     raeburn  10831:     &courseidput($udom,$newcourse,$uhome,'notime');
1.358     www      10832: # set toplevel url
1.271     www      10833:     my $topurl=$url;
                   10834:     unless ($nonstandard) {
                   10835: # ------------------------------------------ For standard courses, make top url
                   10836:         my $mapurl=&clutter($url);
1.278     www      10837:         if ($mapurl eq '/res/') { $mapurl=''; }
1.620     albertel 10838:         $env{'form.initmap'}=(<<ENDINITMAP);
1.271     www      10839: <map>
                   10840: <resource id="1" type="start"></resource>
                   10841: <resource id="2" src="$mapurl"></resource>
                   10842: <resource id="3" type="finish"></resource>
                   10843: <link index="1" from="1" to="2"></link>
                   10844: <link index="2" from="2" to="3"></link>
                   10845: </map>
                   10846: ENDINITMAP
                   10847:         $topurl=&declutter(
1.638     albertel 10848:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271     www      10849:                           );
                   10850:     }
                   10851: # ----------------------------------------------------------- Write preferences
1.84      www      10852:     &writecoursepref($udom.'_'.$uname,
1.1056    raeburn  10853:                      ('description'              => $description,
                   10854:                       'url'                      => $topurl,
                   10855:                       'internal.creator'         => $env{'user.name'}.':'.
                   10856:                                                     $env{'user.domain'},
                   10857:                       'internal.created'         => $now,
                   10858:                       'internal.creationcontext' => $context)
                   10859:                     );
1.84      www      10860:     return '/'.$udom.'/'.$uname;
                   10861: }
                   10862: 
1.1011    raeburn  10863: # ------------------------------------------------------------------- Create ID
                   10864: sub generate_coursenum {
1.1038    raeburn  10865:     my ($udom,$crstype) = @_;
1.1011    raeburn  10866:     my $domdesc = &domain($udom);
                   10867:     return 'error: invalid domain' if ($domdesc eq '');
1.1038    raeburn  10868:     my $first;
                   10869:     if ($crstype eq 'Community') {
                   10870:         $first = '0';
                   10871:     } else {
                   10872:         $first = int(1+rand(9)); 
                   10873:     } 
                   10874:     my $uname=$first.
1.1011    raeburn  10875:         ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
                   10876:         substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   10877:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
                   10878: # ----------------------------------------------- Make sure that does not exist
                   10879:     my $uhome=&homeserver($uname,$udom,'true');
                   10880:     unless (($uhome eq '') || ($uhome eq 'no_host')) {
1.1038    raeburn  10881:         if ($crstype eq 'Community') {
                   10882:             $first = '0';
                   10883:         } else {
                   10884:             $first = int(1+rand(9));
                   10885:         }
                   10886:         $uname=$first.
1.1011    raeburn  10887:                ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
                   10888:                substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   10889:                unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
                   10890:         $uhome=&homeserver($uname,$udom,'true');
                   10891:         unless (($uhome eq '') || ($uhome eq 'no_host')) {
                   10892:             return 'error: unable to generate unique course-ID';
                   10893:         }
                   10894:     }
                   10895:     return $uname;
                   10896: }
                   10897: 
1.813     albertel 10898: sub is_course {
1.1167    droeschl 10899:     my ($cdom, $cnum) = scalar(@_) == 1 ? 
                   10900:          ($_[0] =~ /^($match_domain)_($match_courseid)$/)  :  @_;
                   10901: 
1.1381    raeburn  10902:     return unless (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/));
                   10903:     my $uhome=&homeserver($cnum,$cdom);
                   10904:     my $iscourse;
                   10905:     if (grep { $_ eq $uhome } current_machine_ids()) {
1.1382    raeburn  10906:         $iscourse = &LONCAPA::Lond::is_course($cdom,$cnum);
1.1381    raeburn  10907:     } else {
                   10908:         my $hashid = $cdom.':'.$cnum;
                   10909:         ($iscourse,my $cached) = &is_cached_new('iscourse',$hashid);
                   10910:         unless (defined($cached)) {
                   10911:             my %courses = &courseiddump($cdom, '.', 1, '.', '.',
                   10912:                                         $cnum,undef,undef,'.');
                   10913:             $iscourse = 0;
                   10914:             if (exists($courses{$cdom.'_'.$cnum})) {
                   10915:                 $iscourse = 1;
                   10916:             }
                   10917:             &do_cache_new('iscourse',$hashid,$iscourse,3600);
                   10918:         }
                   10919:     }
                   10920:     return unless ($iscourse);
1.1167    droeschl 10921:     return wantarray ? ($cdom, $cnum) : $cdom.'_'.$cnum;
1.813     albertel 10922: }
                   10923: 
1.1015    raeburn  10924: sub store_userdata {
                   10925:     my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
1.1013    raeburn  10926:     my $result;
1.1016    raeburn  10927:     if ($datakey ne '') {
1.1013    raeburn  10928:         if (ref($storehash) eq 'HASH') {
1.1017    raeburn  10929:             if ($udom eq '' || $uname eq '') {
                   10930:                 $udom = $env{'user.domain'};
                   10931:                 $uname = $env{'user.name'};
                   10932:             }
                   10933:             my $uhome=&homeserver($uname,$udom);
1.1013    raeburn  10934:             if (($uhome eq '') || ($uhome eq 'no_host')) {
                   10935:                 $result = 'error: no_host';
                   10936:             } else {
1.1434    raeburn  10937:                 $storehash->{'ip'} = &get_requestor_ip();
1.1013    raeburn  10938:                 $storehash->{'host'} = $perlvar{'lonHostID'};
                   10939: 
                   10940:                 my $namevalue='';
                   10941:                 foreach my $key (keys(%{$storehash})) {
                   10942:                     $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
                   10943:                 }
                   10944:                 $namevalue=~s/\&$//;
1.1224    raeburn  10945:                 unless ($namespace eq 'courserequests') {
                   10946:                     $datakey = &escape($datakey);
                   10947:                 }
1.1105    raeburn  10948:                 $result =  &reply("store:$udom:$uname:$namespace:$datakey:".
                   10949:                                   $namevalue,$uhome);
1.1013    raeburn  10950:             }
                   10951:         } else {
                   10952:             $result = 'error: data to store was not a hash reference'; 
                   10953:         }
                   10954:     } else {
                   10955:         $result= 'error: invalid requestkey'; 
                   10956:     }
                   10957:     return $result;
                   10958: }
                   10959: 
1.21      www      10960: # ---------------------------------------------------------- Assign Custom Role
                   10961: 
                   10962: sub assigncustomrole {
1.957     raeburn  10963:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
1.21      www      10964:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.957     raeburn  10965:                        $end,$start,$deleteflag,$selfenroll,$context);
1.21      www      10966: }
                   10967: 
                   10968: # ----------------------------------------------------------------- Revoke Role
                   10969: 
                   10970: sub revokerole {
1.957     raeburn  10971:     my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
1.21      www      10972:     my $now=time;
1.965     raeburn  10973:     return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
1.21      www      10974: }
                   10975: 
                   10976: # ---------------------------------------------------------- Revoke Custom Role
                   10977: 
                   10978: sub revokecustomrole {
1.957     raeburn  10979:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
1.21      www      10980:     my $now=time;
1.357     www      10981:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
1.957     raeburn  10982:            $deleteflag,$selfenroll,$context);
1.17      www      10983: }
                   10984: 
1.533     banghart 10985: # ------------------------------------------------------------ Disk usage
1.535     albertel 10986: sub diskusage {
1.955     raeburn  10987:     my ($udom,$uname,$directorypath,$getpropath)=@_;
                   10988:     $directorypath =~ s/\/$//;
                   10989:     my $listing=&reply('du2:'.&escape($directorypath).':'
                   10990:                        .&escape($getpropath).':'.&escape($uname).':'
                   10991:                        .&escape($udom),homeserver($uname,$udom));
                   10992:     if ($listing eq 'unknown_cmd') {
                   10993:         if ($getpropath) {
                   10994:             $directorypath = &propath($udom,$uname).'/'.$directorypath; 
                   10995:         }
                   10996:         $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
                   10997:     }
1.514     albertel 10998:     return $listing;
1.512     banghart 10999: }
                   11000: 
1.566     banghart 11001: sub is_locked {
1.1096    raeburn  11002:     my ($file_name, $domain, $user, $which) = @_;
1.566     banghart 11003:     my @check;
                   11004:     my $is_locked;
1.1093    raeburn  11005:     push (@check,$file_name);
1.613     albertel 11006:     my %locked = &get('file_permissions',\@check,
1.620     albertel 11007: 		      $env{'user.domain'},$env{'user.name'});
1.615     albertel 11008:     my ($tmp)=keys(%locked);
                   11009:     if ($tmp=~/^error:/) { undef(%locked); }
1.745     raeburn  11010:     
1.566     banghart 11011:     if (ref($locked{$file_name}) eq 'ARRAY') {
1.745     raeburn  11012:         $is_locked = 'false';
                   11013:         foreach my $entry (@{$locked{$file_name}}) {
1.1096    raeburn  11014:            if (ref($entry) eq 'ARRAY') {
1.746     raeburn  11015:                $is_locked = 'true';
1.1096    raeburn  11016:                if (ref($which) eq 'ARRAY') {
                   11017:                    push(@{$which},$entry);
                   11018:                } else {
                   11019:                    last;
                   11020:                }
1.745     raeburn  11021:            }
                   11022:        }
1.566     banghart 11023:     } else {
                   11024:         $is_locked = 'false';
                   11025:     }
1.1093    raeburn  11026:     return $is_locked;
1.566     banghart 11027: }
                   11028: 
1.759     albertel 11029: sub declutter_portfile {
                   11030:     my ($file) = @_;
1.833     albertel 11031:     $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759     albertel 11032:     return $file;
                   11033: }
                   11034: 
1.559     banghart 11035: # ------------------------------------------------------------- Mark as Read Only
                   11036: 
                   11037: sub mark_as_readonly {
                   11038:     my ($domain,$user,$files,$what) = @_;
1.613     albertel 11039:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 11040:     my ($tmp)=keys(%current_permissions);
                   11041:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560     banghart 11042:     foreach my $file (@{$files}) {
1.759     albertel 11043: 	$file = &declutter_portfile($file);
1.561     banghart 11044:         push(@{$current_permissions{$file}},$what);
1.559     banghart 11045:     }
1.613     albertel 11046:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 11047:     return;
                   11048: }
                   11049: 
1.572     banghart 11050: # ------------------------------------------------------------Save Selected Files
                   11051: 
                   11052: sub save_selected_files {
                   11053:     my ($user, $path, @files) = @_;
                   11054:     my $filename = $user."savedfiles";
1.573     banghart 11055:     my @other_files = &files_not_in_path($user, $path);
1.1359    raeburn  11056:     open (OUT,'>',LONCAPA::tempdir().$filename);
1.573     banghart 11057:     foreach my $file (@files) {
1.620     albertel 11058:         print (OUT $env{'form.currentpath'}.$file."\n");
1.573     banghart 11059:     }
                   11060:     foreach my $file (@other_files) {
1.574     banghart 11061:         print (OUT $file."\n");
1.572     banghart 11062:     }
1.574     banghart 11063:     close (OUT);
1.572     banghart 11064:     return 'ok';
                   11065: }
                   11066: 
1.574     banghart 11067: sub clear_selected_files {
                   11068:     my ($user) = @_;
                   11069:     my $filename = $user."savedfiles";
1.1359    raeburn  11070:     open (OUT,'>',LONCAPA::tempdir().$filename);
1.574     banghart 11071:     print (OUT undef);
                   11072:     close (OUT);
                   11073:     return ("ok");    
                   11074: }
                   11075: 
1.572     banghart 11076: sub files_in_path {
                   11077:     my ($user, $path) = @_;
                   11078:     my $filename = $user."savedfiles";
                   11079:     my %return_files;
1.1359    raeburn  11080:     open (IN,'<',LONCAPA::tempdir().$filename);
1.573     banghart 11081:     while (my $line_in = <IN>) {
1.574     banghart 11082:         chomp ($line_in);
                   11083:         my @paths_and_file = split (m!/!, $line_in);
                   11084:         my $file_part = pop (@paths_and_file);
                   11085:         my $path_part = join ('/', @paths_and_file);
1.573     banghart 11086:         $path_part.='/';
                   11087:         my $path_and_file = $path_part.$file_part;
                   11088:         if ($path_part eq $path) {
                   11089:             $return_files{$file_part}= 'selected';
                   11090:         }
                   11091:     }
1.574     banghart 11092:     close (IN);
                   11093:     return (\%return_files);
1.572     banghart 11094: }
                   11095: 
                   11096: # called in portfolio select mode, to show files selected NOT in current directory
                   11097: sub files_not_in_path {
                   11098:     my ($user, $path) = @_;
                   11099:     my $filename = $user."savedfiles";
                   11100:     my @return_files;
                   11101:     my $path_part;
1.1359    raeburn  11102:     open(IN, '<',LONCAPA::tempdir().$filename);
1.800     albertel 11103:     while (my $line = <IN>) {
1.572     banghart 11104:         #ok, I know it's clunky, but I want it to work
1.800     albertel 11105:         my @paths_and_file = split(m|/|, $line);
                   11106:         my $file_part = pop(@paths_and_file);
                   11107:         chomp($file_part);
                   11108:         my $path_part = join('/', @paths_and_file);
1.572     banghart 11109:         $path_part .= '/';
                   11110:         my $path_and_file = $path_part.$file_part;
                   11111:         if ($path_part ne $path) {
1.800     albertel 11112:             push(@return_files, ($path_and_file));
1.572     banghart 11113:         }
                   11114:     }
1.800     albertel 11115:     close(OUT);
1.574     banghart 11116:     return (@return_files);
1.572     banghart 11117: }
                   11118: 
1.1273    raeburn  11119: #------------------------------Submitted/Handedback Portfolio Files Versioning
                   11120:  
                   11121: sub portfiles_versioning {
                   11122:     my ($symb,$domain,$stu_name,$portfiles,$versioned_portfiles) = @_;
                   11123:     my $portfolio_root = '/userfiles/portfolio';
                   11124:     return unless ((ref($portfiles) eq 'ARRAY') && (ref($versioned_portfiles) eq 'ARRAY'));
                   11125:     foreach my $file (@{$portfiles}) {
                   11126:         &unmark_as_readonly($domain,$stu_name,[$symb,$env{'request.course.id'}],$file);
                   11127:         my ($directory,$answer_file) =($file =~ /^(.*?)([^\/]*)$/);
                   11128:         my ($answer_name,$answer_ver,$answer_ext) = &file_name_version_ext($answer_file);
                   11129:         my $getpropath = 1;
                   11130:         my ($dir_list,$listerror) = &dirlist($portfolio_root.$directory,$domain,
                   11131:                                              $stu_name,$getpropath);
                   11132:         my $version = &get_next_version($answer_name,$answer_ext,$dir_list);
                   11133:         my $new_answer = 
                   11134:             &version_selected_portfile($domain,$stu_name,$directory,$answer_file,$version);
                   11135:         if ($new_answer ne 'problem getting file') {
                   11136:             push(@{$versioned_portfiles}, $directory.$new_answer);
                   11137:             &mark_as_readonly($domain,$stu_name,[$directory.$new_answer],
                   11138:                               [$symb,$env{'request.course.id'},'graded']);
                   11139:         }
                   11140:     }
                   11141: }
                   11142: 
                   11143: sub get_next_version {
                   11144:     my ($answer_name, $answer_ext, $dir_list) = @_;
                   11145:     my $version;
                   11146:     if (ref($dir_list) eq 'ARRAY') {
                   11147:         foreach my $row (@{$dir_list}) {
                   11148:             my ($file) = split(/\&/,$row,2);
                   11149:             my ($file_name,$file_version,$file_ext) =
                   11150:                 &file_name_version_ext($file);
                   11151:             if (($file_name eq $answer_name) &&
                   11152:                 ($file_ext eq $answer_ext)) {
                   11153:                      # gets here if filename and extension match,
                   11154:                      # regardless of version
                   11155:                 if ($file_version ne '') {
                   11156:                     # a versioned file is found  so save it for later
                   11157:                     if ($file_version > $version) {
                   11158:                         $version = $file_version;
                   11159:                     }
                   11160:                 }
                   11161:             }
                   11162:         }
                   11163:     }
                   11164:     $version ++;
                   11165:     return($version);
                   11166: }
                   11167: 
                   11168: sub version_selected_portfile {
                   11169:     my ($domain,$stu_name,$directory,$file_name,$version) = @_;
                   11170:     my ($answer_name,$answer_ver,$answer_ext) =
                   11171:         &file_name_version_ext($file_name);
                   11172:     my $new_answer;
                   11173:     $env{'form.copy'} =
                   11174:         &getfile("/uploaded/$domain/$stu_name/portfolio$directory$file_name");
                   11175:     if($env{'form.copy'} eq '-1') {
                   11176:         $new_answer = 'problem getting file';
                   11177:     } else {
                   11178:         $new_answer = $answer_name.'.'.$version.'.'.$answer_ext;
                   11179:         my $copy_result = 
                   11180:             &finishuserfileupload($stu_name,$domain,'copy',
                   11181:                                   '/portfolio'.$directory.$new_answer);
                   11182:     }
                   11183:     undef($env{'form.copy'});
                   11184:     return ($new_answer);
                   11185: }
                   11186: 
                   11187: sub file_name_version_ext {
                   11188:     my ($file)=@_;
                   11189:     my @file_parts = split(/\./, $file);
                   11190:     my ($name,$version,$ext);
                   11191:     if (@file_parts > 1) {
                   11192:         $ext=pop(@file_parts);
                   11193:         if (@file_parts > 1 && $file_parts[-1] =~ /^\d+$/) {
                   11194:             $version=pop(@file_parts);
                   11195:         }
                   11196:         $name=join('.',@file_parts);
                   11197:     } else {
                   11198:         $name=join('.',@file_parts);
                   11199:     }
                   11200:     return($name,$version,$ext);
                   11201: }
                   11202: 
1.745     raeburn  11203: #----------------------------------------------Get portfolio file permissions
1.629     banghart 11204: 
1.745     raeburn  11205: sub get_portfile_permissions {
                   11206:     my ($domain,$user) = @_;
1.613     albertel 11207:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 11208:     my ($tmp)=keys(%current_permissions);
                   11209:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  11210:     return \%current_permissions;
                   11211: }
                   11212: 
                   11213: #---------------------------------------------Get portfolio file access controls
                   11214: 
1.749     raeburn  11215: sub get_access_controls {
1.745     raeburn  11216:     my ($current_permissions,$group,$file) = @_;
1.769     albertel 11217:     my %access;
                   11218:     my $real_file = $file;
                   11219:     $file =~ s/\.meta$//;
1.745     raeburn  11220:     if (defined($file)) {
1.749     raeburn  11221:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
                   11222:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769     albertel 11223:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749     raeburn  11224:             }
                   11225:         }
1.745     raeburn  11226:     } else {
1.749     raeburn  11227:         foreach my $key (keys(%{$current_permissions})) {
                   11228:             if ($key =~ /\0accesscontrol$/) {
                   11229:                 if (defined($group)) {
                   11230:                     if ($key !~ m-^\Q$group\E/-) {
                   11231:                         next;
                   11232:                     }
                   11233:                 }
                   11234:                 my ($fullpath) = split(/\0/,$key);
                   11235:                 if (ref($$current_permissions{$key}) eq 'HASH') {
                   11236:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
                   11237:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
                   11238:                     }
                   11239:                 }
                   11240:             }
                   11241:         }
                   11242:     }
                   11243:     return %access;
                   11244: }
                   11245: 
                   11246: sub modify_access_controls {
                   11247:     my ($file_name,$changes,$domain,$user)=@_;
                   11248:     my ($outcome,$deloutcome);
                   11249:     my %store_permissions;
                   11250:     my %new_values;
                   11251:     my %new_control;
                   11252:     my %translation;
                   11253:     my @deletions = ();
                   11254:     my $now = time;
                   11255:     if (exists($$changes{'activate'})) {
                   11256:         if (ref($$changes{'activate'}) eq 'HASH') {
                   11257:             my @newitems = sort(keys(%{$$changes{'activate'}}));
                   11258:             my $numnew = scalar(@newitems);
                   11259:             for (my $i=0; $i<$numnew; $i++) {
                   11260:                 my $newkey = $newitems[$i];
                   11261:                 my $newid = &Apache::loncommon::get_cgi_id();
1.797     raeburn  11262:                 if ($newkey =~ /^\d+:/) { 
                   11263:                     $newkey =~ s/^(\d+)/$newid/;
                   11264:                     $translation{$1} = $newid;
                   11265:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
                   11266:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
                   11267:                     $translation{$1} = $newid;
                   11268:                 }
1.749     raeburn  11269:                 $new_values{$file_name."\0".$newkey} = 
                   11270:                                           $$changes{'activate'}{$newitems[$i]};
                   11271:                 $new_control{$newkey} = $now;
                   11272:             }
                   11273:         }
                   11274:     }
                   11275:     my %todelete;
                   11276:     my %changed_items;
                   11277:     foreach my $action ('delete','update') {
                   11278:         if (exists($$changes{$action})) {
                   11279:             if (ref($$changes{$action}) eq 'HASH') {
                   11280:                 foreach my $key (keys(%{$$changes{$action}})) {
                   11281:                     my ($itemnum) = ($key =~ /^([^:]+):/);
                   11282:                     if ($action eq 'delete') { 
                   11283:                         $todelete{$itemnum} = 1;
                   11284:                     } else {
                   11285:                         $changed_items{$itemnum} = $key;
                   11286:                     }
                   11287:                 }
1.745     raeburn  11288:             }
                   11289:         }
1.749     raeburn  11290:     }
                   11291:     # get lock on access controls for file.
                   11292:     my $lockhash = {
                   11293:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
                   11294:                                                        ':'.$env{'user.domain'},
                   11295:                    }; 
                   11296:     my $tries = 0;
                   11297:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   11298:    
1.1288    damieng  11299:     while (($gotlock ne 'ok') && $tries < 10) {
1.749     raeburn  11300:         $tries ++;
1.1289    damieng  11301:         sleep(0.1);
1.749     raeburn  11302:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   11303:     }
                   11304:     if ($gotlock eq 'ok') {
                   11305:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
                   11306:         my ($tmp)=keys(%curr_permissions);
                   11307:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
                   11308:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
                   11309:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
                   11310:             if (ref($curr_controls) eq 'HASH') {
                   11311:                 foreach my $control_item (keys(%{$curr_controls})) {
                   11312:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
                   11313:                     if (defined($todelete{$itemnum})) {
                   11314:                         push(@deletions,$file_name."\0".$control_item);
                   11315:                     } else {
                   11316:                         if (defined($changed_items{$itemnum})) {
                   11317:                             $new_control{$changed_items{$itemnum}} = $now;
                   11318:                             push(@deletions,$file_name."\0".$control_item);
                   11319:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
                   11320:                         } else {
                   11321:                             $new_control{$control_item} = $$curr_controls{$control_item};
                   11322:                         }
                   11323:                     }
1.745     raeburn  11324:                 }
                   11325:             }
                   11326:         }
1.970     raeburn  11327:         my ($group);
                   11328:         if (&is_course($domain,$user)) {
                   11329:             ($group,my $file) = split(/\//,$file_name,2);
                   11330:         }
1.749     raeburn  11331:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
                   11332:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
                   11333:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
                   11334:         #  remove lock
                   11335:         my @del_lock = ($file_name."\0".'locked_access_records');
                   11336:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818     raeburn  11337:         my $sqlresult =
1.970     raeburn  11338:             &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
1.818     raeburn  11339:                                     $group);
1.749     raeburn  11340:     } else {
                   11341:         $outcome = "error: could not obtain lockfile\n";  
1.745     raeburn  11342:     }
1.749     raeburn  11343:     return ($outcome,$deloutcome,\%new_values,\%translation);
1.745     raeburn  11344: }
                   11345: 
1.827     raeburn  11346: sub make_public_indefinitely {
1.1271    raeburn  11347:     my (@requrl) = @_;
                   11348:     return &automated_portfile_access('public',\@requrl);
                   11349: }
                   11350: 
                   11351: sub automated_portfile_access {
                   11352:     my ($accesstype,$addsref,$delsref,$info) = @_;
1.1273    raeburn  11353:     unless (($accesstype eq 'public') || ($accesstype eq 'ip')) {
                   11354:         return 'invalid';
                   11355:     }
1.1271    raeburn  11356:     my %urls;
                   11357:     if (ref($addsref) eq 'ARRAY') {
                   11358:         foreach my $requrl (@{$addsref}) {
                   11359:             if (&is_portfolio_url($requrl)) {
                   11360:                 unless (exists($urls{$requrl})) {
                   11361:                     $urls{$requrl} = 'add';
                   11362:                 }
                   11363:             }
                   11364:         }
                   11365:     }
                   11366:     if (ref($delsref) eq 'ARRAY') {
                   11367:         foreach my $requrl (@{$delsref}) { 
                   11368:             if (&is_portfolio_url($requrl)) {
                   11369:                 unless (exists($urls{$requrl})) {
                   11370:                     $urls{$requrl} = 'delete'; 
                   11371:                 }
                   11372:             }
                   11373:         }
                   11374:     }
                   11375:     unless (keys(%urls)) {
                   11376:         return 'invalid';
                   11377:     }
                   11378:     my $ip;
                   11379:     if ($accesstype eq 'ip') {
                   11380:         if (ref($info) eq 'HASH') {
                   11381:             if ($info->{'ip'} ne '') {
                   11382:                 $ip = $info->{'ip'};
                   11383:             }
                   11384:         }
                   11385:         if ($ip eq '') {
                   11386:             return 'invalid';
                   11387:         }
                   11388:     }
                   11389:     my $errors;
1.827     raeburn  11390:     my $now = time;
1.1271    raeburn  11391:     my %current_perms;
                   11392:     foreach my $requrl (sort(keys(%urls))) {
                   11393:         my $action;
                   11394:         if ($urls{$requrl} eq 'add') {
                   11395:             $action = 'activate';
                   11396:         } else {
                   11397:             $action = 'none';
                   11398:         }
                   11399:         my $aclnum = 0;
1.827     raeburn  11400:         my (undef,$udom,$unum,$file_name,$group) =
                   11401:             &parse_portfolio_url($requrl);
1.1271    raeburn  11402:         unless (exists($current_perms{$unum.':'.$udom})) {
                   11403:             $current_perms{$unum.':'.$udom} = &get_portfile_permissions($udom,$unum);
                   11404:         }
                   11405:         my %access_controls = &get_access_controls($current_perms{$unum.':'.$udom},
1.827     raeburn  11406:                                                    $group,$file_name);
                   11407:         foreach my $key (keys(%{$access_controls{$file_name}})) {
                   11408:             my ($num,$scope,$end,$start) = 
                   11409:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
1.1271    raeburn  11410:             if ($scope eq $accesstype) {
                   11411:                 if (($start <= $now) && ($end == 0)) {
                   11412:                     if ($accesstype eq 'ip') {
                   11413:                         if (ref($access_controls{$file_name}{$key}) eq 'HASH') {
                   11414:                             if (ref($access_controls{$file_name}{$key}{'ip'}) eq 'ARRAY') {
                   11415:                                 if (grep(/^\Q$ip\E$/,@{$access_controls{$file_name}{$key}{'ip'}})) {
                   11416:                                     if ($urls{$requrl} eq 'add') {
                   11417:                                         $action = 'none';
                   11418:                                         last;
                   11419:                                     } else {
                   11420:                                         $action = 'delete';
                   11421:                                         $aclnum = $num;
                   11422:                                         last;
                   11423:                                     }
                   11424:                                 }
                   11425:                             }
                   11426:                         }
                   11427:                     } elsif ($accesstype eq 'public') {
                   11428:                         if ($urls{$requrl} eq 'add') {
                   11429:                             $action = 'none';
                   11430:                             last;
                   11431:                         } else {
                   11432:                             $action = 'delete';
                   11433:                             $aclnum = $num;
                   11434:                             last;
                   11435:                         }
                   11436:                     }
                   11437:                 } elsif ($accesstype eq 'public') {
1.827     raeburn  11438:                     $action = 'update';
                   11439:                     $aclnum = $num;
1.1271    raeburn  11440:                     last;
1.827     raeburn  11441:                 }
                   11442:             }
                   11443:         }
                   11444:         if ($action eq 'none') {
1.1271    raeburn  11445:             next;
1.827     raeburn  11446:         } else {
                   11447:             my %changes;
                   11448:             my $newend = 0;
                   11449:             my $newstart = $now;
1.1271    raeburn  11450:             my $newkey = $aclnum.':'.$accesstype.'_'.$newend.'_'.$newstart;
1.827     raeburn  11451:             $changes{$action}{$newkey} = {
1.1271    raeburn  11452:                 type => $accesstype,
1.827     raeburn  11453:                 time => {
                   11454:                     start => $newstart,
                   11455:                     end   => $newend,
                   11456:                 },
                   11457:             };
1.1271    raeburn  11458:             if ($accesstype eq 'ip') {
                   11459:                 $changes{$action}{$newkey}{'ip'} = [$ip];
                   11460:             }
1.827     raeburn  11461:             my ($outcome,$deloutcome,$new_values,$translation) =
                   11462:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
1.1271    raeburn  11463:             unless ($outcome eq 'ok') {
                   11464:                 $errors .= $outcome.' ';
                   11465:             }
1.827     raeburn  11466:         }
1.1271    raeburn  11467:     }
                   11468:     if ($errors) {
                   11469:         $errors =~ s/\s$//;
                   11470:         return $errors;
1.827     raeburn  11471:     } else {
1.1271    raeburn  11472:         return 'ok';
1.827     raeburn  11473:     }
                   11474: }
                   11475: 
1.745     raeburn  11476: #------------------------------------------------------Get Marked as Read Only
                   11477: 
                   11478: sub get_marked_as_readonly {
                   11479:     my ($domain,$user,$what,$group) = @_;
                   11480:     my $current_permissions = &get_portfile_permissions($domain,$user);
1.563     banghart 11481:     my @readonly_files;
1.629     banghart 11482:     my $cmp1=$what;
                   11483:     if (ref($what)) { $cmp1=join('',@{$what}) };
1.745     raeburn  11484:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   11485:         if (defined($group)) {
                   11486:             if ($file_name !~ m-^\Q$group\E/-) {
                   11487:                 next;
                   11488:             }
                   11489:         }
1.561     banghart 11490:         if (ref($value) eq "ARRAY"){
                   11491:             foreach my $stored_what (@{$value}) {
1.629     banghart 11492:                 my $cmp2=$stored_what;
1.759     albertel 11493:                 if (ref($stored_what) eq 'ARRAY') {
1.746     raeburn  11494:                     $cmp2=join('',@{$stored_what});
1.745     raeburn  11495:                 }
1.629     banghart 11496:                 if ($cmp1 eq $cmp2) {
1.561     banghart 11497:                     push(@readonly_files, $file_name);
1.745     raeburn  11498:                     last;
1.563     banghart 11499:                 } elsif (!defined($what)) {
                   11500:                     push(@readonly_files, $file_name);
1.745     raeburn  11501:                     last;
1.561     banghart 11502:                 }
                   11503:             }
1.745     raeburn  11504:         }
1.561     banghart 11505:     }
                   11506:     return @readonly_files;
                   11507: }
1.577     banghart 11508: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561     banghart 11509: 
1.577     banghart 11510: sub get_marked_as_readonly_hash {
1.745     raeburn  11511:     my ($current_permissions,$group,$what) = @_;
1.577     banghart 11512:     my %readonly_files;
1.745     raeburn  11513:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   11514:         if (defined($group)) {
                   11515:             if ($file_name !~ m-^\Q$group\E/-) {
                   11516:                 next;
                   11517:             }
                   11518:         }
1.577     banghart 11519:         if (ref($value) eq "ARRAY"){
                   11520:             foreach my $stored_what (@{$value}) {
1.745     raeburn  11521:                 if (ref($stored_what) eq 'ARRAY') {
1.750     banghart 11522:                     foreach my $lock_descriptor(@{$stored_what}) {
                   11523:                         if ($lock_descriptor eq 'graded') {
                   11524:                             $readonly_files{$file_name} = 'graded';
                   11525:                         } elsif ($lock_descriptor eq 'handback') {
                   11526:                             $readonly_files{$file_name} = 'handback';
                   11527:                         } else {
                   11528:                             if (!exists($readonly_files{$file_name})) {
                   11529:                                 $readonly_files{$file_name} = 'locked';
                   11530:                             }
                   11531:                         }
1.745     raeburn  11532:                     }
1.750     banghart 11533:                 } 
1.577     banghart 11534:             }
                   11535:         } 
                   11536:     }
                   11537:     return %readonly_files;
                   11538: }
1.559     banghart 11539: # ------------------------------------------------------------ Unmark as Read Only
                   11540: 
                   11541: sub unmark_as_readonly {
1.629     banghart 11542:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
                   11543:     # for portfolio submissions, $what contains [$symb,$crsid] 
1.745     raeburn  11544:     my ($domain,$user,$what,$file_name,$group) = @_;
1.759     albertel 11545:     $file_name = &declutter_portfile($file_name);
1.634     albertel 11546:     my $symb_crs = $what;
                   11547:     if (ref($what)) { $symb_crs=join('',@$what); }
1.745     raeburn  11548:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615     albertel 11549:     my ($tmp)=keys(%current_permissions);
                   11550:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  11551:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650     albertel 11552:     foreach my $file (@readonly_files) {
1.759     albertel 11553: 	my $clean_file = &declutter_portfile($file);
                   11554: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650     albertel 11555: 	my $current_locks = $current_permissions{$file};
1.563     banghart 11556:         my @new_locks;
                   11557:         my @del_keys;
                   11558:         if (ref($current_locks) eq "ARRAY"){
                   11559:             foreach my $locker (@{$current_locks}) {
1.632     albertel 11560:                 my $compare=$locker;
1.749     raeburn  11561:                 if (ref($locker) eq 'ARRAY') {
1.745     raeburn  11562:                     $compare=join('',@{$locker});
1.746     raeburn  11563:                     if ($compare ne $symb_crs) {
                   11564:                         push(@new_locks, $locker);
                   11565:                     }
1.563     banghart 11566:                 }
                   11567:             }
1.650     albertel 11568:             if (scalar(@new_locks) > 0) {
1.563     banghart 11569:                 $current_permissions{$file} = \@new_locks;
                   11570:             } else {
                   11571:                 push(@del_keys, $file);
1.613     albertel 11572:                 &del('file_permissions',\@del_keys, $domain, $user);
1.650     albertel 11573:                 delete($current_permissions{$file});
1.563     banghart 11574:             }
                   11575:         }
1.561     banghart 11576:     }
1.613     albertel 11577:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 11578:     return;
                   11579: }
1.512     banghart 11580: 
1.17      www      11581: # ------------------------------------------------------------ Directory lister
                   11582: 
                   11583: sub dirlist {
1.955     raeburn  11584:     my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
1.18      www      11585:     $uri=~s/^\///;
                   11586:     $uri=~s/\/$//;
1.253     stredwic 11587:     my ($udom, $uname);
1.955     raeburn  11588:     if ($getuserdir) {
1.253     stredwic 11589:         $udom = $userdomain;
                   11590:         $uname = $username;
1.955     raeburn  11591:     } else {
                   11592:         (undef,$udom,$uname)=split(/\//,$uri);
                   11593:         if(defined($userdomain)) {
                   11594:             $udom = $userdomain;
                   11595:         }
                   11596:         if(defined($username)) {
                   11597:             $uname = $username;
                   11598:         }
1.253     stredwic 11599:     }
1.955     raeburn  11600:     my ($dirRoot,$listing,@listing_results);
1.253     stredwic 11601: 
1.955     raeburn  11602:     $dirRoot = $perlvar{'lonDocRoot'};
                   11603:     if (defined($getpropath)) {
                   11604:         $dirRoot = &propath($udom,$uname);
1.253     stredwic 11605:         $dirRoot =~ s/\/$//;
1.955     raeburn  11606:     } elsif (defined($getuserdir)) {
                   11607:         my $subdir=$uname.'__';
                   11608:         $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
                   11609:         $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
                   11610:                    ."/$udom/$subdir/$uname";
                   11611:     } elsif (defined($alternateRoot)) {
                   11612:         $dirRoot = $alternateRoot;
1.751     banghart 11613:     }
1.253     stredwic 11614: 
                   11615:     if($udom) {
                   11616:         if($uname) {
1.1135    raeburn  11617:             my $uhome = &homeserver($uname,$udom);
1.1136    raeburn  11618:             if ($uhome eq 'no_host') {
                   11619:                 return ([],'no_host');
                   11620:             }
1.955     raeburn  11621:             $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
1.956     raeburn  11622:                               .$getuserdir.':'.&escape($dirRoot)
1.1135    raeburn  11623:                               .':'.&escape($uname).':'.&escape($udom),$uhome);
1.955     raeburn  11624:             if ($listing eq 'unknown_cmd') {
1.1135    raeburn  11625:                 $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
1.955     raeburn  11626:             } else {
                   11627:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
                   11628:             }
1.605     matthew  11629:             if ($listing eq 'unknown_cmd') {
1.1135    raeburn  11630:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
1.605     matthew  11631:                 @listing_results = split(/:/,$listing);
                   11632:             } else {
                   11633:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
                   11634:             }
1.1135    raeburn  11635:             if (($listing eq 'no_such_host') || ($listing eq 'con_lost') || 
1.1136    raeburn  11636:                 ($listing eq 'rejected') || ($listing eq 'refused') ||
                   11637:                 ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
                   11638:                 return ([],$listing);
                   11639:             } else {
                   11640:                 return (\@listing_results);
1.1135    raeburn  11641:             }
1.955     raeburn  11642:         } elsif(!$alternateRoot) {
1.1136    raeburn  11643:             my (%allusers,%listerror);
1.841     albertel 11644: 	    my %servers = &get_servers($udom,'library');
1.955     raeburn  11645:  	    foreach my $tryserver (keys(%servers)) {
                   11646:                 $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
                   11647:                                   &escape($udom),$tryserver);
                   11648:                 if ($listing eq 'unknown_cmd') {
                   11649: 		    $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
                   11650: 				      $udom, $tryserver);
                   11651:                 } else {
                   11652:                     @listing_results = map { &unescape($_); } split(/:/,$listing);
                   11653:                 }
1.841     albertel 11654: 		if ($listing eq 'unknown_cmd') {
                   11655: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
                   11656: 				      $udom, $tryserver);
                   11657: 		    @listing_results = split(/:/,$listing);
                   11658: 		} else {
                   11659: 		    @listing_results =
                   11660: 			map { &unescape($_); } split(/:/,$listing);
                   11661: 		}
1.1136    raeburn  11662:                 if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
                   11663:                     ($listing eq 'rejected') || ($listing eq 'refused') ||
                   11664:                     ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
                   11665:                     $listerror{$tryserver} = $listing;
                   11666:                 } else {
1.841     albertel 11667: 		    foreach my $line (@listing_results) {
                   11668: 			my ($entry) = split(/&/,$line,2);
                   11669: 			$allusers{$entry} = 1;
                   11670: 		    }
                   11671: 		}
1.253     stredwic 11672:             }
1.1136    raeburn  11673:             my @alluserslist=();
1.800     albertel 11674:             foreach my $user (sort(keys(%allusers))) {
1.1136    raeburn  11675:                 push(@alluserslist,$user.'&user');
1.253     stredwic 11676:             }
1.1318    droeschl 11677: 
                   11678:             if (!%listerror) {
                   11679:                 # no errors
                   11680:                 return (\@alluserslist);
                   11681:             } elsif (scalar(keys(%servers)) == 1) {
                   11682:                 # one library server, one error 
                   11683:                 my ($key) = keys(%listerror);
                   11684:                 return (\@alluserslist, $listerror{$key});
                   11685:             } elsif ( grep { $_ eq 'con_lost' } values(%listerror) ) {
                   11686:                 # con_lost indicates that we might miss data from at least one
                   11687:                 # library server
                   11688:                 return (\@alluserslist, 'con_lost');
                   11689:             } else {
                   11690:                 # multiple library servers and no con_lost -> data should be
                   11691:                 # complete. 
                   11692:                 return (\@alluserslist);
                   11693:             }
                   11694: 
1.253     stredwic 11695:         } else {
1.1136    raeburn  11696:             return ([],'missing username');
1.253     stredwic 11697:         }
1.955     raeburn  11698:     } elsif(!defined($getpropath)) {
1.1136    raeburn  11699:         my $path = $perlvar{'lonDocRoot'}.'/res/'; 
                   11700:         my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
                   11701:         return (\@all_domains);
1.955     raeburn  11702:     } else {
1.1136    raeburn  11703:         return ([],'missing domain');
1.275     stredwic 11704:     }
                   11705: }
                   11706: 
                   11707: # --------------------------------------------- GetFileTimestamp
                   11708: # This function utilizes dirlist and returns the date stamp for
                   11709: # when it was last modified.  It will also return an error of -1
                   11710: # if an error occurs
                   11711: 
                   11712: sub GetFileTimestamp {
1.955     raeburn  11713:     my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
1.807     albertel 11714:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
                   11715:     $studentName   = &LONCAPA::clean_username($studentName);
1.1136    raeburn  11716:     my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
                   11717:                                     undef,$getuserdir);
                   11718:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
                   11719:         return -1;
                   11720:     }
                   11721:     if (ref($fileref) eq 'ARRAY') {
                   11722:         my @stats = split('&',$fileref->[0]);
1.375     matthew  11723:         # @stats contains first the filename, then the stat output
                   11724:         return $stats[10]; # so this is 10 instead of 9.
1.275     stredwic 11725:     } else {
                   11726:         return -1;
1.253     stredwic 11727:     }
1.26      www      11728: }
                   11729: 
1.712     albertel 11730: sub stat_file {
                   11731:     my ($uri) = @_;
1.787     albertel 11732:     $uri = &clutter_with_no_wrapper($uri);
1.722     albertel 11733: 
1.955     raeburn  11734:     my ($udom,$uname,$file);
1.712     albertel 11735:     if ($uri =~ m-^/(uploaded|editupload)/-) {
                   11736: 	($udom,$uname,$file) =
1.811     albertel 11737: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712     albertel 11738: 	$file = 'userfiles/'.$file;
                   11739:     }
                   11740:     if ($uri =~ m-^/res/-) {
                   11741: 	($udom,$uname) = 
1.807     albertel 11742: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712     albertel 11743: 	$file = $uri;
                   11744:     }
                   11745: 
                   11746:     if (!$udom || !$uname || !$file) {
                   11747: 	# unable to handle the uri
                   11748: 	return ();
                   11749:     }
1.956     raeburn  11750:     my $getpropath;
                   11751:     if ($file =~ /^userfiles\//) {
                   11752:         $getpropath = 1;
                   11753:     }
1.1136    raeburn  11754:     my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
                   11755:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
                   11756:         return ();
                   11757:     } else {
                   11758:         if (ref($listref) eq 'ARRAY') {
                   11759:             my @stats = split('&',$listref->[0]);
                   11760: 	    shift(@stats); #filename is first
                   11761: 	    return @stats;
                   11762:         }
1.712     albertel 11763:     }
                   11764:     return ();
                   11765: }
                   11766: 
1.1313    raeburn  11767: # --------------------------------------------------------- recursedirs
                   11768: # Recursive function to traverse either a specific user's Authoring Space
                   11769: # or corresponding Published Resource Space, and populate the hash ref:
                   11770: # $dirhashref with URLs of all directories, and if $filehashref hash
                   11771: # ref arg is provided, the URLs of any files, excluding versioned, .meta,
                   11772: # or .rights files in resource space, and .meta, .save, .log, and .bak
                   11773: # files in Authoring Space.
                   11774: #
                   11775: # Inputs:
                   11776: #
                   11777: # $is_home - true if current server is home server for user's space
                   11778: # $context - either: priv, or res respectively for Authoring or Resource Space.
                   11779: # $docroot - Document root (i.e., /home/httpd/html
                   11780: # $toppath - Top level directory (i.e., /res/$dom/$uname or /priv/$dom/$uname
                   11781: # $relpath - Current path (relative to top level).
                   11782: # $dirhashref - reference to hash to populate with URLs of directories (Required)
                   11783: # $filehashref - reference to hash to populate with URLs of files (Optional)
                   11784: #
                   11785: # Returns: nothing
                   11786: #
                   11787: # Side Effects: populates $dirhashref, and $filehashref (if provided).
                   11788: #
                   11789: # Currently used by interface/londocs.pm to create linked select boxes for
                   11790: # directory and filename to import a Course "Author" resource into a course, and
                   11791: # also to create linked select boxes for Authoring Space and Directory to choose
                   11792: # save location for creation of a new "standard" problem from the Course Editor.
                   11793: #
                   11794: 
                   11795: sub recursedirs {
                   11796:     my ($is_home,$context,$docroot,$toppath,$relpath,$dirhashref,$filehashref) = @_;
                   11797:     return unless (ref($dirhashref) eq 'HASH');
                   11798:     my $currpath = $docroot.$toppath;
                   11799:     if ($relpath) {
                   11800:         $currpath .= "/$relpath";
                   11801:     }
                   11802:     my $savefile;
                   11803:     if (ref($filehashref)) {
                   11804:         $savefile = 1;
                   11805:     }
                   11806:     if ($is_home) {
                   11807:         if (opendir(my $dirh,$currpath)) {
                   11808:             foreach my $item (sort { lc($a) cmp lc($b) } grep(!/^\.+$/,readdir($dirh))) {
                   11809:                 next if ($item eq '');
                   11810:                 if (-d "$currpath/$item") {
                   11811:                     my $newpath;
                   11812:                     if ($relpath) {
                   11813:                         $newpath = "$relpath/$item";
                   11814:                     } else {
                   11815:                         $newpath = $item;
                   11816:                     }
                   11817:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
                   11818:                     &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
                   11819:                 } elsif ($savefile) {
                   11820:                     if ($context eq 'priv') {
                   11821:                         unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
                   11822:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
                   11823:                         }
                   11824:                     } else {
                   11825:                         unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/) || ($item =~ /\.rights$/)) {
                   11826:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
                   11827:                         }
                   11828:                     }
                   11829:                 }
                   11830:             }
                   11831:             closedir($dirh);
                   11832:         }
                   11833:     } else {
                   11834:         my ($dirlistref,$listerror) =
                   11835:             &dirlist($toppath.$relpath);
                   11836:         my @dir_lines;
                   11837:         my $dirptr=16384;
                   11838:         if (ref($dirlistref) eq 'ARRAY') {
                   11839:             foreach my $dir_line (sort
                   11840:                               {
                   11841:                                   my ($afile)=split('&',$a,2);
                   11842:                                   my ($bfile)=split('&',$b,2);
                   11843:                                   return (lc($afile) cmp lc($bfile));
                   11844:                               } (@{$dirlistref})) {
                   11845:                 my ($item,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef) =
                   11846:                     split(/\&/,$dir_line,16);
                   11847:                 $item =~ s/\s+$//;
                   11848:                 next if (($item =~ /^\.\.?$/) || ($obs));
                   11849:                 if ($dirptr&$testdir) {
                   11850:                     my $newpath;
                   11851:                     if ($relpath) {
                   11852:                         $newpath = "$relpath/$item";
                   11853:                     } else {
                   11854:                         $relpath = '/';
                   11855:                         $newpath = $item;
                   11856:                     }
                   11857:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
                   11858:                     &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
                   11859:                 } elsif ($savefile) {
                   11860:                     if ($context eq 'priv') {
                   11861:                         unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
                   11862:                             $filehashref->{$relpath}{$item} = 1;
                   11863:                         }
                   11864:                     } else {
                   11865:                         unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/)) {
                   11866:                             $filehashref->{$relpath}{$item} = 1;
                   11867:                         }
                   11868:                     }
                   11869:                 }
                   11870:             }
                   11871:         }
                   11872:     }
                   11873:     return;
                   11874: }
                   11875: 
1.26      www      11876: # -------------------------------------------------------- Value of a Condition
                   11877: 
1.713     albertel 11878: # gets the value of a specific preevaluated condition
                   11879: #    stored in the string  $env{user.state.<cid>}
                   11880: # or looks up a condition reference in the bighash and if if hasn't
                   11881: # already been evaluated recurses into docondval to get the value of
                   11882: # the condition, then memoizing it to 
                   11883: #   $env{user.state.<cid>.<condition>}
1.40      www      11884: sub directcondval {
                   11885:     my $number=shift;
1.620     albertel 11886:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555     albertel 11887: 	&Apache::lonuserstate::evalstate();
                   11888:     }
1.713     albertel 11889:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
                   11890: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
                   11891:     } elsif ($number =~ /^_/) {
                   11892: 	my $sub_condition;
                   11893: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
                   11894: 		&GDBM_READER(),0640)) {
                   11895: 	    $sub_condition=$bighash{'conditions'.$number};
                   11896: 	    untie(%bighash);
                   11897: 	}
                   11898: 	my $value = &docondval($sub_condition);
1.949     raeburn  11899: 	&appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
1.713     albertel 11900: 	return $value;
                   11901:     }
1.620     albertel 11902:     if ($env{'user.state.'.$env{'request.course.id'}}) {
                   11903:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40      www      11904:     } else {
                   11905:        return 2;
                   11906:     }
                   11907: }
                   11908: 
1.713     albertel 11909: # get the collection of conditions for this resource
1.26      www      11910: sub condval {
                   11911:     my $condidx=shift;
1.54      www      11912:     my $allpathcond='';
1.713     albertel 11913:     foreach my $cond (split(/\|/,$condidx)) {
                   11914: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
                   11915: 	    $allpathcond.=
                   11916: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
                   11917: 	}
1.191     harris41 11918:     }
1.54      www      11919:     $allpathcond=~s/\|$//;
1.713     albertel 11920:     return &docondval($allpathcond);
                   11921: }
                   11922: 
                   11923: #evaluates an expression of conditions
                   11924: sub docondval {
                   11925:     my ($allpathcond) = @_;
                   11926:     my $result=0;
                   11927:     if ($env{'request.course.id'}
                   11928: 	&& defined($allpathcond)) {
                   11929: 	my $operand='|';
                   11930: 	my @stack;
                   11931: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
                   11932: 	    if ($chunk eq '(') {
                   11933: 		push @stack,($operand,$result);
                   11934: 	    } elsif ($chunk eq ')') {
                   11935: 		my $before=pop @stack;
                   11936: 		if (pop @stack eq '&') {
                   11937: 		    $result=$result>$before?$before:$result;
                   11938: 		} else {
                   11939: 		    $result=$result>$before?$result:$before;
                   11940: 		}
                   11941: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
                   11942: 		$operand=$chunk;
                   11943: 	    } else {
                   11944: 		my $new=directcondval($chunk);
                   11945: 		if ($operand eq '&') {
                   11946: 		    $result=$result>$new?$new:$result;
                   11947: 		} else {
                   11948: 		    $result=$result>$new?$result:$new;
                   11949: 		}
                   11950: 	    }
                   11951: 	}
1.26      www      11952:     }
                   11953:     return $result;
1.421     albertel 11954: }
                   11955: 
                   11956: # ---------------------------------------------------- Devalidate courseresdata
                   11957: 
                   11958: sub devalidatecourseresdata {
                   11959:     my ($coursenum,$coursedomain)=@_;
                   11960:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 11961:     &devalidate_cache_new('courseres',$hashid);
1.28      www      11962: }
                   11963: 
1.763     www      11964: 
1.200     www      11965: # --------------------------------------------------- Course Resourcedata Query
1.878     foxr     11966: #
                   11967: #  Parameters:
                   11968: #      $coursenum    - Number of the course.
                   11969: #      $coursedomain - Domain at which the course was created.
                   11970: #  Returns:
                   11971: #     A hash of the course parameters along (I think) with timestamps
                   11972: #     and version info.
1.877     foxr     11973: 
1.624     albertel 11974: sub get_courseresdata {
                   11975:     my ($coursenum,$coursedomain)=@_;
1.200     www      11976:     my $coursehom=&homeserver($coursenum,$coursedomain);
                   11977:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 11978:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624     albertel 11979:     my %dumpreply;
1.417     albertel 11980:     unless (defined($cached)) {
1.624     albertel 11981: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417     albertel 11982: 	$result=\%dumpreply;
1.251     albertel 11983: 	my ($tmp) = keys(%dumpreply);
                   11984: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599     albertel 11985: 	    &do_cache_new('courseres',$hashid,$result,600);
1.306     albertel 11986: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
                   11987: 	    return $tmp;
1.416     albertel 11988: 	} elsif ($tmp =~ /^(error)/) {
1.417     albertel 11989: 	    $result=undef;
1.599     albertel 11990: 	    &do_cache_new('courseres',$hashid,$result,600);
1.250     albertel 11991: 	}
                   11992:     }
1.624     albertel 11993:     return $result;
                   11994: }
                   11995: 
1.633     albertel 11996: sub devalidateuserresdata {
                   11997:     my ($uname,$udom)=@_;
                   11998:     my $hashid="$udom:$uname";
                   11999:     &devalidate_cache_new('userres',$hashid);
                   12000: }
                   12001: 
1.624     albertel 12002: sub get_userresdata {
                   12003:     my ($uname,$udom)=@_;
                   12004:     #most student don\'t have any data set, check if there is some data
                   12005:     if (&EXT_cache_status($udom,$uname)) { return undef; }
                   12006: 
                   12007:     my $hashid="$udom:$uname";
                   12008:     my ($result,$cached)=&is_cached_new('userres',$hashid);
                   12009:     if (!defined($cached)) {
                   12010: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
                   12011: 	$result=\%resourcedata;
                   12012: 	&do_cache_new('userres',$hashid,$result,600);
                   12013:     }
                   12014:     my ($tmp)=keys(%$result);
                   12015:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
                   12016: 	return $result;
                   12017:     }
                   12018:     #error 2 occurs when the .db doesn't exist
                   12019:     if ($tmp!~/error: 2 /) {
1.1294    raeburn  12020:         if ((!defined($cached)) || ($tmp ne 'con_lost')) {
                   12021: 	    &logthis("<font color=\"blue\">WARNING:".
                   12022: 		     " Trying to get resource data for ".
                   12023: 		     $uname." at ".$udom.": ".
                   12024: 		     $tmp."</font>");
                   12025:         }
1.624     albertel 12026:     } elsif ($tmp=~/error: 2 /) {
1.633     albertel 12027: 	#&EXT_cache_set($udom,$uname);
                   12028: 	&do_cache_new('userres',$hashid,undef,600);
1.636     albertel 12029: 	undef($tmp); # not really an error so don't send it back
1.624     albertel 12030:     }
                   12031:     return $tmp;
                   12032: }
1.879     foxr     12033: #----------------------------------------------- resdata - return resource data
                   12034: #  Purpose:
                   12035: #    Return resource data for either users or for a course.
                   12036: #  Parameters:
                   12037: #     $name      - Course/user name.
                   12038: #     $domain    - Name of the domain the user/course is registered on.
1.1311    raeburn  12039: #     $type      - Type of thing $name is (must be 'course' or 'user')
1.1301    raeburn  12040: #     $mapp      - decluttered URL of enclosing map  
                   12041: #     $recursed  - Ref to scalar -- set to 1, if nested maps have been recursed.
                   12042: #     $recurseup - Ref to array of map URLs, starting with map containing
                   12043: #                  $mapp up through hierarchy of nested maps to top level map.  
                   12044: #     $courseid  - CourseID (first part of param identifier).
                   12045: #     $modifier  - Middle part of param identifier.
                   12046: #     $what      - Last part of param identifier.
1.879     foxr     12047: #     @which     - Array of names of resources desired.
                   12048: #  Returns:
                   12049: #     The value of the first reasource in @which that is found in the
                   12050: #     resource hash.
                   12051: #  Exceptional Conditions:
                   12052: #     If the $type passed in is not valid (not the string 'course' or 
                   12053: #     'user', an undefined  reference is returned.
                   12054: #     If none of the resources are found, an undef is returned
1.624     albertel 12055: sub resdata {
1.1301    raeburn  12056:     my ($name,$domain,$type,$mapp,$recursed,$recurseup,$courseid,
                   12057:         $modifier,$what,@which)=@_;
1.624     albertel 12058:     my $result;
                   12059:     if ($type eq 'course') {
                   12060: 	$result=&get_courseresdata($name,$domain);
                   12061:     } elsif ($type eq 'user') {
                   12062: 	$result=&get_userresdata($name,$domain);
                   12063:     }
                   12064:     if (!ref($result)) { return $result; }    
1.251     albertel 12065:     foreach my $item (@which) {
1.1301    raeburn  12066:         if ($item->[1] eq 'course') {
                   12067:             if ((ref($recurseup) eq 'ARRAY') && (ref($recursed) eq 'SCALAR')) {
                   12068:                 unless ($$recursed) {
1.1302    raeburn  12069:                     @{$recurseup} = &get_map_hierarchy($mapp,$courseid);
1.1301    raeburn  12070:                     $$recursed = 1;
                   12071:                 }
                   12072:                 foreach my $item (@${recurseup}) {
                   12073:                     my $norecursechk=$courseid.$modifier.$item.'___(all).'.$what;
                   12074:                     last if (defined($result->{$norecursechk}));
                   12075:                     my $recursechk=$courseid.$modifier.$item.'___(rec).'.$what;
                   12076:                     if (defined($result->{$recursechk})) { return [$result->{$recursechk},'map']; }
                   12077:                 }
                   12078:             }
                   12079:         }
                   12080:         if (defined($result->{$item->[0]})) {
1.927     albertel 12081: 	    return [$result->{$item->[0]},$item->[1]];
1.251     albertel 12082: 	}
1.250     albertel 12083:     }
1.291     albertel 12084:     return undef;
1.200     www      12085: }
                   12086: 
1.1360    raeburn  12087: sub get_domain_lti {
                   12088:     my ($cdom,$context) = @_;
                   12089:     my ($name,%lti);
                   12090:     if ($context eq 'consumer') {
                   12091:         $name = 'ltitools';
                   12092:     } elsif ($context eq 'provider') {
                   12093:         $name = 'lti';
                   12094:     } else {
                   12095:         return %lti;
                   12096:     }
                   12097:     my ($result,$cached)=&is_cached_new($name,$cdom);
1.1298    raeburn  12098:     if (defined($cached)) {
                   12099:         if (ref($result) eq 'HASH') {
1.1360    raeburn  12100:             %lti = %{$result};
1.1298    raeburn  12101:         }
                   12102:     } else {
1.1360    raeburn  12103:         my %domconfig = &get_dom('configuration',[$name],$cdom);
                   12104:         if (ref($domconfig{$name}) eq 'HASH') {
                   12105:             %lti = %{$domconfig{$name}};
                   12106:             my %encdomconfig = &get_dom('encconfig',[$name],$cdom);
                   12107:             if (ref($encdomconfig{$name}) eq 'HASH') {
                   12108:                 foreach my $id (keys(%lti)) {
                   12109:                     if (ref($encdomconfig{$name}{$id}) eq 'HASH') {
1.1344    raeburn  12110:                         foreach my $item ('key','secret') {
1.1360    raeburn  12111:                             $lti{$id}{$item} = $encdomconfig{$name}{$id}{$item};
1.1344    raeburn  12112:                         }
                   12113:                     }
                   12114:                 }
                   12115:             }
1.1298    raeburn  12116:         }
                   12117:         my $cachetime = 24*60*60;
1.1360    raeburn  12118:         &do_cache_new($name,$cdom,\%lti,$cachetime);
1.1298    raeburn  12119:     }
1.1360    raeburn  12120:     return %lti;
1.1298    raeburn  12121: }
                   12122: 
1.1236    raeburn  12123: sub get_numsuppfiles {
                   12124:     my ($cnum,$cdom,$ignorecache)=@_;
                   12125:     my $hashid=$cnum.':'.$cdom;
                   12126:     my ($suppcount,$cached);
                   12127:     unless ($ignorecache) {
                   12128:         ($suppcount,$cached) = &is_cached_new('suppcount',$hashid);
                   12129:     }
                   12130:     unless (defined($cached)) {
                   12131:         my $chome=&homeserver($cnum,$cdom);
                   12132:         unless ($chome eq 'no_host') {
1.1366    raeburn  12133:             ($suppcount,my $supptools,my $errors) = (0,0,0);
1.1236    raeburn  12134:             my $suppmap = 'supplemental.sequence';
1.1366    raeburn  12135:             ($suppcount,$supptools,$errors) =
                   12136:                 &Apache::loncommon::recurse_supplemental($cnum,$cdom,$suppmap,$suppcount,
                   12137:                                                          $supptools,$errors);
1.1236    raeburn  12138:         }
                   12139:         &do_cache_new('suppcount',$hashid,$suppcount,600);
                   12140:     }
                   12141:     return $suppcount;
                   12142: }
                   12143: 
1.379     matthew  12144: #
                   12145: # EXT resource caching routines
                   12146: #
                   12147: 
1.1302    raeburn  12148: {
                   12149: # Cache (5 seconds) of map hierarchy for speedup of navmaps display
                   12150: #
                   12151: # The course for which we cache
                   12152: my $cachedmapkey='';
                   12153: # The cached recursive maps for this course
                   12154: my %cachedmaps=();
                   12155: # When this was last done
                   12156: my $cachedmaptime='';
                   12157: 
1.379     matthew  12158: sub clear_EXT_cache_status {
1.383     albertel 12159:     &delenv('cache.EXT.');
1.379     matthew  12160: }
                   12161: 
                   12162: sub EXT_cache_status {
                   12163:     my ($target_domain,$target_user) = @_;
1.383     albertel 12164:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620     albertel 12165:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379     matthew  12166:         # We know already the user has no data
                   12167:         return 1;
                   12168:     } else {
                   12169:         return 0;
                   12170:     }
                   12171: }
                   12172: 
                   12173: sub EXT_cache_set {
                   12174:     my ($target_domain,$target_user) = @_;
1.383     albertel 12175:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.949     raeburn  12176:     #&appenv({$cachename => time});
1.379     matthew  12177: }
                   12178: 
1.28      www      12179: # --------------------------------------------------------- Value of a Variable
1.58      www      12180: sub EXT {
1.715     albertel 12181: 
1.1228    raeburn  12182:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse,$cid)=@_;
1.68      www      12183:     unless ($varname) { return ''; }
1.218     albertel 12184:     #get real user name/domain, courseid and symb
                   12185:     my $courseid;
1.359     albertel 12186:     my $publicuser;
1.427     www      12187:     if ($symbparm) {
                   12188: 	$symbparm=&get_symb_from_alias($symbparm);
                   12189:     }
1.218     albertel 12190:     if (!($uname && $udom)) {
1.790     albertel 12191:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218     albertel 12192:       if (!$symbparm) {	$symbparm=$cursymb; }
                   12193:     } else {
1.620     albertel 12194: 	$courseid=$env{'request.course.id'};
1.218     albertel 12195:     }
1.48      www      12196:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
                   12197:     my $rest;
1.320     albertel 12198:     if (defined($therest[0])) {
1.48      www      12199:        $rest=join('.',@therest);
                   12200:     } else {
                   12201:        $rest='';
                   12202:     }
1.320     albertel 12203: 
1.57      www      12204:     my $qualifierrest=$qualifier;
                   12205:     if ($rest) { $qualifierrest.='.'.$rest; }
                   12206:     my $spacequalifierrest=$space;
                   12207:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28      www      12208:     if ($realm eq 'user') {
1.48      www      12209: # --------------------------------------------------------------- user.resource
                   12210: 	if ($space eq 'resource') {
1.651     albertel 12211: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
                   12212: 		  || defined($Apache::lonhomework::parsing_a_task))
                   12213: 		 &&
1.744     albertel 12214: 		 ($symbparm eq &symbread()) ) {	
                   12215: 		# if we are in the middle of processing the resource the
                   12216: 		# get the value we are planning on committing
                   12217:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
                   12218:                     return $Apache::lonhomework::results{$qualifierrest};
                   12219:                 } else {
                   12220:                     return $Apache::lonhomework::history{$qualifierrest};
                   12221:                 }
1.335     albertel 12222: 	    } else {
1.359     albertel 12223: 		my %restored;
1.620     albertel 12224: 		if ($publicuser || $env{'request.state'} eq 'construct') {
1.359     albertel 12225: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
                   12226: 		} else {
                   12227: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
                   12228: 		}
1.335     albertel 12229: 		return $restored{$qualifierrest};
                   12230: 	    }
1.48      www      12231: # ----------------------------------------------------------------- user.access
                   12232:         } elsif ($space eq 'access') {
1.218     albertel 12233: 	    # FIXME - not supporting calls for a specific user
1.48      www      12234:             return &allowed($qualifier,$rest);
                   12235: # ------------------------------------------ user.preferences, user.environment
                   12236:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620     albertel 12237: 	    if (($uname eq $env{'user.name'}) &&
                   12238: 		($udom eq $env{'user.domain'})) {
                   12239: 		return $env{join('.',('environment',$qualifierrest))};
1.218     albertel 12240: 	    } else {
1.359     albertel 12241: 		my %returnhash;
                   12242: 		if (!$publicuser) {
                   12243: 		    %returnhash=&userenvironment($udom,$uname,
                   12244: 						 $qualifierrest);
                   12245: 		}
1.218     albertel 12246: 		return $returnhash{$qualifierrest};
                   12247: 	    }
1.48      www      12248: # ----------------------------------------------------------------- user.course
                   12249:         } elsif ($space eq 'course') {
1.218     albertel 12250: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 12251:             return $env{join('.',('request.course',$qualifier))};
1.48      www      12252: # ------------------------------------------------------------------- user.role
                   12253:         } elsif ($space eq 'role') {
1.218     albertel 12254: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 12255:             my ($role,$where)=split(/\./,$env{'request.role'});
1.48      www      12256:             if ($qualifier eq 'value') {
                   12257: 		return $role;
                   12258:             } elsif ($qualifier eq 'extent') {
                   12259:                 return $where;
                   12260:             }
                   12261: # ----------------------------------------------------------------- user.domain
                   12262:         } elsif ($space eq 'domain') {
1.218     albertel 12263:             return $udom;
1.48      www      12264: # ------------------------------------------------------------------- user.name
                   12265:         } elsif ($space eq 'name') {
1.218     albertel 12266:             return $uname;
1.48      www      12267: # ---------------------------------------------------- Any other user namespace
1.29      www      12268:         } else {
1.359     albertel 12269: 	    my %reply;
                   12270: 	    if (!$publicuser) {
                   12271: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
                   12272: 	    }
                   12273: 	    return $reply{$qualifierrest};
1.48      www      12274:         }
1.236     www      12275:     } elsif ($realm eq 'query') {
                   12276: # ---------------------------------------------- pull stuff out of query string
1.384     albertel 12277:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   12278: 						[$spacequalifierrest]);
1.620     albertel 12279: 	return $env{'form.'.$spacequalifierrest}; 
1.236     www      12280:    } elsif ($realm eq 'request') {
1.48      www      12281: # ------------------------------------------------------------- request.browser
                   12282:         if ($space eq 'browser') {
1.1145    bisitz   12283:             return $env{'browser.'.$qualifier};
1.57      www      12284: # ------------------------------------------------------------ request.filename
                   12285:         } else {
1.620     albertel 12286:             return $env{'request.'.$spacequalifierrest};
1.29      www      12287:         }
1.28      www      12288:     } elsif ($realm eq 'course') {
1.48      www      12289: # ---------------------------------------------------------- course.description
1.620     albertel 12290:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57      www      12291:     } elsif ($realm eq 'resource') {
1.165     www      12292: 
1.620     albertel 12293: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539     albertel 12294: 	    if (!$symbparm) { $symbparm=&symbread(); }
                   12295: 	}
1.693     albertel 12296: 
1.1232    raeburn  12297:         if ($qualifier eq '') {
                   12298: 	    if ($space eq 'title') {
                   12299: 	        if (!$symbparm) { $symbparm = $env{'request.filename'}; }
                   12300: 	        return &gettitle($symbparm);
                   12301: 	    }
1.693     albertel 12302: 	
1.1232    raeburn  12303: 	    if ($space eq 'map') {
                   12304: 	        my ($map) = &decode_symb($symbparm);
                   12305: 	        return &symbread($map);
                   12306: 	    }
                   12307:             if ($space eq 'maptitle') {
                   12308:                 my ($map) = &decode_symb($symbparm);
                   12309:                 return &gettitle($map);
                   12310:             }
                   12311: 	    if ($space eq 'filename') {
                   12312: 	        if ($symbparm) {
                   12313: 		    return &clutter((&decode_symb($symbparm))[2]);
                   12314: 	        }
                   12315: 	        return &hreflocation('',$env{'request.filename'});
1.905     albertel 12316: 	    }
1.1232    raeburn  12317: 
1.1233    raeburn  12318:             if ((defined($courseid)) && ($courseid eq $env{'request.course.id'}) && $symbparm) {
                   12319:                 if ($space eq 'visibleparts') {
                   12320:                     my $navmap = Apache::lonnavmaps::navmap->new();
                   12321:                     my $item;
                   12322:                     if (ref($navmap)) {
                   12323:                         my $res = $navmap->getBySymb($symbparm);
                   12324:                         my $parts = $res->parts();
                   12325:                         if (ref($parts) eq 'ARRAY') {
                   12326:                             $item = join(',',@{$parts});
                   12327:                         }
                   12328:                         undef($navmap);
1.1232    raeburn  12329:                     }
1.1233    raeburn  12330:                     return $item;
1.1232    raeburn  12331:                 }
                   12332:             }
                   12333:         }
1.693     albertel 12334: 
1.1301    raeburn  12335: 	my ($section, $group, @groups, @recurseup, $recursed);
                   12336: 	my ($courselevelm,$courseleveli,$courselevel,$mapp);
1.1228    raeburn  12337:         if (($courseid eq '') && ($cid)) {
                   12338:             $courseid = $cid;
                   12339:         }
                   12340: 	if (($symbparm && $courseid) && 
                   12341: 	    (($courseid eq $env{'request.course.id'}) || ($courseid eq $cid)))  {
1.165     www      12342: 
1.218     albertel 12343: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165     www      12344: 
1.60      www      12345: # ----------------------------------------------------- Cascading lookup scheme
1.218     albertel 12346: 	    my $symbp=$symbparm;
1.1301    raeburn  12347: 	    $mapp=&deversion((&decode_symb($symbp))[0]);
1.218     albertel 12348: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
1.1301    raeburn  12349:             my $recurseparm=$mapp.'___(rec).'.$spacequalifierrest;
1.218     albertel 12350: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
1.620     albertel 12351: 	    if (($env{'user.name'} eq $uname) &&
                   12352: 		($env{'user.domain'} eq $udom)) {
                   12353: 		$section=$env{'request.course.sec'};
1.733     raeburn  12354:                 @groups = split(/:/,$env{'request.course.groups'});  
                   12355:                 @groups=&sort_course_groups($courseid,@groups); 
1.218     albertel 12356: 	    } else {
1.539     albertel 12357: 		if (! defined($usection)) {
1.551     albertel 12358: 		    $section=&getsection($udom,$uname,$courseid);
1.539     albertel 12359: 		} else {
                   12360: 		    $section = $usection;
                   12361: 		}
1.733     raeburn  12362:                 @groups = &get_users_groups($udom,$uname,$courseid);
1.218     albertel 12363: 	    }
                   12364: 
                   12365: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
                   12366: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
1.1301    raeburn  12367:             my $secleveli=$courseid.'.['.$section.'].'.$recurseparm;
1.218     albertel 12368: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
                   12369: 
1.593     albertel 12370: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
1.218     albertel 12371: 	    my $courselevelr=$courseid.'.'.$symbparm;
1.1301    raeburn  12372:             $courseleveli=$courseid.'.'.$recurseparm;
1.593     albertel 12373: 	    $courselevelm=$courseid.'.'.$mapparm;
1.69      www      12374: 
1.60      www      12375: # ----------------------------------------------------------- first, check user
1.624     albertel 12376: 
1.1301    raeburn  12377: 	    my $userreply=&resdata($uname,$udom,'user',$mapp,\$recursed,
                   12378:                                    \@recurseup,$courseid,'.',$spacequalifierrest, 
1.927     albertel 12379: 				       ([$courselevelr,'resource'],
                   12380: 					[$courselevelm,'map'     ],
1.1301    raeburn  12381:                                         [$courseleveli,'map'     ],
1.927     albertel 12382: 					[$courselevel, 'course'  ]));
1.931     albertel 12383: 	    if (defined($userreply)) { return &get_reply($userreply); }
1.95      www      12384: 
1.594     albertel 12385: # ------------------------------------------------ second, check some of course
1.684     raeburn  12386:             my $coursereply;
1.691     raeburn  12387:             if (@groups > 0) {
                   12388:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
1.1301    raeburn  12389:                                        $recurseparm,$mapparm,$spacequalifierrest,
                   12390:                                        $mapp,\$recursed,\@recurseup);
                   12391:                 if (defined($coursereply)) { return &get_reply($coursereply); } 
1.684     raeburn  12392:             }
1.96      www      12393: 
1.684     raeburn  12394: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.927     albertel 12395: 				  $env{'course.'.$courseid.'.domain'},
1.1301    raeburn  12396: 				  'course',$mapp,\$recursed,\@recurseup,
                   12397:                                   $courseid,'.['.$section.'].',$spacequalifierrest,
1.927     albertel 12398: 				  ([$seclevelr,   'resource'],
                   12399: 				   [$seclevelm,   'map'     ],
1.1301    raeburn  12400:                                    [$secleveli,   'map'     ],
1.927     albertel 12401: 				   [$seclevel,    'course'  ],
                   12402: 				   [$courselevelr,'resource']));
                   12403: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
1.200     www      12404: 
1.60      www      12405: # ------------------------------------------------------ third, check map parms
1.218     albertel 12406: 	    my %parmhash=();
                   12407: 	    my $thisparm='';
                   12408: 	    if (tie(%parmhash,'GDBM_File',
1.620     albertel 12409: 		    $env{'request.course.fn'}.'_parms.db',
1.256     albertel 12410: 		    &GDBM_READER(),0640)) {
1.218     albertel 12411: 		$thisparm=$parmhash{$symbparm};
                   12412: 		untie(%parmhash);
                   12413: 	    }
1.927     albertel 12414: 	    if ($thisparm) { return &get_reply([$thisparm,'resource']); }
1.218     albertel 12415: 	}
1.594     albertel 12416: # ------------------------------------------ fourth, look in resource metadata
1.1301    raeburn  12417:  
                   12418:         my $what = $spacequalifierrest;
                   12419: 	$what=~s/\./\_/;
1.282     albertel 12420: 	my $filename;
                   12421: 	if (!$symbparm) { $symbparm=&symbread(); }
                   12422: 	if ($symbparm) {
1.409     www      12423: 	    $filename=(&decode_symb($symbparm))[2];
1.282     albertel 12424: 	} else {
1.620     albertel 12425: 	    $filename=$env{'request.filename'};
1.282     albertel 12426: 	}
1.1362    raeburn  12427:         my $toolsymb;
                   12428:         if (($filename =~ /ext\.tool$/) && ($what ne '0_gradable')) {
                   12429:             $toolsymb = $symbparm;
                   12430:         }
                   12431: 	my $metadata=&metadata($filename,$what,$toolsymb);
1.927     albertel 12432: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.1362    raeburn  12433: 	$metadata=&metadata($filename,'parameter_'.$what,$toolsymb);
1.927     albertel 12434: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.142     www      12435: 
1.1301    raeburn  12436: # ----------------------------------------------- fifth, look in rest of course
1.593     albertel 12437: 	if ($symbparm && defined($courseid) && 
1.620     albertel 12438: 	    $courseid eq $env{'request.course.id'}) {
1.624     albertel 12439: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
                   12440: 				     $env{'course.'.$courseid.'.domain'},
1.1301    raeburn  12441: 				     'course',$mapp,\$recursed,\@recurseup,
                   12442:                                      $courseid,'.',$spacequalifierrest,
1.927     albertel 12443: 				     ([$courselevelm,'map'   ],
1.1301    raeburn  12444:                                       [$courseleveli,'map'   ],
1.927     albertel 12445: 				      [$courselevel, 'course']));
                   12446: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
1.593     albertel 12447: 	}
1.145     www      12448: # ------------------------------------------------------------------ Cascade up
1.218     albertel 12449: 	unless ($space eq '0') {
1.336     albertel 12450: 	    my @parts=split(/_/,$space);
                   12451: 	    my $id=pop(@parts);
                   12452: 	    my $part=join('_',@parts);
                   12453: 	    if ($part eq '') { $part='0'; }
1.927     albertel 12454: 	    my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395     albertel 12455: 				 $symbparm,$udom,$uname,$section,1);
1.938     raeburn  12456: 	    if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
1.218     albertel 12457: 	}
1.395     albertel 12458: 	if ($recurse) { return undef; }
1.1362    raeburn  12459: 	my $pack_def=&packages_tab_default($filename,$varname,$toolsymb);
1.927     albertel 12460: 	if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
1.48      www      12461: # ---------------------------------------------------- Any other user namespace
                   12462:     } elsif ($realm eq 'environment') {
                   12463: # ----------------------------------------------------------------- environment
1.620     albertel 12464: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
                   12465: 	    return $env{'environment.'.$spacequalifierrest};
1.219     albertel 12466: 	} else {
1.770     albertel 12467: 	    if ($uname eq 'anonymous' && $udom eq '') {
                   12468: 		return '';
                   12469: 	    }
1.219     albertel 12470: 	    my %returnhash=&userenvironment($udom,$uname,
                   12471: 					    $spacequalifierrest);
                   12472: 	    return $returnhash{$spacequalifierrest};
                   12473: 	}
1.28      www      12474:     } elsif ($realm eq 'system') {
1.48      www      12475: # ----------------------------------------------------------------- system.time
                   12476: 	if ($space eq 'time') {
                   12477: 	    return time;
                   12478:         }
1.696     albertel 12479:     } elsif ($realm eq 'server') {
                   12480: # ----------------------------------------------------------------- system.time
                   12481: 	if ($space eq 'name') {
                   12482: 	    return $ENV{'SERVER_NAME'};
                   12483:         }
1.1415    raeburn  12484:     } elsif ($realm eq 'client') {
                   12485:         if ($space eq 'remote_addr') {
1.1441    raeburn  12486:             return &get_requestor_ip();
1.1415    raeburn  12487:         }
1.28      www      12488:     }
1.48      www      12489:     return '';
1.61      www      12490: }
                   12491: 
1.927     albertel 12492: sub get_reply {
                   12493:     my ($reply_value) = @_;
1.940     raeburn  12494:     if (ref($reply_value) eq 'ARRAY') {
                   12495:         if (wantarray) {
                   12496: 	    return @$reply_value;
                   12497:         }
                   12498:         return $reply_value->[0];
                   12499:     } else {
                   12500:         return $reply_value;
1.927     albertel 12501:     }
                   12502: }
                   12503: 
1.691     raeburn  12504: sub check_group_parms {
1.1301    raeburn  12505:     my ($courseid,$groups,$symbparm,$recurseparm,$mapparm,$what,$mapp,
                   12506:         $recursed,$recurseupref) = @_;
                   12507:     my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$recurseparm,'map'],
                   12508:                   [$what,'course']);
                   12509:     my $coursereply;
1.691     raeburn  12510:     foreach my $group (@{$groups}) {
1.1301    raeburn  12511:         my @groupitems = ();
1.691     raeburn  12512:         foreach my $level (@levels) {
1.927     albertel 12513:              my $item = $courseid.'.['.$group.'].'.$level->[0];
                   12514:              push(@groupitems,[$item,$level->[1]]);
1.691     raeburn  12515:         }
1.1301    raeburn  12516:         my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
                   12517:                                    $env{'course.'.$courseid.'.domain'},
                   12518:                                    'course',$mapp,$recursed,$recurseupref,
                   12519:                                    $courseid,'.['.$group.'].',$what,
                   12520:                                    @groupitems);
                   12521:         last if (defined($coursereply));
1.691     raeburn  12522:     }
                   12523:     return $coursereply;
                   12524: }
                   12525: 
1.1301    raeburn  12526: sub get_map_hierarchy {
1.1302    raeburn  12527:     my ($mapname,$courseid) = @_;
                   12528:     my @recurseup = ();
1.1301    raeburn  12529:     if ($mapname) {
1.1302    raeburn  12530:         if (($cachedmapkey eq $courseid) &&
                   12531:             (abs($cachedmaptime-time)<5)) {
                   12532:             if (ref($cachedmaps{$mapname}) eq 'ARRAY') {
                   12533:                 return @{$cachedmaps{$mapname}};
                   12534:             }
                   12535:         }
1.1301    raeburn  12536:         my $navmap = Apache::lonnavmaps::navmap->new();
                   12537:         if (ref($navmap)) {
                   12538:             @recurseup = $navmap->recurseup_maps($mapname);
                   12539:             undef($navmap);
1.1302    raeburn  12540:             $cachedmaps{$mapname} = \@recurseup;
                   12541:             $cachedmaptime=time;
                   12542:             $cachedmapkey=$courseid;
1.1301    raeburn  12543:         }
                   12544:     }
                   12545:     return @recurseup;
                   12546: }
                   12547: 
1.1302    raeburn  12548: }
                   12549: 
1.691     raeburn  12550: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733     raeburn  12551:     my ($courseid,@groups) = @_;
                   12552:     @groups = sort(@groups);
1.691     raeburn  12553:     return @groups;
                   12554: }
                   12555: 
1.395     albertel 12556: sub packages_tab_default {
1.1362    raeburn  12557:     my ($uri,$varname,$toolsymb)=@_;
1.395     albertel 12558:     my (undef,$part,$name)=split(/\./,$varname);
1.738     albertel 12559: 
                   12560:     my (@extension,@specifics,$do_default);
1.1362    raeburn  12561:     foreach my $package (split(/,/,&metadata($uri,'packages',$toolsymb))) {
1.395     albertel 12562: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738     albertel 12563: 	if ($pack_type eq 'default') {
                   12564: 	    $do_default=1;
                   12565: 	} elsif ($pack_type eq 'extension') {
                   12566: 	    push(@extension,[$package,$pack_type,$pack_part]);
1.885     albertel 12567: 	} elsif ($pack_part eq $part || $pack_type eq 'part') {
1.848     albertel 12568: 	    # only look at packages defaults for packages that this id is
1.738     albertel 12569: 	    push(@specifics,[$package,$pack_type,$pack_part]);
                   12570: 	}
                   12571:     }
                   12572:     # first look for a package that matches the requested part id
                   12573:     foreach my $package (@specifics) {
                   12574: 	my (undef,$pack_type,$pack_part)=@{$package};
                   12575: 	next if ($pack_part ne $part);
                   12576: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   12577: 	    return $packagetab{"$pack_type&$name&default"};
                   12578: 	}
                   12579:     }
                   12580:     # look for any possible matching non extension_ package
                   12581:     foreach my $package (@specifics) {
                   12582: 	my (undef,$pack_type,$pack_part)=@{$package};
1.468     albertel 12583: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   12584: 	    return $packagetab{"$pack_type&$name&default"};
                   12585: 	}
1.585     albertel 12586: 	if ($pack_type eq 'part') { $pack_part='0'; }
1.468     albertel 12587: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
                   12588: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395     albertel 12589: 	}
                   12590:     }
1.738     albertel 12591:     # look for any posible extension_ match
                   12592:     foreach my $package (@extension) {
                   12593: 	my ($package,$pack_type)=@{$package};
                   12594: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   12595: 	    return $packagetab{"$pack_type&$name&default"};
                   12596: 	}
                   12597: 	if (defined($packagetab{$package."&$name&default"})) {
                   12598: 	    return $packagetab{$package."&$name&default"};
                   12599: 	}
                   12600:     }
                   12601:     # look for a global default setting
                   12602:     if ($do_default && defined($packagetab{"default&$name&default"})) {
                   12603: 	return $packagetab{"default&$name&default"};
                   12604:     }
1.395     albertel 12605:     return undef;
                   12606: }
                   12607: 
1.334     albertel 12608: sub add_prefix_and_part {
                   12609:     my ($prefix,$part)=@_;
                   12610:     my $keyroot;
                   12611:     if (defined($prefix) && $prefix !~ /^__/) {
                   12612: 	# prefix that has a part already
                   12613: 	$keyroot=$prefix;
                   12614:     } elsif (defined($prefix)) {
                   12615: 	# prefix that is missing a part
                   12616: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
                   12617:     } else {
                   12618: 	# no prefix at all
                   12619: 	if (defined($part)) { $keyroot='_'.$part; }
                   12620:     }
                   12621:     return $keyroot;
                   12622: }
                   12623: 
1.71      www      12624: # ---------------------------------------------------------------- Get metadata
                   12625: 
1.599     albertel 12626: my %metaentry;
1.1070    www      12627: my %importedpartids;
1.1369    raeburn  12628: my %importedrespids;
1.71      www      12629: sub metadata {
1.1362    raeburn  12630:     my ($uri,$what,$toolsymb,$liburi,$prefix,$depthcount)=@_;
1.71      www      12631:     $uri=&declutter($uri);
1.288     albertel 12632:     # if it is a non metadata possible uri return quickly
1.529     albertel 12633:     if (($uri eq '') || 
                   12634: 	(($uri =~ m|^/*adm/|) && 
1.1343    raeburn  12635: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m{/(smppg|bulletinboard|ext\.tool)$})) ||
1.1108    raeburn  12636:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
1.924     albertel 12637: 	return undef;
                   12638:     }
1.1261    raeburn  12639:     if (($uri =~ /^priv/ || $uri=~m{^home/httpd/html/priv}) 
1.924     albertel 12640: 	&& &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
1.468     albertel 12641: 	return undef;
1.288     albertel 12642:     }
1.73      www      12643:     my $filename=$uri;
                   12644:     $uri=~s/\.meta$//;
1.172     www      12645: #
                   12646: # Is the metadata already cached?
1.177     www      12647: # Look at timestamp of caching
1.172     www      12648: # Everything is cached by the main uri, libraries are never directly cached
                   12649: #
1.428     albertel 12650:     if (!defined($liburi)) {
1.599     albertel 12651: 	my ($result,$cached)=&is_cached_new('meta',$uri);
1.428     albertel 12652: 	if (defined($cached)) { return $result->{':'.$what}; }
                   12653:     }
1.1362    raeburn  12654: 
                   12655: #
                   12656: # If the uri is for an external tool the file from
                   12657: # which metadata should be retrieved depends on whether
                   12658: # the tool had been configured to be gradable (set in the Course
                   12659: # Editor or Resource Editor).
                   12660: #
                   12661: # If a valid symb has been included as the third arg in the call
                   12662: # to &metadata() that can be used to retrieve the value of
                   12663: # parameter_0_gradable set for the resource, and included in the
                   12664: # uploaded map containing the tool. The value is retrieved via
                   12665: # &EXT(), if a valid symb is available.  Otherwise the value of
                   12666: # gradable in the exttool_$marker.db file for the tool instance
1.1364    raeburn  12667: # is retrieved via &get().
                   12668: #
                   12669: # When lonuserstate::traceroute() calls lonnet::EXT() for 
                   12670: # hiddenresource and encrypturl (during course initialization)
                   12671: # the map-level parameter for resource.0.gradable included in the 
                   12672: # uploaded map containing the tool will not yet have been stored
                   12673: # in the user_course_parms.db file for the user's session, so in 
                   12674: # this case fall back to retrieving gradable status from the
                   12675: # exttool_$marker.db file.
1.1362    raeburn  12676: #
                   12677: # In order to avoid an infinite loop, &metadata() will return
                   12678: # before a call to &EXT(), if the uri is for an external tool
                   12679: # and the $what for which metadata is being requested is
                   12680: # parameter_0_gradable or 0_gradable.
                   12681: #
                   12682: 
                   12683:     if ($uri =~ /ext\.tool$/) {
                   12684:         if (($what eq 'parameter_0_gradable') || ($what eq '0_gradable')) {
                   12685:             return;
                   12686:         } else {
                   12687:             my ($checked,$use_passback);
                   12688:             if ($toolsymb ne '') {
                   12689:                 (undef,undef,my $tooluri) = &decode_symb($toolsymb);
1.1364    raeburn  12690:                 if (($tooluri eq $uri) && (&EXT('resource.0.gradable',$toolsymb))) {
1.1362    raeburn  12691:                     $checked = 1;
                   12692:                     if (&EXT('resource.0.gradable',$toolsymb) =~ /^yes$/i) {
                   12693:                         $use_passback = 1;
                   12694:                     }
                   12695:                 }
                   12696:             }
                   12697:             unless ($checked) {
                   12698:                 my ($ignore,$cdom,$cnum,$marker) = split(m{/},$uri);
                   12699:                 $marker=~s/\D//g;
1.1363    raeburn  12700:                 if ($marker) {
1.1362    raeburn  12701:                     my %toolsettings=&get('exttool_'.$marker,['gradable'],$cdom,$cnum);
                   12702:                     $use_passback = $toolsettings{'gradable'};
                   12703:                 }
                   12704:             }
                   12705:             if ($use_passback) {
                   12706:                 $filename = '/home/httpd/html/res/lib/templates/LTIpassback.tool';
                   12707:             } else {
                   12708:                 $filename = '/home/httpd/html/res/lib/templates/LTIstandard.tool';
                   12709:             }
                   12710:         }
                   12711:     }
                   12712: 
1.428     albertel 12713:     {
1.1069    www      12714: # Imported parts would go here
1.1369    raeburn  12715:         my @origfiletagids=();
1.1069    www      12716:         my $importedparts=0;
1.1369    raeburn  12717: 
                   12718: # Imported responseids would go here
                   12719:         my $importedresponses=0;
1.172     www      12720: #
                   12721: # Is this a recursive call for a library?
                   12722: #
1.599     albertel 12723: #	if (! exists($metacache{$uri})) {
                   12724: #	    $metacache{$uri}={};
                   12725: #	}
1.924     albertel 12726: 	my $cachetime = 60*60;
1.171     www      12727:         if ($liburi) {
                   12728: 	    $liburi=&declutter($liburi);
                   12729:             $filename=$liburi;
1.401     bowersj2 12730:         } else {
1.599     albertel 12731: 	    &devalidate_cache_new('meta',$uri);
                   12732: 	    undef(%metaentry);
1.401     bowersj2 12733: 	}
1.140     www      12734:         my %metathesekeys=();
1.73      www      12735:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489     albertel 12736: 	my $metastring;
1.1140    www      12737: 	if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
1.929     albertel 12738: 	    my $which = &hreflocation('','/'.($liburi || $uri));
1.924     albertel 12739: 	    $metastring = 
1.929     albertel 12740: 		&Apache::lonnet::ssi_body($which,
1.924     albertel 12741: 					  ('grade_target' => 'meta'));
                   12742: 	    $cachetime = 1; # only want this cached in the child not long term
1.1108    raeburn  12743: 	} elsif (($uri !~ m -^(editupload)/-) && 
                   12744:                  ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
1.543     albertel 12745: 	    my $file=&filelocation('',&clutter($filename));
1.599     albertel 12746: 	    #push(@{$metaentry{$uri.'.file'}},$file);
1.543     albertel 12747: 	    $metastring=&getfile($file);
1.489     albertel 12748: 	}
1.208     albertel 12749:         my $parser=HTML::LCParser->new(\$metastring);
1.71      www      12750:         my $token;
1.140     www      12751:         undef %metathesekeys;
1.71      www      12752:         while ($token=$parser->get_token) {
1.339     albertel 12753: 	    if ($token->[0] eq 'S') {
                   12754: 		if (defined($token->[2]->{'package'})) {
1.172     www      12755: #
                   12756: # This is a package - get package info
                   12757: #
1.339     albertel 12758: 		    my $package=$token->[2]->{'package'};
                   12759: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   12760: 		    if (defined($token->[2]->{'id'})) { 
                   12761: 			$keyroot.='_'.$token->[2]->{'id'}; 
                   12762: 		    }
1.599     albertel 12763: 		    if ($metaentry{':packages'}) {
                   12764: 			$metaentry{':packages'}.=','.$package.$keyroot;
1.339     albertel 12765: 		    } else {
1.599     albertel 12766: 			$metaentry{':packages'}=$package.$keyroot;
1.339     albertel 12767: 		    }
1.736     albertel 12768: 		    foreach my $pack_entry (keys(%packagetab)) {
1.432     albertel 12769: 			my $part=$keyroot;
                   12770: 			$part=~s/^\_//;
1.736     albertel 12771: 			if ($pack_entry=~/^\Q$package\E\&/ || 
                   12772: 			    $pack_entry=~/^\Q$package\E_0\&/) {
                   12773: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395     albertel 12774: 			    # ignore package.tab specified default values
                   12775:                             # here &package_tab_default() will fetch those
                   12776: 			    if ($subp eq 'default') { next; }
1.736     albertel 12777: 			    my $value=$packagetab{$pack_entry};
1.432     albertel 12778: 			    my $unikey;
                   12779: 			    if ($pack =~ /_0$/) {
                   12780: 				$unikey='parameter_0_'.$name;
                   12781: 				$part=0;
                   12782: 			    } else {
                   12783: 				$unikey='parameter'.$keyroot.'_'.$name;
                   12784: 			    }
1.339     albertel 12785: 			    if ($subp eq 'display') {
                   12786: 				$value.=' [Part: '.$part.']';
                   12787: 			    }
1.599     albertel 12788: 			    $metaentry{':'.$unikey.'.part'}=$part;
1.395     albertel 12789: 			    $metathesekeys{$unikey}=1;
1.599     albertel 12790: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   12791: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
1.339     albertel 12792: 			    }
1.599     albertel 12793: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
                   12794: 				$metaentry{':'.$unikey}=
                   12795: 				    $metaentry{':'.$unikey.'.default'};
1.356     albertel 12796: 			    }
1.339     albertel 12797: 			}
                   12798: 		    }
                   12799: 		} else {
1.172     www      12800: #
                   12801: # This is not a package - some other kind of start tag
1.339     albertel 12802: #
                   12803: 		    my $entry=$token->[1];
1.1068    www      12804: 		    my $unikey='';
1.175     www      12805: 
1.339     albertel 12806: 		    if ($entry eq 'import') {
1.175     www      12807: #
                   12808: # Importing a library here
1.339     albertel 12809: #
1.1067    www      12810:                         my $location=$parser->get_text('/import');
                   12811:                         my $dir=$filename;
                   12812:                         $dir=~s|[^/]*$||;
                   12813:                         $location=&filelocation($dir,$location);
1.1369    raeburn  12814: 
                   12815:                         my $importid=$token->[2]->{'id'};
1.1068    www      12816:                         my $importmode=$token->[2]->{'importmode'};
1.1369    raeburn  12817: #
                   12818: # Check metadata for imported file to
                   12819: # see if it contained response items
                   12820: #
1.1372    raeburn  12821:                         my ($origfile,@libfilekeys);
1.1370    raeburn  12822:                         my %currmetaentry = %metaentry;
1.1372    raeburn  12823:                         @libfilekeys = split(/,/,&metadata($location,'keys',undef,undef,undef,
                   12824:                                                            $depthcount+1));
                   12825:                         if (grep(/^responseorder$/,@libfilekeys)) {
                   12826:                             my $libresponseorder = &metadata($location,'responseorder',undef,undef,
                   12827:                                                              undef,$depthcount+1);
                   12828:                             if ($libresponseorder ne '') {
                   12829:                                 if ($#origfiletagids<0) {
                   12830:                                     undef(%importedrespids);
                   12831:                                     undef(%importedpartids);
                   12832:                                 }
1.1373    raeburn  12833:                                 my @respids = split(/\s*,\s*/,$libresponseorder);
                   12834:                                 if (@respids) {
                   12835:                                     $importedrespids{$importid} = join(',',map { $importid.'_'.$_ } @respids);
                   12836:                                 }
                   12837:                                 if ($importedrespids{$importid} ne '') {
1.1372    raeburn  12838:                                     $importedresponses = 1;
1.1369    raeburn  12839: # We need to get the original file and the imported file to get the response order correct
                   12840: # Load and inspect original file
1.1372    raeburn  12841:                                     if ($#origfiletagids<0) {
                   12842:                                         my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
                   12843:                                         $origfile=&getfile($origfilelocation);
                   12844:                                         @origfiletagids=($origfile=~/<((?:\w+)response|import|part)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
                   12845:                                     }
1.1369    raeburn  12846:                                 }
                   12847:                             }
                   12848:                         }
1.1370    raeburn  12849: # Do not overwrite contents of %metaentry hash for resource itself with 
                   12850: # hash populated for imported library file
                   12851:                         %metaentry = %currmetaentry;
                   12852:                         undef(%currmetaentry);
1.1374    raeburn  12853:                         if ($importmode eq 'part') {
1.1068    www      12854: # Import as part(s)
1.1069    www      12855:                            $importedparts=1;
                   12856: # We need to get the original file and the imported file to get the part order correct
                   12857: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
1.1369    raeburn  12858: # Load and inspect original file if we didn't do that already
                   12859:                            if ($#origfiletagids<0) {
                   12860:                                undef(%importedrespids);
                   12861:                                undef(%importedpartids);
                   12862:                                if ($origfile eq '') {
                   12863:                                    my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
                   12864:                                    $origfile=&getfile($origfilelocation);
                   12865:                                    @origfiletagids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
                   12866:                                }
1.1070    www      12867:                            }
1.1372    raeburn  12868:                            my @impfilepartids;
                   12869: # If <partorder> tag is included in metadata for the imported file
                   12870: # get the parts in the imported file from that.
                   12871:                            if (grep(/^partorder$/,@libfilekeys)) {
                   12872:                                %currmetaentry = %metaentry;
                   12873:                                my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
                   12874:                                                             $depthcount+1);
                   12875:                                %metaentry = %currmetaentry;
                   12876:                                undef(%currmetaentry);
                   12877:                                if ($libpartorder ne '') {
                   12878:                                    @impfilepartids=split(/\s*,\s*/,$libpartorder);
                   12879:                                }
                   12880:                            } else {
                   12881: # If no <partorder> tag available, load and inspect imported file
                   12882:                                my $impfile=&getfile($location);
                   12883:                                @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
                   12884:                            }
1.1069    www      12885:                            if ($#impfilepartids>=0) {
                   12886: # This problem had parts
1.1070    www      12887:                                $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
1.1069    www      12888:                            } else {
                   12889: # Importing by turning a single problem into a problem part
                   12890: # It gets the import-tags ID as part-ID
                   12891:                                $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
1.1070    www      12892:                                $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
1.1069    www      12893:                            }
1.1068    www      12894:                         } else {
1.1374    raeburn  12895: # Import as problem or as normal import
                   12896:                             $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   12897:                             unless ($importmode eq 'problem') {
1.1068    www      12898: # Normal import
1.1374    raeburn  12899:                                 if (defined($token->[2]->{'id'})) {
                   12900:                                     $unikey.='_'.$token->[2]->{'id'};
                   12901:                                 }
                   12902:                             }
                   12903: # Check metadata for imported file to
                   12904: # see if it contained parts
                   12905:                             if (grep(/^partorder$/,@libfilekeys)) {
                   12906:                                 %currmetaentry = %metaentry;
                   12907:                                 my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
                   12908:                                                              $depthcount+1);
                   12909:                                 %metaentry = %currmetaentry;
                   12910:                                 undef(%currmetaentry);
                   12911:                                 if ($libpartorder ne '') {
                   12912:                                     $importedparts = 1;
                   12913:                                     $importedpartids{$token->[2]->{'id'}}=$libpartorder;
                   12914:                                 }
                   12915:                             }
1.1067    www      12916:                         }
1.339     albertel 12917: 			if ($depthcount<20) {
1.736     albertel 12918: 			    my $metadata = 
1.1362    raeburn  12919: 				&metadata($uri,'keys',$toolsymb,$location,$unikey,
1.736     albertel 12920: 					  $depthcount+1);
                   12921: 			    foreach my $meta (split(',',$metadata)) {
                   12922: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
                   12923: 				$metathesekeys{$meta}=1;
1.339     albertel 12924: 			    }
1.1068    www      12925:                         }
1.1067    www      12926: 		    } else {
                   12927: #
                   12928: # Not importing, some other kind of non-package, non-library start tag
                   12929: # 
                   12930:                         $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   12931:                         if (defined($token->[2]->{'id'})) {
                   12932:                             $unikey.='_'.$token->[2]->{'id'};
                   12933:                         }
1.339     albertel 12934: 			if (defined($token->[2]->{'name'})) { 
                   12935: 			    $unikey.='_'.$token->[2]->{'name'}; 
                   12936: 			}
                   12937: 			$metathesekeys{$unikey}=1;
1.736     albertel 12938: 			foreach my $param (@{$token->[3]}) {
                   12939: 			    $metaentry{':'.$unikey.'.'.$param} =
                   12940: 				$token->[2]->{$param};
1.339     albertel 12941: 			}
                   12942: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599     albertel 12943: 			my $default=$metaentry{':'.$unikey.'.default'};
1.339     albertel 12944: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
                   12945: 		 # only ws inside the tag, and not in default, so use default
                   12946: 		 # as value
1.599     albertel 12947: 			    $metaentry{':'.$unikey}=$default;
1.908     albertel 12948: 			} elsif ( $internaltext =~ /\S/ ) {
                   12949: 		  # something interesting inside the tag
                   12950: 			    $metaentry{':'.$unikey}=$internaltext;
1.339     albertel 12951: 			} else {
1.908     albertel 12952: 		  # no interesting values, don't set a default
1.339     albertel 12953: 			}
1.172     www      12954: # end of not-a-package not-a-library import
1.339     albertel 12955: 		    }
1.172     www      12956: # end of not-a-package start tag
1.339     albertel 12957: 		}
1.172     www      12958: # the next is the end of "start tag"
1.339     albertel 12959: 	    }
                   12960: 	}
1.483     albertel 12961: 	my ($extension) = ($uri =~ /\.(\w+)$/);
1.883     albertel 12962: 	$extension = lc($extension);
                   12963: 	if ($extension eq 'htm') { $extension='html'; }
                   12964: 
1.737     albertel 12965: 	foreach my $key (keys(%packagetab)) {
1.483     albertel 12966: 	    #no specific packages #how's our extension
                   12967: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488     albertel 12968: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483     albertel 12969: 					 \%metathesekeys);
                   12970: 	}
1.883     albertel 12971: 
                   12972: 	if (!exists($metaentry{':packages'})
                   12973: 	    || $packagetab{"import_defaults&extension_$extension"}) {
1.737     albertel 12974: 	    foreach my $key (keys(%packagetab)) {
1.483     albertel 12975: 		#no specific packages well let's get default then
                   12976: 		if ($key!~/^default&/) { next; }
1.488     albertel 12977: 		&metadata_create_package_def($uri,$key,'default',
1.483     albertel 12978: 					     \%metathesekeys);
                   12979: 	    }
                   12980: 	}
1.338     www      12981: # are there custom rights to evaluate
1.599     albertel 12982: 	if ($metaentry{':copyright'} eq 'custom') {
1.339     albertel 12983: 
1.338     www      12984:     #
                   12985:     # Importing a rights file here
1.339     albertel 12986:     #
                   12987: 	    unless ($depthcount) {
1.599     albertel 12988: 		my $location=$metaentry{':customdistributionfile'};
1.339     albertel 12989: 		my $dir=$filename;
                   12990: 		$dir=~s|[^/]*$||;
                   12991: 		$location=&filelocation($dir,$location);
1.736     albertel 12992: 		my $rights_metadata =
1.1362    raeburn  12993: 		    &metadata($uri,'keys',$toolsymb,$location,'_rights',
1.736     albertel 12994: 			      $depthcount+1);
                   12995: 		foreach my $rights (split(',',$rights_metadata)) {
                   12996: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
                   12997: 		    $metathesekeys{$rights}=1;
1.339     albertel 12998: 		}
                   12999: 	    }
                   13000: 	}
1.737     albertel 13001: 	# uniqifiy package listing
                   13002: 	my %seen;
                   13003: 	my @uniq_packages =
                   13004: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
                   13005: 	$metaentry{':packages'} = join(',',@uniq_packages);
                   13006: 
1.1369    raeburn  13007:         if (($importedresponses) || ($importedparts)) {
                   13008:             if ($importedparts) {
1.1070    www      13009: # We had imported parts and need to rebuild partorder
1.1369    raeburn  13010:                 $metaentry{':partorder'}='';
                   13011:                 $metathesekeys{'partorder'}=1;
                   13012:             }
                   13013:             if ($importedresponses) {
                   13014: # We had imported responses and need to rebuil responseorder
                   13015:                 $metaentry{':responseorder'}='';
                   13016:                 $metathesekeys{'responseorder'}=1;
                   13017:             }
                   13018:             for (my $index=0;$index<$#origfiletagids;$index+=2) {
                   13019:                 my $origid = $origfiletagids[$index+1];
                   13020:                 if ($origfiletagids[$index] eq 'part') {
                   13021: # Original part, part of the problem
                   13022:                     if ($importedparts) {
                   13023:                         $metaentry{':partorder'}.=','.$origid;
                   13024:                     }
                   13025:                 } elsif ($origfiletagids[$index] eq 'import') {
                   13026:                     if ($importedparts) {
                   13027: # We have imported parts at this position
1.1373    raeburn  13028:                         if ($importedpartids{$origid} ne '') {
                   13029:                             $metaentry{':partorder'}.=','.$importedpartids{$origid};
                   13030:                         }
1.1369    raeburn  13031:                     }
                   13032:                     if ($importedresponses) {
                   13033: # We have imported responses at this position
1.1373    raeburn  13034:                         if ($importedrespids{$origid} ne '') {
                   13035:                             $metaentry{':responseorder'}.=','.$importedrespids{$origid};
1.1369    raeburn  13036:                         }
                   13037:                     }
                   13038:                 } else {
                   13039: # Original response item, part of the problem
                   13040:                     if ($importedresponses) {
                   13041:                         $metaentry{':responseorder'}.=','.$origid;
                   13042:                     }
                   13043:                 }
                   13044:             }
                   13045:             if ($importedparts) {
                   13046:                 $metaentry{':partorder'}=~s/^\,//;
                   13047:             }
                   13048:             if ($importedresponses) {
                   13049:                 $metaentry{':responseorder'}=~s/^\,//;
                   13050:             }
1.1070    www      13051:         }
1.737     albertel 13052: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599     albertel 13053: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
1.1274    raeburn  13054: 	$metaentry{':allpossiblekeys'}=join(',',keys(%metathesekeys));
1.1371    raeburn  13055:         unless ($liburi) {
                   13056: 	    &do_cache_new('meta',$uri,\%metaentry,$cachetime);
                   13057:         }
1.177     www      13058: # this is the end of "was not already recently cached
1.71      www      13059:     }
1.599     albertel 13060:     return $metaentry{':'.$what};
1.261     albertel 13061: }
                   13062: 
1.488     albertel 13063: sub metadata_create_package_def {
1.483     albertel 13064:     my ($uri,$key,$package,$metathesekeys)=@_;
                   13065:     my ($pack,$name,$subp)=split(/\&/,$key);
                   13066:     if ($subp eq 'default') { next; }
                   13067:     
1.599     albertel 13068:     if (defined($metaentry{':packages'})) {
                   13069: 	$metaentry{':packages'}.=','.$package;
1.483     albertel 13070:     } else {
1.599     albertel 13071: 	$metaentry{':packages'}=$package;
1.483     albertel 13072:     }
                   13073:     my $value=$packagetab{$key};
                   13074:     my $unikey;
                   13075:     $unikey='parameter_0_'.$name;
1.599     albertel 13076:     $metaentry{':'.$unikey.'.part'}=0;
1.483     albertel 13077:     $$metathesekeys{$unikey}=1;
1.599     albertel 13078:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   13079: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
1.483     albertel 13080:     }
1.599     albertel 13081:     if (defined($metaentry{':'.$unikey.'.default'})) {
                   13082: 	$metaentry{':'.$unikey}=
                   13083: 	    $metaentry{':'.$unikey.'.default'};
1.483     albertel 13084:     }
                   13085: }
                   13086: 
1.261     albertel 13087: sub metadata_generate_part0 {
                   13088:     my ($metadata,$metacache,$uri) = @_;
                   13089:     my %allnames;
1.737     albertel 13090:     foreach my $metakey (keys(%$metadata)) {
1.261     albertel 13091: 	if ($metakey=~/^parameter\_(.*)/) {
1.428     albertel 13092: 	  my $part=$$metacache{':'.$metakey.'.part'};
                   13093: 	  my $name=$$metacache{':'.$metakey.'.name'};
1.356     albertel 13094: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261     albertel 13095: 	    $allnames{$name}=$part;
                   13096: 	  }
                   13097: 	}
                   13098:     }
                   13099:     foreach my $name (keys(%allnames)) {
                   13100:       $$metadata{"parameter_0_$name"}=1;
1.428     albertel 13101:       my $key=":parameter_0_$name";
1.261     albertel 13102:       $$metacache{"$key.part"}='0';
                   13103:       $$metacache{"$key.name"}=$name;
1.428     albertel 13104:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261     albertel 13105: 					   $allnames{$name}.'_'.$name.
                   13106: 					   '.type'};
1.428     albertel 13107:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261     albertel 13108: 			     '.display'};
1.644     www      13109:       my $expr='[Part: '.$allnames{$name}.']';
1.479     albertel 13110:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261     albertel 13111:       $$metacache{"$key.display"}=$olddis;
                   13112:     }
1.71      www      13113: }
                   13114: 
1.764     albertel 13115: # ------------------------------------------------------ Devalidate title cache
                   13116: 
                   13117: sub devalidate_title_cache {
                   13118:     my ($url)=@_;
                   13119:     if (!$env{'request.course.id'}) { return; }
                   13120:     my $symb=&symbread($url);
                   13121:     if (!$symb) { return; }
                   13122:     my $key=$env{'request.course.id'}."\0".$symb;
                   13123:     &devalidate_cache_new('title',$key);
                   13124: }
                   13125: 
1.1014    droeschl 13126: # ------------------------------------------------- Get the title of a course
                   13127: 
                   13128: sub current_course_title {
                   13129:     return $env{ 'course.' . $env{'request.course.id'} . '.description' };
                   13130: }
1.301     www      13131: # ------------------------------------------------- Get the title of a resource
                   13132: 
                   13133: sub gettitle {
                   13134:     my $urlsymb=shift;
                   13135:     my $symb=&symbread($urlsymb);
1.534     albertel 13136:     if ($symb) {
1.620     albertel 13137: 	my $key=$env{'request.course.id'}."\0".$symb;
1.599     albertel 13138: 	my ($result,$cached)=&is_cached_new('title',$key);
1.575     albertel 13139: 	if (defined($cached)) { 
                   13140: 	    return $result;
                   13141: 	}
1.534     albertel 13142: 	my ($map,$resid,$url)=&decode_symb($symb);
                   13143: 	my $title='';
1.907     albertel 13144: 	if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
                   13145: 	    $title = $env{'course.'.$env{'request.course.id'}.'.description'};
                   13146: 	} else {
                   13147: 	    if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
                   13148: 		    &GDBM_READER(),0640)) {
                   13149: 		my $mapid=$bighash{'map_pc_'.&clutter($map)};
                   13150: 		$title=$bighash{'title_'.$mapid.'.'.$resid};
                   13151: 		untie(%bighash);
                   13152: 	    }
1.534     albertel 13153: 	}
                   13154: 	$title=~s/\&colon\;/\:/gs;
                   13155: 	if ($title) {
1.1159    www      13156: # Remember both $symb and $title for dynamic metadata
                   13157:             $accesshash{$symb.'___crstitle'}=$title;
1.1161    www      13158:             $accesshash{&declutter($map).'___'.&declutter($url).'___usage'}=time;
1.1159    www      13159: # Cache this title and then return it
1.599     albertel 13160: 	    return &do_cache_new('title',$key,$title,600);
1.534     albertel 13161: 	}
                   13162: 	$urlsymb=$url;
                   13163:     }
                   13164:     my $title=&metadata($urlsymb,'title');
                   13165:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
                   13166:     return $title;
1.301     www      13167: }
1.613     albertel 13168: 
1.614     albertel 13169: sub get_slot {
                   13170:     my ($which,$cnum,$cdom)=@_;
                   13171:     if (!$cnum || !$cdom) {
1.790     albertel 13172: 	(undef,my $courseid)=&whichuser();
1.620     albertel 13173: 	$cdom=$env{'course.'.$courseid.'.domain'};
                   13174: 	$cnum=$env{'course.'.$courseid.'.num'};
1.614     albertel 13175:     }
1.703     albertel 13176:     my $key=join("\0",'slots',$cdom,$cnum,$which);
                   13177:     my %slotinfo;
                   13178:     if (exists($remembered{$key})) {
                   13179: 	$slotinfo{$which} = $remembered{$key};
                   13180:     } else {
                   13181: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
                   13182: 	&Apache::lonhomework::showhash(%slotinfo);
                   13183: 	my ($tmp)=keys(%slotinfo);
                   13184: 	if ($tmp=~/^error:/) { return (); }
                   13185: 	$remembered{$key} = $slotinfo{$which};
                   13186:     }
1.616     albertel 13187:     if (ref($slotinfo{$which}) eq 'HASH') {
                   13188: 	return %{$slotinfo{$which}};
                   13189:     }
                   13190:     return $slotinfo{$which};
1.614     albertel 13191: }
1.1150    raeburn  13192: 
                   13193: sub get_reservable_slots {
                   13194:     my ($cnum,$cdom,$uname,$udom) = @_;
                   13195:     my $now = time;
                   13196:     my $reservable_info;
                   13197:     my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
                   13198:     if (exists($remembered{$key})) {
                   13199:         $reservable_info = $remembered{$key};
                   13200:     } else {
                   13201:         my %resv;
                   13202:         ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
                   13203:         &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
                   13204:         $reservable_info = \%resv;
                   13205:         $remembered{$key} = $reservable_info;
                   13206:     }
                   13207:     return $reservable_info;
                   13208: }
                   13209: 
                   13210: sub get_course_slots {
                   13211:     my ($cnum,$cdom) = @_;
                   13212:     my $hashid=$cnum.':'.$cdom;
                   13213:     my ($result,$cached) = &Apache::lonnet::is_cached_new('allslots',$hashid);
                   13214:     if (defined($cached)) {
                   13215:         if (ref($result) eq 'HASH') {
                   13216:             return %{$result};
                   13217:         }
                   13218:     } else {
                   13219:         my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
                   13220:         my ($tmp) = keys(%slots);
                   13221:         if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.1219    raeburn  13222:             &do_cache_new('allslots',$hashid,\%slots,600);
1.1150    raeburn  13223:             return %slots;
                   13224:         }
                   13225:     }
                   13226:     return;
                   13227: }
                   13228: 
                   13229: sub devalidate_slots_cache {
                   13230:     my ($cnum,$cdom)=@_;
                   13231:     my $hashid=$cnum.':'.$cdom;
                   13232:     &devalidate_cache_new('allslots',$hashid);
                   13233: }
                   13234: 
1.1181    raeburn  13235: sub get_coursechange {
                   13236:     my ($cdom,$cnum) = @_;
                   13237:     if ($cdom eq '' || $cnum eq '') {
                   13238:         return unless ($env{'request.course.id'});
                   13239:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   13240:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   13241:     }
                   13242:     my $hashid=$cdom.'_'.$cnum;
                   13243:     my ($change,$cached)=&is_cached_new('crschange',$hashid);
                   13244:     if ((defined($cached)) && ($change ne '')) {
                   13245:         return $change;
                   13246:     } else {
                   13247:         my %crshash;
                   13248:         %crshash = &get('environment',['internal.contentchange'],$cdom,$cnum);
                   13249:         if ($crshash{'internal.contentchange'} eq '') {
                   13250:             $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
                   13251:             if ($change eq '') {
                   13252:                 %crshash = &get('environment',['internal.created'],$cdom,$cnum);
                   13253:                 $change = $crshash{'internal.created'};
                   13254:             }
                   13255:         } else {
                   13256:             $change = $crshash{'internal.contentchange'};
                   13257:         }
                   13258:         my $cachetime = 600;
                   13259:         &do_cache_new('crschange',$hashid,$change,$cachetime);
                   13260:     }
                   13261:     return $change;
                   13262: }
                   13263: 
                   13264: sub devalidate_coursechange_cache {
                   13265:     my ($cnum,$cdom)=@_;
                   13266:     my $hashid=$cnum.':'.$cdom;
                   13267:     &devalidate_cache_new('crschange',$hashid);
                   13268: }
                   13269: 
1.31      www      13270: # ------------------------------------------------- Update symbolic store links
                   13271: 
                   13272: sub symblist {
                   13273:     my ($mapname,%newhash)=@_;
1.438     www      13274:     $mapname=&deversion(&declutter($mapname));
1.31      www      13275:     my %hash;
1.620     albertel 13276:     if (($env{'request.course.fn'}) && (%newhash)) {
                   13277:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256     albertel 13278:                       &GDBM_WRCREAT(),0640)) {
1.1000    raeburn  13279: 	    foreach my $url (keys(%newhash)) {
1.711     albertel 13280: 		next if ($url eq 'last_known'
                   13281: 			 && $env{'form.no_update_last_known'});
                   13282: 		$hash{declutter($url)}=&encode_symb($mapname,
                   13283: 						    $newhash{$url}->[1],
                   13284: 						    $newhash{$url}->[0]);
1.191     harris41 13285:             }
1.31      www      13286:             if (untie(%hash)) {
                   13287: 		return 'ok';
                   13288:             }
                   13289:         }
                   13290:     }
                   13291:     return 'error';
1.212     www      13292: }
                   13293: 
                   13294: # --------------------------------------------------------------- Verify a symb
                   13295: 
                   13296: sub symbverify {
1.1190    raeburn  13297:     my ($symb,$thisurl,$encstate)=@_;
1.510     www      13298:     my $thisfn=$thisurl;
1.439     www      13299:     $thisfn=&declutter($thisfn);
1.215     www      13300: # direct jump to resource in page or to a sequence - will construct own symbs
                   13301:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
                   13302: # check URL part
1.409     www      13303:     my ($map,$resid,$url)=&decode_symb($symb);
1.439     www      13304: 
1.431     www      13305:     unless ($url eq $thisfn) { return 0; }
1.213     www      13306: 
1.216     www      13307:     $symb=&symbclean($symb);
1.510     www      13308:     $thisurl=&deversion($thisurl);
1.439     www      13309:     $thisfn=&deversion($thisfn);
1.213     www      13310: 
                   13311:     my %bighash;
                   13312:     my $okay=0;
1.431     www      13313: 
1.620     albertel 13314:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 13315:                             &GDBM_READER(),0640)) {
1.1032    raeburn  13316:         if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
                   13317:             $thisurl =~ s/\?.+$//;
1.1204    raeburn  13318:             if ($map =~ m{^uploaded/.+\.page$}) {
                   13319:                 $thisurl =~ s{^(/adm/wrapper|)/ext/}{http://};
                   13320:                 $thisurl =~ s{^\Qhttp://https://\E}{https://};
                   13321:             }
                   13322:         }
                   13323:         my $ids;
1.1419    raeburn  13324:         if ($map =~ m{^uploaded/.+\.page$}) {
                   13325:             $ids=$bighash{'ids_'.&clutter_with_no_wrapper($thisurl)};
1.1204    raeburn  13326:         } else {
                   13327:             $ids=$bighash{'ids_'.&clutter($thisurl)};
1.1032    raeburn  13328:         }
1.1102    raeburn  13329:         unless ($ids) {
                   13330:             my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;  
                   13331:             $ids=$bighash{$idkey};
1.216     www      13332:         }
                   13333:         if ($ids) {
                   13334: # ------------------------------------------------------------------- Has ID(s)
1.1202    raeburn  13335:             if ($thisfn =~ m{^/adm/wrapper/ext/}) {
1.1203    raeburn  13336:                 $symb =~ s/\?.+$//;
1.1202    raeburn  13337:             }
1.800     albertel 13338: 	    foreach my $id (split(/\,/,$ids)) {
                   13339: 	       my ($mapid,$resid)=split(/\./,$id);
1.216     www      13340:                if (
                   13341:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
1.1190    raeburn  13342:    eq $symb) {
                   13343:                    if (ref($encstate)) {
                   13344:                        $$encstate = $bighash{'encrypted_'.$id};
                   13345:                    }
1.620     albertel 13346: 		   if (($env{'request.role.adv'}) ||
1.1101    raeburn  13347: 		       ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
                   13348:                        ($thisurl eq '/adm/navmaps')) {
1.1190    raeburn  13349: 		       $okay=1;
1.1202    raeburn  13350:                        last;
1.582     albertel 13351: 		   }
                   13352: 	       }
1.216     www      13353: 	   }
                   13354:         }
1.213     www      13355: 	untie(%bighash);
                   13356:     }
                   13357:     return $okay;
1.31      www      13358: }
                   13359: 
1.210     www      13360: # --------------------------------------------------------------- Clean-up symb
                   13361: 
                   13362: sub symbclean {
                   13363:     my $symb=shift;
1.568     albertel 13364:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210     www      13365: # remove version from map
                   13366:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215     www      13367: 
1.210     www      13368: # remove version from URL
                   13369:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213     www      13370: 
1.507     www      13371: # remove wrapper
                   13372: 
1.510     www      13373:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694     albertel 13374:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210     www      13375:     return $symb;
1.409     www      13376: }
                   13377: 
                   13378: # ---------------------------------------------- Split symb to find map and url
1.429     albertel 13379: 
                   13380: sub encode_symb {
                   13381:     my ($map,$resid,$url)=@_;
                   13382:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
                   13383: }
1.409     www      13384: 
                   13385: sub decode_symb {
1.568     albertel 13386:     my $symb=shift;
                   13387:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
                   13388:     my ($map,$resid,$url)=split(/___/,$symb);
1.413     www      13389:     return (&fixversion($map),$resid,&fixversion($url));
                   13390: }
                   13391: 
                   13392: sub fixversion {
                   13393:     my $fn=shift;
1.609     banghart 13394:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435     www      13395:     my %bighash;
                   13396:     my $uri=&clutter($fn);
1.620     albertel 13397:     my $key=$env{'request.course.id'}.'_'.$uri;
1.440     www      13398: # is this cached?
1.599     albertel 13399:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440     www      13400:     if (defined($cached)) { return $result; }
                   13401: # unfortunately not cached, or expired
1.620     albertel 13402:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440     www      13403: 	    &GDBM_READER(),0640)) {
                   13404:  	if ($bighash{'version_'.$uri}) {
                   13405:  	    my $version=$bighash{'version_'.$uri};
1.444     www      13406:  	    unless (($version eq 'mostrecent') || 
                   13407: 		    ($version==&getversion($uri))) {
1.440     www      13408:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
                   13409:  	    }
                   13410:  	}
                   13411:  	untie %bighash;
1.413     www      13412:     }
1.599     albertel 13413:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438     www      13414: }
                   13415: 
                   13416: sub deversion {
                   13417:     my $url=shift;
                   13418:     $url=~s/\.\d+\.(\w+)$/\.$1/;
                   13419:     return $url;
1.210     www      13420: }
                   13421: 
1.31      www      13422: # ------------------------------------------------------ Return symb list entry
                   13423: 
                   13424: sub symbread {
1.1424    raeburn  13425:     my ($thisfn,$donotrecurse,$ignorecachednull,$checkforblock,$possibles,
1.1427    raeburn  13426:         $ignoresymbdb,$noenccheck)=@_;
1.1265    raeburn  13427:     my $cache_str='request.symbread.cached.'.$thisfn;
1.1427    raeburn  13428:     if (defined($env{$cache_str})) {
1.1424    raeburn  13429:         unless (ref($possibles) eq 'HASH') {
                   13430:             if ($ignorecachednull) {
                   13431:                 return $env{$cache_str} unless ($env{$cache_str} eq '');
                   13432:             } else {
                   13433:                 return $env{$cache_str};
                   13434:             }
1.1282    raeburn  13435:         }
                   13436:     }
1.242     www      13437: # no filename provided? try from environment
1.1265    raeburn  13438:     unless ($thisfn) {
1.620     albertel 13439:         if ($env{'request.symb'}) {
1.1427    raeburn  13440:             return $env{$cache_str}=&symbclean($env{'request.symb'});
1.539     albertel 13441: 	}
1.620     albertel 13442: 	$thisfn=$env{'request.filename'};
1.44      www      13443:     }
1.569     albertel 13444:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242     www      13445: # is that filename actually a symb? Verify, clean, and return
                   13446:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539     albertel 13447: 	if (&symbverify($thisfn,$1)) {
1.1427    raeburn  13448: 	    return $env{$cache_str}=&symbclean($thisfn);
1.539     albertel 13449: 	}
1.242     www      13450:     }
1.44      www      13451:     $thisfn=declutter($thisfn);
1.31      www      13452:     my %hash;
1.37      www      13453:     my %bighash;
                   13454:     my $syval='';
1.620     albertel 13455:     if (($env{'request.course.fn'}) && ($thisfn)) {
1.481     raeburn  13456:         my $targetfn = $thisfn;
1.609     banghart 13457:         if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
1.481     raeburn  13458:             $targetfn = 'adm/wrapper/'.$thisfn;
                   13459:         }
1.687     albertel 13460: 	if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
                   13461: 	    $targetfn=$1;
                   13462: 	}
1.1427    raeburn  13463:         unless ($ignoresymbdb) {
1.1424    raeburn  13464:             if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
                   13465:                           &GDBM_READER(),0640)) {
                   13466: 	        $syval=$hash{$targetfn};
                   13467:                 untie(%hash);
                   13468:             }
1.1427    raeburn  13469:             if ($syval && $checkforblock) {
                   13470:                 my @blockers = &has_comm_blocking('bre',$syval,$thisfn,$ignoresymbdb,$noenccheck);
1.1424    raeburn  13471:                 if (@blockers) {
                   13472:                     $syval='';
                   13473:                 }
                   13474:             }
1.37      www      13475:         }
                   13476: # ---------------------------------------------------------- There was an entry
                   13477:         if ($syval) {
1.601     albertel 13478: 	    #unless ($syval=~/\_\d+$/) {
1.620     albertel 13479: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.949     raeburn  13480: 		    #&appenv({'request.ambiguous' => $thisfn});
1.620     albertel 13481: 		    #return $env{$cache_str}='';
1.601     albertel 13482: 		#}    
                   13483: 		#$syval.=$1;
                   13484: 	    #}
1.37      www      13485:         } else {
                   13486: # ------------------------------------------------------- Was not in symb table
1.620     albertel 13487:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 13488:                             &GDBM_READER(),0640)) {
1.37      www      13489: # ---------------------------------------------- Get ID(s) for current resource
1.280     www      13490:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65      www      13491:               unless ($ids) { 
                   13492:                  $ids=$bighash{'ids_/'.$thisfn};
1.242     www      13493:               }
                   13494:               unless ($ids) {
                   13495: # alias?
                   13496: 		  $ids=$bighash{'mapalias_'.$thisfn};
1.65      www      13497:               }
1.37      www      13498:               if ($ids) {
                   13499: # ------------------------------------------------------------------- Has ID(s)
                   13500:                  my @possibilities=split(/\,/,$ids);
1.39      www      13501:                  if ($#possibilities==0) {
                   13502: # ----------------------------------------------- There is only one possibility
1.37      www      13503: 		     my ($mapid,$resid)=split(/\./,$ids);
1.626     albertel 13504: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
                   13505: 						    $resid,$thisfn);
1.1282    raeburn  13506:                      if (ref($possibles) eq 'HASH') {
1.1424    raeburn  13507:                          unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
                   13508:                              $possibles->{$syval} = 1;
                   13509:                          }
1.1282    raeburn  13510:                      }
                   13511:                      if ($checkforblock) {
1.1424    raeburn  13512:                          unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
1.1426    raeburn  13513:                              my @blockers = &has_comm_blocking('bre',$syval,$bighash{'src_'.$ids},'',$noenccheck);
1.1424    raeburn  13514:                              if (@blockers) {
                   13515:                                  $syval = '';
1.1425    raeburn  13516:                                  untie(%bighash);
                   13517:                                  return $env{$cache_str}='';
1.1424    raeburn  13518:                              }
1.1282    raeburn  13519:                          }
                   13520:                      }
                   13521:                  } elsif ((!$donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) { 
1.39      www      13522: # ------------------------------------------ There is more than one possibility
                   13523:                      my $realpossible=0;
1.800     albertel 13524:                      foreach my $id (@possibilities) {
                   13525: 			 my $file=$bighash{'src_'.$id};
1.1282    raeburn  13526:                          my $canaccess;
                   13527:                          if (($donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
                   13528:                              $canaccess = 1;
                   13529:                          } else { 
                   13530:                              $canaccess = &allowed('bre',$file);
                   13531:                          }
                   13532:                          if ($canaccess) {
                   13533:          		     my ($mapid,$resid)=split(/\./,$id);
                   13534:                              if ($bighash{'map_type_'.$mapid} ne 'page') {
                   13535:                                  my $poss_syval=&encode_symb($bighash{'map_id_'.$mapid},
                   13536: 						             $resid,$thisfn);
1.1424    raeburn  13537:                                  next if ($bighash{'randomout_'.$id} && !$env{'request.role.adv'});
1.1426    raeburn  13538:                                  next unless (($noenccheck) || ($bighash{'encrypted_'.$id} eq $env{'request.enc'}));
1.1282    raeburn  13539:                                  if ($checkforblock) {
1.1426    raeburn  13540:                                      my @blockers = &has_comm_blocking('bre',$poss_syval,$file,'',$noenccheck);
1.1424    raeburn  13541:                                      if (@blockers > 0) {
                   13542:                                          $syval = '';
                   13543:                                      } else {
1.1282    raeburn  13544:                                          $syval = $poss_syval;
                   13545:                                          $realpossible++;
                   13546:                                      }
                   13547:                                  } else {
                   13548:                                      $syval = $poss_syval;
                   13549:                                      $realpossible++;
                   13550:                                  }
1.1424    raeburn  13551:                                  if ($syval) {
                   13552:                                      if (ref($possibles) eq 'HASH') {
                   13553:                                          $possibles->{$syval} = 1;
                   13554:                                      }
                   13555:                                  }
1.1282    raeburn  13556:                              }
1.39      www      13557: 			 }
1.191     harris41 13558:                      }
1.39      www      13559: 		     if ($realpossible!=1) { $syval=''; }
1.249     www      13560:                  } else {
                   13561:                      $syval='';
1.37      www      13562:                  }
                   13563: 	      }
1.1282    raeburn  13564:               untie(%bighash);
1.481     raeburn  13565:            }
1.31      www      13566:         }
1.62      www      13567:         if ($syval) {
1.1427    raeburn  13568: 	    return $env{$cache_str}=$syval;
1.62      www      13569:         }
1.31      www      13570:     }
1.949     raeburn  13571:     &appenv({'request.ambiguous' => $thisfn});
1.620     albertel 13572:     return $env{$cache_str}='';
1.31      www      13573: }
                   13574: 
                   13575: # ---------------------------------------------------------- Return random seed
                   13576: 
1.32      www      13577: sub numval {
                   13578:     my $txt=shift;
                   13579:     $txt=~tr/A-J/0-9/;
                   13580:     $txt=~tr/a-j/0-9/;
                   13581:     $txt=~tr/K-T/0-9/;
                   13582:     $txt=~tr/k-t/0-9/;
                   13583:     $txt=~tr/U-Z/0-5/;
                   13584:     $txt=~tr/u-z/0-5/;
                   13585:     $txt=~s/\D//g;
1.564     albertel 13586:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32      www      13587:     return int($txt);
1.368     albertel 13588: }
                   13589: 
1.484     albertel 13590: sub numval2 {
                   13591:     my $txt=shift;
                   13592:     $txt=~tr/A-J/0-9/;
                   13593:     $txt=~tr/a-j/0-9/;
                   13594:     $txt=~tr/K-T/0-9/;
                   13595:     $txt=~tr/k-t/0-9/;
                   13596:     $txt=~tr/U-Z/0-5/;
                   13597:     $txt=~tr/u-z/0-5/;
                   13598:     $txt=~s/\D//g;
                   13599:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   13600:     my $total;
                   13601:     foreach my $val (@txts) { $total+=$val; }
1.564     albertel 13602:     if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484     albertel 13603:     return int($total);
                   13604: }
                   13605: 
1.575     albertel 13606: sub numval3 {
                   13607:     use integer;
                   13608:     my $txt=shift;
                   13609:     $txt=~tr/A-J/0-9/;
                   13610:     $txt=~tr/a-j/0-9/;
                   13611:     $txt=~tr/K-T/0-9/;
                   13612:     $txt=~tr/k-t/0-9/;
                   13613:     $txt=~tr/U-Z/0-5/;
                   13614:     $txt=~tr/u-z/0-5/;
                   13615:     $txt=~s/\D//g;
                   13616:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   13617:     my $total;
                   13618:     foreach my $val (@txts) { $total+=$val; }
                   13619:     if ($_64bit) { $total=(($total<<32)>>32); }
                   13620:     return $total;
                   13621: }
                   13622: 
1.675     albertel 13623: sub digest {
                   13624:     my ($data)=@_;
                   13625:     my $digest=&Digest::MD5::md5($data);
                   13626:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
                   13627:     my ($e,$f);
                   13628:     {
                   13629:         use integer;
                   13630:         $e=($a+$b);
                   13631:         $f=($c+$d);
                   13632:         if ($_64bit) {
                   13633:             $e=(($e<<32)>>32);
                   13634:             $f=(($f<<32)>>32);
                   13635:         }
                   13636:     }
                   13637:     if (wantarray) {
                   13638: 	return ($e,$f);
                   13639:     } else {
                   13640: 	my $g;
                   13641: 	{
                   13642: 	    use integer;
                   13643: 	    $g=($e+$f);
                   13644: 	    if ($_64bit) {
                   13645: 		$g=(($g<<32)>>32);
                   13646: 	    }
                   13647: 	}
                   13648: 	return $g;
                   13649:     }
                   13650: }
                   13651: 
1.368     albertel 13652: sub latest_rnd_algorithm_id {
1.675     albertel 13653:     return '64bit5';
1.366     albertel 13654: }
1.32      www      13655: 
1.503     albertel 13656: sub get_rand_alg {
                   13657:     my ($courseid)=@_;
1.790     albertel 13658:     if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503     albertel 13659:     if ($courseid) {
1.620     albertel 13660: 	return $env{"course.$courseid.rndseed"};
1.503     albertel 13661:     }
                   13662:     return &latest_rnd_algorithm_id();
                   13663: }
                   13664: 
1.562     albertel 13665: sub validCODE {
                   13666:     my ($CODE)=@_;
                   13667:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
                   13668:     return 0;
                   13669: }
                   13670: 
1.491     albertel 13671: sub getCODE {
1.620     albertel 13672:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618     albertel 13673:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
                   13674: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
                   13675: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491     albertel 13676: 	return $Apache::lonhomework::history{'resource.CODE'};
                   13677:     }
                   13678:     return undef;
                   13679: }
1.1133    foxr     13680: #
                   13681: #  Determines the random seed for a specific context:
                   13682: #
                   13683: # parameters:
                   13684: #   symb      - in course context the symb for the seed.
                   13685: #   course_id - The course id of the form domain_coursenum.
                   13686: #   domain    - Domain for the user.
                   13687: #   course    - Course for the user.
                   13688: #   cenv      - environment of the course.
                   13689: #
                   13690: # NOTE:
                   13691: #   All parameters are picked out of the environment if missing
                   13692: #   or not defined.
                   13693: #   If a symb cannot be determined the current time is used instead.
                   13694: #
                   13695: #  For a given well defined symb, courside, domain, username,
                   13696: #  and course environment, the seed is reproducible.
                   13697: #
1.31      www      13698: sub rndseed {
1.1133    foxr     13699:     my ($symb,$courseid,$domain,$username, $cenv)=@_;
1.790     albertel 13700:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.896     albertel 13701:     if (!defined($symb)) {
1.366     albertel 13702: 	unless ($symb=$wsymb) { return time; }
                   13703:     }
1.1146    foxr     13704:     if (!defined $courseid) { 
                   13705: 	$courseid=$wcourseid; 
                   13706:     }
                   13707:     if (!defined $domain) { $domain=$wdomain; }
                   13708:     if (!defined $username) { $username=$wusername }
1.1133    foxr     13709: 
                   13710:     my $which;
                   13711:     if (defined($cenv->{'rndseed'})) {
                   13712: 	$which = $cenv->{'rndseed'};
                   13713:     } else {
                   13714: 	$which =&get_rand_alg($courseid);
                   13715:     }
1.491     albertel 13716:     if (defined(&getCODE())) {
1.1133    foxr     13717: 
1.675     albertel 13718: 	if ($which eq '64bit5') {
                   13719: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
                   13720: 	} elsif ($which eq '64bit4') {
1.575     albertel 13721: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
                   13722: 	} else {
                   13723: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
                   13724: 	}
1.675     albertel 13725:     } elsif ($which eq '64bit5') {
                   13726: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575     albertel 13727:     } elsif ($which eq '64bit4') {
                   13728: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501     albertel 13729:     } elsif ($which eq '64bit3') {
                   13730: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443     albertel 13731:     } elsif ($which eq '64bit2') {
                   13732: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366     albertel 13733:     } elsif ($which eq '64bit') {
                   13734: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
                   13735:     }
                   13736:     return &rndseed_32bit($symb,$courseid,$domain,$username);
                   13737: }
                   13738: 
                   13739: sub rndseed_32bit {
                   13740:     my ($symb,$courseid,$domain,$username)=@_;
                   13741:     {
                   13742: 	use integer;
                   13743: 	my $symbchck=unpack("%32C*",$symb) << 27;
                   13744: 	my $symbseed=numval($symb) << 22;
                   13745: 	my $namechck=unpack("%32C*",$username) << 17;
                   13746: 	my $nameseed=numval($username) << 12;
                   13747: 	my $domainseed=unpack("%32C*",$domain) << 7;
                   13748: 	my $courseseed=unpack("%32C*",$courseid);
                   13749: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790     albertel 13750: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   13751: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 13752: 	if ($_64bit) { $num=(($num<<32)>>32); }
1.366     albertel 13753: 	return $num;
                   13754:     }
                   13755: }
                   13756: 
                   13757: sub rndseed_64bit {
                   13758:     my ($symb,$courseid,$domain,$username)=@_;
                   13759:     {
                   13760: 	use integer;
                   13761: 	my $symbchck=unpack("%32S*",$symb) << 21;
                   13762: 	my $symbseed=numval($symb) << 10;
                   13763: 	my $namechck=unpack("%32S*",$username);
                   13764: 	
                   13765: 	my $nameseed=numval($username) << 21;
                   13766: 	my $domainseed=unpack("%32S*",$domain) << 10;
                   13767: 	my $courseseed=unpack("%32S*",$courseid);
                   13768: 	
                   13769: 	my $num1=$symbchck+$symbseed+$namechck;
                   13770: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 13771: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   13772: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 13773: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366     albertel 13774: 	return "$num1,$num2";
1.155     albertel 13775:     }
1.366     albertel 13776: }
                   13777: 
1.443     albertel 13778: sub rndseed_64bit2 {
                   13779:     my ($symb,$courseid,$domain,$username)=@_;
                   13780:     {
                   13781: 	use integer;
                   13782: 	# strings need to be an even # of cahracters long, it it is odd the
                   13783:         # last characters gets thrown away
                   13784: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   13785: 	my $symbseed=numval($symb) << 10;
                   13786: 	my $namechck=unpack("%32S*",$username.' ');
                   13787: 	
                   13788: 	my $nameseed=numval($username) << 21;
1.501     albertel 13789: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   13790: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   13791: 	
                   13792: 	my $num1=$symbchck+$symbseed+$namechck;
                   13793: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 13794: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   13795: 	#&logthis("rndseed :$num:$symb");
1.803     albertel 13796: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501     albertel 13797: 	return "$num1,$num2";
                   13798:     }
                   13799: }
                   13800: 
                   13801: sub rndseed_64bit3 {
                   13802:     my ($symb,$courseid,$domain,$username)=@_;
                   13803:     {
                   13804: 	use integer;
                   13805: 	# strings need to be an even # of cahracters long, it it is odd the
                   13806:         # last characters gets thrown away
                   13807: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   13808: 	my $symbseed=numval2($symb) << 10;
                   13809: 	my $namechck=unpack("%32S*",$username.' ');
                   13810: 	
                   13811: 	my $nameseed=numval2($username) << 21;
1.443     albertel 13812: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   13813: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   13814: 	
                   13815: 	my $num1=$symbchck+$symbseed+$namechck;
                   13816: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 13817: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   13818: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.564     albertel 13819: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110    www      13820: 	
1.503     albertel 13821: 	return "$num1:$num2";
1.443     albertel 13822:     }
                   13823: }
                   13824: 
1.575     albertel 13825: sub rndseed_64bit4 {
                   13826:     my ($symb,$courseid,$domain,$username)=@_;
                   13827:     {
                   13828: 	use integer;
                   13829: 	# strings need to be an even # of cahracters long, it it is odd the
                   13830:         # last characters gets thrown away
                   13831: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   13832: 	my $symbseed=numval3($symb) << 10;
                   13833: 	my $namechck=unpack("%32S*",$username.' ');
                   13834: 	
                   13835: 	my $nameseed=numval3($username) << 21;
                   13836: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   13837: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   13838: 	
                   13839: 	my $num1=$symbchck+$symbseed+$namechck;
                   13840: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 13841: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   13842: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.575     albertel 13843: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110    www      13844: 	
1.575     albertel 13845: 	return "$num1:$num2";
                   13846:     }
                   13847: }
                   13848: 
1.675     albertel 13849: sub rndseed_64bit5 {
                   13850:     my ($symb,$courseid,$domain,$username)=@_;
                   13851:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
                   13852:     return "$num1:$num2";
                   13853: }
                   13854: 
1.366     albertel 13855: sub rndseed_CODE_64bit {
                   13856:     my ($symb,$courseid,$domain,$username)=@_;
1.155     albertel 13857:     {
1.366     albertel 13858: 	use integer;
1.443     albertel 13859: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484     albertel 13860: 	my $symbseed=numval2($symb);
1.491     albertel 13861: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   13862: 	my $CODEseed=numval(&getCODE());
1.443     albertel 13863: 	my $courseseed=unpack("%32S*",$courseid.' ');
1.484     albertel 13864: 	my $num1=$symbseed+$CODEchck;
                   13865: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 13866: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   13867: 	#&logthis("rndseed :$num1:$num2:$symb");
1.564     albertel 13868: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   13869: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503     albertel 13870: 	return "$num1:$num2";
1.366     albertel 13871:     }
                   13872: }
                   13873: 
1.575     albertel 13874: sub rndseed_CODE_64bit4 {
                   13875:     my ($symb,$courseid,$domain,$username)=@_;
                   13876:     {
                   13877: 	use integer;
                   13878: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
                   13879: 	my $symbseed=numval3($symb);
                   13880: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   13881: 	my $CODEseed=numval3(&getCODE());
                   13882: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   13883: 	my $num1=$symbseed+$CODEchck;
                   13884: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 13885: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   13886: 	#&logthis("rndseed :$num1:$num2:$symb");
1.575     albertel 13887: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   13888: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
                   13889: 	return "$num1:$num2";
                   13890:     }
                   13891: }
                   13892: 
1.675     albertel 13893: sub rndseed_CODE_64bit5 {
                   13894:     my ($symb,$courseid,$domain,$username)=@_;
                   13895:     my $code = &getCODE();
                   13896:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
                   13897:     return "$num1:$num2";
                   13898: }
                   13899: 
1.366     albertel 13900: sub setup_random_from_rndseed {
                   13901:     my ($rndseed)=@_;
1.503     albertel 13902:     if ($rndseed =~/([,:])/) {
1.1262    raeburn  13903:         my ($num1,$num2) = map { abs($_); } (split(/[,:]/,$rndseed));
                   13904:         if ((!$num1) || (!$num2) || ($num1 > 2147483562) || ($num2 > 2147483398)) {
                   13905:             &Math::Random::random_set_seed_from_phrase($rndseed);
                   13906:         } else {
                   13907:             &Math::Random::random_set_seed($num1,$num2);
                   13908:         }
1.366     albertel 13909:     } else {
                   13910: 	&Math::Random::random_set_seed_from_phrase($rndseed);
1.98      albertel 13911:     }
1.36      albertel 13912: }
                   13913: 
1.474     albertel 13914: sub latest_receipt_algorithm_id {
1.835     albertel 13915:     return 'receipt3';
1.474     albertel 13916: }
                   13917: 
1.480     www      13918: sub recunique {
                   13919:     my $fucourseid=shift;
                   13920:     my $unique;
1.835     albertel 13921:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
                   13922: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 13923: 	$unique=$env{"course.$fucourseid.internal.encseed"};
1.480     www      13924:     } else {
                   13925: 	$unique=$perlvar{'lonReceipt'};
                   13926:     }
                   13927:     return unpack("%32C*",$unique);
                   13928: }
                   13929: 
                   13930: sub recprefix {
                   13931:     my $fucourseid=shift;
                   13932:     my $prefix;
1.835     albertel 13933:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
                   13934: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 13935: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
1.480     www      13936:     } else {
                   13937: 	$prefix=$perlvar{'lonHostID'};
                   13938:     }
                   13939:     return unpack("%32C*",$prefix);
                   13940: }
                   13941: 
1.76      www      13942: sub ireceipt {
1.474     albertel 13943:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835     albertel 13944: 
                   13945:     my $return =&recprefix($fucourseid).'-';
                   13946: 
                   13947:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
                   13948: 	$env{'request.state'} eq 'construct') {
                   13949: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
                   13950: 	return $return;
                   13951:     }
                   13952: 
1.76      www      13953:     my $cuname=unpack("%32C*",$funame);
                   13954:     my $cudom=unpack("%32C*",$fudom);
                   13955:     my $cucourseid=unpack("%32C*",$fucourseid);
                   13956:     my $cusymb=unpack("%32C*",$fusymb);
1.480     www      13957:     my $cunique=&recunique($fucourseid);
1.474     albertel 13958:     my $cpart=unpack("%32S*",$part);
1.835     albertel 13959:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
                   13960: 
1.790     albertel 13961: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474     albertel 13962: 			       
                   13963: 	$return.= ($cunique%$cuname+
                   13964: 		   $cunique%$cudom+
                   13965: 		   $cusymb%$cuname+
                   13966: 		   $cusymb%$cudom+
                   13967: 		   $cucourseid%$cuname+
                   13968: 		   $cucourseid%$cudom+
                   13969: 		   $cpart%$cuname+
                   13970: 		   $cpart%$cudom);
                   13971:     } else {
                   13972: 	$return.= ($cunique%$cuname+
                   13973: 		   $cunique%$cudom+
                   13974: 		   $cusymb%$cuname+
                   13975: 		   $cusymb%$cudom+
                   13976: 		   $cucourseid%$cuname+
                   13977: 		   $cucourseid%$cudom);
                   13978:     }
                   13979:     return $return;
1.76      www      13980: }
                   13981: 
                   13982: sub receipt {
1.474     albertel 13983:     my ($part)=@_;
1.790     albertel 13984:     my ($symb,$courseid,$domain,$name) = &whichuser();
1.474     albertel 13985:     return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76      www      13986: }
1.260     ng       13987: 
1.790     albertel 13988: sub whichuser {
                   13989:     my ($passedsymb)=@_;
                   13990:     my ($symb,$courseid,$domain,$name,$publicuser);
                   13991:     if (defined($env{'form.grade_symb'})) {
                   13992: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
                   13993: 	my $allowed=&allowed('vgr',$tmp_courseid);
                   13994: 	if (!$allowed &&
                   13995: 	    exists($env{'request.course.sec'}) &&
                   13996: 	    $env{'request.course.sec'} !~ /^\s*$/) {
                   13997: 	    $allowed=&allowed('vgr',$tmp_courseid.
                   13998: 			      '/'.$env{'request.course.sec'});
                   13999: 	}
                   14000: 	if ($allowed) {
                   14001: 	    ($symb)=&get_env_multiple('form.grade_symb');
                   14002: 	    $courseid=$tmp_courseid;
                   14003: 	    ($domain)=&get_env_multiple('form.grade_domain');
                   14004: 	    ($name)=&get_env_multiple('form.grade_username');
                   14005: 	    return ($symb,$courseid,$domain,$name,$publicuser);
                   14006: 	}
                   14007:     }
                   14008:     if (!$passedsymb) {
                   14009: 	$symb=&symbread();
                   14010:     } else {
                   14011: 	$symb=$passedsymb;
                   14012:     }
                   14013:     $courseid=$env{'request.course.id'};
                   14014:     $domain=$env{'user.domain'};
                   14015:     $name=$env{'user.name'};
                   14016:     if ($name eq 'public' && $domain eq 'public') {
                   14017: 	if (!defined($env{'form.username'})) {
                   14018: 	    $env{'form.username'}.=time.rand(10000000);
                   14019: 	}
                   14020: 	$name.=$env{'form.username'};
                   14021:     }
                   14022:     return ($symb,$courseid,$domain,$name,$publicuser);
                   14023: 
                   14024: }
                   14025: 
1.36      albertel 14026: # ------------------------------------------------------------ Serves up a file
1.472     albertel 14027: # returns either the contents of the file or 
                   14028: # -1 if the file doesn't exist
1.481     raeburn  14029: #
                   14030: # if the target is a file that was uploaded via DOCS, 
                   14031: # a check will be made to see if a current copy exists on the local server,
                   14032: # if it does this will be served, otherwise a copy will be retrieved from
                   14033: # the home server for the course and stored in /home/httpd/html/userfiles on
                   14034: # the local server.   
1.472     albertel 14035: 
1.36      albertel 14036: sub getfile {
1.538     albertel 14037:     my ($file) = @_;
1.609     banghart 14038:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538     albertel 14039:     &repcopy($file);
                   14040:     return &readfile($file);
                   14041: }
                   14042: 
                   14043: sub repcopy_userfile {
                   14044:     my ($file)=@_;
1.1142    raeburn  14045:     my $londocroot = $perlvar{'lonDocRoot'};
                   14046:     if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
1.1164    raeburn  14047:     if ($file =~ m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
1.538     albertel 14048:     my ($cdom,$cnum,$filename) = 
1.811     albertel 14049: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538     albertel 14050:     my $uri="/uploaded/$cdom/$cnum/$filename";
                   14051:     if (-e "$file") {
1.828     www      14052: # we already have a local copy, check it out
1.538     albertel 14053: 	my @fileinfo = stat($file);
1.828     www      14054: 	my $rtncode;
                   14055: 	my $info;
1.538     albertel 14056: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482     albertel 14057: 	if ($lwpresp ne 'ok') {
1.828     www      14058: # there is no such file anymore, even though we had a local copy
1.482     albertel 14059: 	    if ($rtncode eq '404') {
1.538     albertel 14060: 		unlink($file);
1.482     albertel 14061: 	    }
                   14062: 	    return -1;
                   14063: 	}
                   14064: 	if ($info < $fileinfo[9]) {
1.828     www      14065: # nice, the file we have is up-to-date, just say okay
1.607     raeburn  14066: 	    return 'ok';
1.828     www      14067: 	} else {
                   14068: # the file is outdated, get rid of it
                   14069: 	    unlink($file);
1.482     albertel 14070: 	}
1.828     www      14071:     }
                   14072: # one way or the other, at this point, we don't have the file
                   14073: # construct the correct path for the file
                   14074:     my @parts = ($cdom,$cnum); 
                   14075:     if ($filename =~ m|^(.+)/[^/]+$|) {
                   14076: 	push @parts, split(/\//,$1);
                   14077:     }
                   14078:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
                   14079:     foreach my $part (@parts) {
                   14080: 	$path .= '/'.$part;
                   14081: 	if (!-e $path) {
                   14082: 	    mkdir($path,0770);
1.482     albertel 14083: 	}
                   14084:     }
1.828     www      14085: # now the path exists for sure
                   14086: # get a user agent
                   14087:     my $transferfile=$file.'.in.transfer';
                   14088: # FIXME: this should flock
                   14089:     if (-e $transferfile) { return 'ok'; }
                   14090:     my $request;
                   14091:     $uri=~s/^\///;
1.980     raeburn  14092:     my $homeserver = &homeserver($cnum,$cdom);
1.1398    raeburn  14093:     my $hostname = &hostname($homeserver);
1.980     raeburn  14094:     my $protocol = $protocol{$homeserver};
                   14095:     $protocol = 'http' if ($protocol ne 'https');
1.1397    raeburn  14096:     $request=new HTTP::Request('GET',$protocol.'://'.$hostname.'/raw/'.$uri);
1.1345    raeburn  14097:     my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,$transferfile,\%perlvar,'',0,1);
1.828     www      14098: # did it work?
                   14099:     if ($response->is_error()) {
                   14100: 	unlink($transferfile);
                   14101: 	&logthis("Userfile repcopy failed for $uri");
                   14102: 	return -1;
                   14103:     }
                   14104: # worked, rename the transfer file
                   14105:     rename($transferfile,$file);
1.607     raeburn  14106:     return 'ok';
1.481     raeburn  14107: }
                   14108: 
1.517     albertel 14109: sub tokenwrapper {
                   14110:     my $uri=shift;
1.980     raeburn  14111:     $uri=~s|^https?\://([^/]+)||;
1.552     albertel 14112:     $uri=~s|^/||;
1.620     albertel 14113:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517     albertel 14114:     my $token=$1;
1.552     albertel 14115:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
                   14116:     if ($udom && $uname && $file) {
                   14117: 	$file=~s|(\?\.*)*$||;
1.949     raeburn  14118:         &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
1.980     raeburn  14119:         my $homeserver = &homeserver($uname,$udom);
1.1397    raeburn  14120:         my $hostname = &hostname($homeserver);
1.980     raeburn  14121:         my $protocol = $protocol{$homeserver};
                   14122:         $protocol = 'http' if ($protocol ne 'https');
1.1397    raeburn  14123:         return $protocol.'://'.$hostname.'/'.$uri.
1.517     albertel 14124:                (($uri=~/\?/)?'&':'?').'token='.$token.
                   14125:                                '&tokenissued='.$perlvar{'lonHostID'};
                   14126:     } else {
                   14127:         return '/adm/notfound.html';
                   14128:     }
                   14129: }
                   14130: 
1.828     www      14131: # call with reqtype HEAD: get last modification time
                   14132: # call with reqtype GET: get the file contents
                   14133: # Do not call this with reqtype GET for large files! It loads everything into memory
                   14134: #
1.481     raeburn  14135: sub getuploaded {
                   14136:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
                   14137:     $uri=~s/^\///;
1.980     raeburn  14138:     my $homeserver = &homeserver($cnum,$cdom);
1.1397    raeburn  14139:     my $hostname = &hostname($homeserver);
1.980     raeburn  14140:     my $protocol = $protocol{$homeserver};
                   14141:     $protocol = 'http' if ($protocol ne 'https');
1.1397    raeburn  14142:     $uri = $protocol.'://'.$hostname.'/raw/'.$uri;
1.481     raeburn  14143:     my $request=new HTTP::Request($reqtype,$uri);
1.1345    raeburn  14144:     my $response=&LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,'',0,1);
1.481     raeburn  14145:     $$rtncode = $response->code;
1.482     albertel 14146:     if (! $response->is_success()) {
                   14147: 	return 'failed';
                   14148:     }      
                   14149:     if ($reqtype eq 'HEAD') {
1.486     www      14150: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482     albertel 14151:     } elsif ($reqtype eq 'GET') {
                   14152: 	$$info = $response->content;
1.472     albertel 14153:     }
1.482     albertel 14154:     return 'ok';
1.36      albertel 14155: }
                   14156: 
1.481     raeburn  14157: sub readfile {
                   14158:     my $file = shift;
                   14159:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
                   14160:     my $fh;
1.1359    raeburn  14161:     open($fh,"<",$file);
1.481     raeburn  14162:     my $a='';
1.800     albertel 14163:     while (my $line = <$fh>) { $a .= $line; }
1.481     raeburn  14164:     return $a;
                   14165: }
                   14166: 
1.36      albertel 14167: sub filelocation {
1.590     banghart 14168:     my ($dir,$file) = @_;
                   14169:     my $location;
                   14170:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700     albertel 14171: 
                   14172:     if ($file =~ m-^/adm/-) {
                   14173: 	$file=~s-^/adm/wrapper/-/-;
                   14174: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
                   14175:     }
1.882     albertel 14176: 
1.1139    www      14177:     if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
1.956     raeburn  14178:         $location = $file;
1.609     banghart 14179:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590     banghart 14180:         my ($udom,$uname,$filename)=
1.811     albertel 14181:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590     banghart 14182:         my $home=&homeserver($uname,$udom);
                   14183:         my $is_me=0;
                   14184:         my @ids=&current_machine_ids();
                   14185:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
                   14186:         if ($is_me) {
1.1117    foxr     14187:   	    $location=propath($udom,$uname).'/userfiles/'.$filename;
1.590     banghart 14188:         } else {
                   14189:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
                   14190:   	      $udom.'/'.$uname.'/'.$filename;
                   14191:         }
1.882     albertel 14192:     } elsif ($file =~ m-^/adm/-) {
                   14193: 	$location = $perlvar{'lonDocRoot'}.'/'.$file;
1.590     banghart 14194:     } else {
                   14195:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.1139    www      14196:         $file=~s:^/(res|priv)/:/:;
                   14197:         my $space=$1;
1.590     banghart 14198:         if ( !( $file =~ m:^/:) ) {
                   14199:             $location = $dir. '/'.$file;
                   14200:         } else {
1.1142    raeburn  14201:             $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
1.590     banghart 14202:         }
1.59      albertel 14203:     }
1.590     banghart 14204:     $location=~s://+:/:g; # remove duplicate /
1.930     albertel 14205:     while ($location=~m{/\.\./}) {
                   14206: 	if ($location =~ m{/[^/]+/\.\./}) {
                   14207: 	    $location=~ s{/[^/]+/\.\./}{/}g;
                   14208: 	} else {
                   14209: 	    $location=~ s{/\.\./}{/}g;
                   14210: 	}
                   14211:     } #remove dir/..
1.590     banghart 14212:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
                   14213:     return $location;
1.46      www      14214: }
1.36      albertel 14215: 
1.46      www      14216: sub hreflocation {
                   14217:     my ($dir,$file)=@_;
1.980     raeburn  14218:     unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
1.666     albertel 14219: 	$file=filelocation($dir,$file);
1.700     albertel 14220:     } elsif ($file=~m-^/adm/-) {
                   14221: 	$file=~s-^/adm/wrapper/-/-;
                   14222: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
1.666     albertel 14223:     }
                   14224:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
                   14225: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
                   14226:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.1143    raeburn  14227: 	$file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
                   14228: 	        {/uploaded/$1/$2/}x;
1.46      www      14229:     }
1.913     albertel 14230:     if ($file=~ m{^/userfiles/}) {
                   14231: 	$file =~ s{^/userfiles/}{/uploaded/};
                   14232:     }
1.462     albertel 14233:     return $file;
1.465     albertel 14234: }
                   14235: 
1.1139    www      14236: 
                   14237: 
                   14238: 
                   14239: 
1.465     albertel 14240: sub current_machine_domains {
1.853     albertel 14241:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
                   14242: }
                   14243: 
                   14244: sub machine_domains {
                   14245:     my ($hostname) = @_;
1.465     albertel 14246:     my @domains;
1.838     albertel 14247:     my %hostname = &all_hostnames();
1.465     albertel 14248:     while( my($id, $name) = each(%hostname)) {
1.467     matthew  14249: #	&logthis("-$id-$name-$hostname-");
1.465     albertel 14250: 	if ($hostname eq $name) {
1.844     albertel 14251: 	    push(@domains,&host_domain($id));
1.465     albertel 14252: 	}
                   14253:     }
                   14254:     return @domains;
                   14255: }
                   14256: 
                   14257: sub current_machine_ids {
1.853     albertel 14258:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
                   14259: }
                   14260: 
                   14261: sub machine_ids {
                   14262:     my ($hostname) = @_;
                   14263:     $hostname ||= &hostname($perlvar{'lonHostID'});
1.465     albertel 14264:     my @ids;
1.888     albertel 14265:     my %name_to_host = &all_names();
1.889     albertel 14266:     if (ref($name_to_host{$hostname}) eq 'ARRAY') {
                   14267: 	return @{ $name_to_host{$hostname} };
                   14268:     }
                   14269:     return;
1.31      www      14270: }
                   14271: 
1.824     raeburn  14272: sub additional_machine_domains {
                   14273:     my @domains;
1.1359    raeburn  14274:     open(my $fh,"<","$perlvar{'lonTabDir'}/expected_domains.tab");
1.824     raeburn  14275:     while( my $line = <$fh>) {
                   14276:         $line =~ s/\s//g;
                   14277:         push(@domains,$line);
                   14278:     }
                   14279:     return @domains;
                   14280: }
                   14281: 
                   14282: sub default_login_domain {
                   14283:     my $domain = $perlvar{'lonDefDomain'};
                   14284:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
                   14285:     foreach my $posdom (&current_machine_domains(),
                   14286:                         &additional_machine_domains()) {
                   14287:         if (lc($posdom) eq lc($testdomain)) {
                   14288:             $domain=$posdom;
                   14289:             last;
                   14290:         }
                   14291:     }
                   14292:     return $domain;
                   14293: }
                   14294: 
1.1414    raeburn  14295: sub shared_institution {
1.1439    raeburn  14296:     my ($dom,$lonhost) = @_;
                   14297:     if ($lonhost eq '') {
                   14298:         $lonhost = $perlvar{'lonHostID'};
                   14299:     }
1.1414    raeburn  14300:     my $same_intdom;
1.1439    raeburn  14301:     my $hostintdom = &internet_dom($lonhost);
1.1414    raeburn  14302:     if ($hostintdom ne '') {
                   14303:         my %iphost = &get_iphost();
                   14304:         my $primary_id = &domain($dom,'primary');
                   14305:         my $primary_ip = &get_host_ip($primary_id);
                   14306:         if (ref($iphost{$primary_ip}) eq 'ARRAY') {
                   14307:             foreach my $id (@{$iphost{$primary_ip}}) {
                   14308:                 my $intdom = &internet_dom($id);
                   14309:                 if ($intdom eq $hostintdom) {
                   14310:                     $same_intdom = 1;
                   14311:                     last;
                   14312:                 }
                   14313:             }
                   14314:         }
                   14315:     }
                   14316:     return $same_intdom;
                   14317: }
                   14318: 
1.1398    raeburn  14319: sub uses_sts {
                   14320:     my ($ignore_cache) = @_;
                   14321:     my $lonhost = $perlvar{'lonHostID'};
                   14322:     my $hostname = &hostname($lonhost);
                   14323:     my $sts_on;
                   14324:     if ($protocol{$lonhost} eq 'https') {
                   14325:         my $cachetime = 12*3600;
                   14326:         if (!$ignore_cache) {
                   14327:             ($sts_on,my $cached)=&is_cached_new('stspolicy',$lonhost);
                   14328:             if (defined($cached)) {
                   14329:                 return $sts_on;
                   14330:             }
                   14331:         }
                   14332:         my $url = $protocol{$lonhost}.'://'.$hostname.'/index.html';
                   14333:         my $request=new HTTP::Request('HEAD',$url);
                   14334:         my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,'','','',1);
                   14335:         if ($response->is_success) {
                   14336:             my $has_sts = $response->header('Strict-Transport-Security');
                   14337:             if ($has_sts eq '') {
                   14338:                 $sts_on = 0;
                   14339:             } else {
                   14340:                 if ($has_sts =~ /\Qmax-age=\E(\d+)/) {
                   14341:                     my $maxage = $1;
                   14342:                     if ($maxage) {
                   14343:                         $sts_on = 1;
                   14344:                     } else {
                   14345:                         $sts_on = 0;
                   14346:                     }
                   14347:                 } else {
                   14348:                     $sts_on = 0;
                   14349:                 }
                   14350:             }
                   14351:             return &do_cache_new('stspolicy',$lonhost,$sts_on,$cachetime);
                   14352:         }
                   14353:     }
                   14354:     return;
                   14355: }
                   14356: 
1.1434    raeburn  14357: sub get_requestor_ip {
                   14358:     my ($r,$nolookup,$noproxy) = @_;
                   14359:     my $from_ip;
                   14360:     if (ref($r)) {
1.1447    raeburn  14361:         if ($r->can('useragent_ip')) {
                   14362:             if ($noproxy && $r->can('client_ip')) {
                   14363:                 $from_ip = $r->client_ip();
                   14364:             } else {
                   14365:                 $from_ip = $r->useragent_ip();
                   14366:             }
                   14367:         } elsif ($r->connection->can('remote_ip')) {
                   14368:             $from_ip = $r->connection->remote_ip();
                   14369:         } else {
                   14370:             $from_ip = $r->get_remote_host($nolookup);
                   14371:         }
1.1434    raeburn  14372:     } else {
                   14373:         $from_ip = $ENV{'REMOTE_ADDR'};
                   14374:     }
                   14375:     return $from_ip if ($noproxy); 
                   14376:     # Who controls proxy settings for server
                   14377:     my $dom_in_use = $Apache::lonnet::perlvar{'lonDefDomain'};
                   14378:     my $proxyinfo = &get_proxy_settings($dom_in_use);
                   14379:     if ((ref($proxyinfo) eq 'HASH') && ($from_ip)) {
1.1437    raeburn  14380:         if ($proxyinfo->{'vpnint'}) {
                   14381:             if (&ip_match($from_ip,$proxyinfo->{'vpnint'})) {
1.1434    raeburn  14382:                 return $from_ip;
                   14383:             }
                   14384:         }
                   14385:         if ($proxyinfo->{'trusted'}) {
                   14386:             if (&ip_match($from_ip,$proxyinfo->{'trusted'})) {
                   14387:                 my $ipheader = $proxyinfo->{'ipheader'};
                   14388:                 my ($ip,$xfor);
                   14389:                 if (ref($r)) {
                   14390:                     if ($ipheader) {
                   14391:                         $ip = $r->headers_in->{$ipheader};
                   14392:                     }
                   14393:                     $xfor = $r->headers_in->{'X-Forwarded-For'};
                   14394:                 } else {
                   14395:                     if ($ipheader) {
                   14396:                         $ip = $ENV{'HTTP_'.uc($ipheader)};
                   14397:                     }
                   14398:                     $xfor = $ENV{'HTTP_X_FORWARDED_FOR'};
                   14399:                 }
                   14400:                 if (($ip eq '') && ($xfor ne '')) {
                   14401:                     foreach my $poss_ip (reverse(split(/\s*,\s*/,$xfor))) {
                   14402:                         unless (&ip_match($poss_ip,$proxyinfo->{'trusted'})) {
                   14403:                             $ip = $poss_ip;
1.1435    raeburn  14404:                             last;
1.1434    raeburn  14405:                         }
                   14406:                     }
                   14407:                 }
                   14408:                 if ($ip ne '') {
                   14409:                     return $ip;
                   14410:                 }
                   14411:             }
                   14412:         }
                   14413:     }
                   14414:     return $from_ip;
                   14415: }
                   14416: 
                   14417: sub get_proxy_settings {
                   14418:     my ($dom_in_use) = @_;
                   14419:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom_in_use);
                   14420:     my $proxyinfo = {
                   14421:                        ipheader => $domdefaults{'waf_ipheader'},
                   14422:                        trusted  => $domdefaults{'waf_trusted'},
1.1437    raeburn  14423:                        vpnint   => $domdefaults{'waf_vpnint'},
1.1438    raeburn  14424:                        vpnext   => $domdefaults{'waf_vpnext'},
1.1434    raeburn  14425:                     };
                   14426:     return $proxyinfo;
                   14427: }
                   14428: 
                   14429: sub ip_match {
                   14430:     my ($ip,$pattern_str) = @_;
                   14431:     $ip=Net::CIDR::cidrvalidate($ip);
                   14432:     if ($ip) {
                   14433:         return Net::CIDR::cidrlookup($ip,split(/\s*,\s*/,$pattern_str));
                   14434:     }
                   14435:     return;
                   14436: }
                   14437: 
                   14438: sub get_proxy_alias {
                   14439:     my $lonhost = $perlvar{'lonHostID'};
                   14440:     if ($lonhost ne '') {
                   14441:         my ($alias,$cached) = &is_cached_new('proxyalias',$lonhost);
                   14442:         if ($cached) {
                   14443:             return $alias;
                   14444:         }
                   14445:         my $dom = &Apache::lonnet::host_domain($lonhost);
                   14446:         if ($dom ne '') {
                   14447:             my $cachetime = 60*60*24;
                   14448:             my %domconfig =
1.1437    raeburn  14449:                 &Apache::lonnet::get_dom('configuration',['wafproxy'],$dom);
1.1434    raeburn  14450:             my $alias;
1.1437    raeburn  14451:             if (ref($domconfig{'wafproxy'}) eq 'HASH') {
                   14452:                 if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
                   14453:                     $alias = $domconfig{'wafproxy'}{'alias'}{$lonhost};
1.1434    raeburn  14454:                 }
                   14455:             }
                   14456:             return &do_cache_new('proxyalias',$lonhost,$alias,$cachetime);
                   14457:         }
                   14458:     }
                   14459:     return;
                   14460: }
                   14461: 
1.31      www      14462: # ------------------------------------------------------------- Declutters URLs
                   14463: 
                   14464: sub declutter {
                   14465:     my $thisfn=shift;
1.569     albertel 14466:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.1261    raeburn  14467:     unless ($thisfn=~m{^/home/httpd/html/priv/}) {
                   14468:         $thisfn=~s{^/home/httpd/html}{};
                   14469:     }
1.31      www      14470:     $thisfn=~s/^\///;
1.697     albertel 14471:     $thisfn=~s|^adm/wrapper/||;
                   14472:     $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31      www      14473:     $thisfn=~s/^res\///;
1.1172    bisitz   14474:     $thisfn=~s/^priv\///;
1.1032    raeburn  14475:     unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
                   14476:         $thisfn=~s/\?.+$//;
                   14477:     }
1.268     www      14478:     return $thisfn;
                   14479: }
                   14480: 
                   14481: # ------------------------------------------------------------- Clutter up URLs
                   14482: 
                   14483: sub clutter {
                   14484:     my $thisfn='/'.&declutter(shift);
1.887     albertel 14485:     if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
1.884     albertel 14486: 	|| $thisfn =~ m{^/adm/(includes|pages)} ) { 
1.270     www      14487:        $thisfn='/res'.$thisfn; 
                   14488:     }
1.1031    raeburn  14489:     if ($thisfn !~m|^/adm|) {
                   14490: 	if ($thisfn =~ m|^/ext/|) {
1.694     albertel 14491: 	    $thisfn='/adm/wrapper'.$thisfn;
1.695     albertel 14492: 	} else {
                   14493: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
                   14494: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698     albertel 14495: 	    if ($embstyle eq 'ssi'
                   14496: 		|| ($embstyle eq 'hdn')
                   14497: 		|| ($embstyle eq 'rat')
                   14498: 		|| ($embstyle eq 'prv')
                   14499: 		|| ($embstyle eq 'ign')) {
                   14500: 		#do nothing with these
                   14501: 	    } elsif (($embstyle eq 'img') 
1.695     albertel 14502: 		|| ($embstyle eq 'emb')
                   14503: 		|| ($embstyle eq 'wrp')) {
                   14504: 		$thisfn='/adm/wrapper'.$thisfn;
1.698     albertel 14505: 	    } elsif ($embstyle eq 'unk'
                   14506: 		     && $thisfn!~/\.(sequence|page)$/) {
1.695     albertel 14507: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698     albertel 14508: 	    } else {
1.718     www      14509: #		&logthis("Got a blank emb style");
1.695     albertel 14510: 	    }
1.694     albertel 14511: 	}
1.1343    raeburn  14512:     } elsif ($thisfn =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
1.1298    raeburn  14513:         $thisfn='/adm/wrapper'.$thisfn;
1.694     albertel 14514:     }
1.31      www      14515:     return $thisfn;
1.12      www      14516: }
                   14517: 
1.787     albertel 14518: sub clutter_with_no_wrapper {
                   14519:     my $uri = &clutter(shift);
                   14520:     if ($uri =~ m-^/adm/-) {
                   14521: 	$uri =~ s-^/adm/wrapper/-/-;
                   14522: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
                   14523:     }
                   14524:     return $uri;
                   14525: }
                   14526: 
1.557     albertel 14527: sub freeze_escape {
                   14528:     my ($value)=@_;
                   14529:     if (ref($value)) {
                   14530: 	$value=&nfreeze($value);
                   14531: 	return '__FROZEN__'.&escape($value);
                   14532:     }
                   14533:     return &escape($value);
                   14534: }
                   14535: 
1.11      www      14536: 
1.557     albertel 14537: sub thaw_unescape {
                   14538:     my ($value)=@_;
                   14539:     if ($value =~ /^__FROZEN__/) {
                   14540: 	substr($value,0,10,undef);
                   14541: 	$value=&unescape($value);
                   14542: 	return &thaw($value);
                   14543:     }
                   14544:     return &unescape($value);
                   14545: }
                   14546: 
1.436     albertel 14547: sub correct_line_ends {
                   14548:     my ($result)=@_;
                   14549:     $$result =~s/\r\n/\n/mg;
                   14550:     $$result =~s/\r/\n/mg;
1.415     albertel 14551: }
1.1       albertel 14552: # ================================================================ Main Program
                   14553: 
1.184     www      14554: sub goodbye {
1.204     albertel 14555:    &logthis("Starting Shut down");
1.443     albertel 14556: #not converted to using infrastruture and probably shouldn't be
1.870     albertel 14557:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
1.443     albertel 14558: #converted
1.599     albertel 14559: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
1.870     albertel 14560:    &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
                   14561: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
                   14562: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
1.425     albertel 14563: #1.1 only
1.870     albertel 14564: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
                   14565: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
                   14566: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
                   14567: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
                   14568:    &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
1.599     albertel 14569:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
                   14570:    &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184     www      14571:    &flushcourselogs();
                   14572:    &logthis("Shutting down");
                   14573: }
                   14574: 
1.852     albertel 14575: sub get_dns {
1.1210    raeburn  14576:     my ($url,$func,$ignore_cache,$nocache,$hashref) = @_;
1.869     albertel 14577:     if (!$ignore_cache) {
                   14578: 	my ($content,$cached)=
                   14579: 	    &Apache::lonnet::is_cached_new('dns',$url);
                   14580: 	if ($cached) {
1.1210    raeburn  14581: 	    &$func($content,$hashref);
1.869     albertel 14582: 	    return;
                   14583: 	}
                   14584:     }
                   14585: 
                   14586:     my %alldns;
1.1379    raeburn  14587:     if (open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab")) {
                   14588:         foreach my $dns (<$config>) {
                   14589: 	    next if ($dns !~ /^\^(\S*)/x);
                   14590:             my $line = $1;
                   14591:             my ($host,$protocol) = split(/:/,$line);
                   14592:             if ($protocol ne 'https') {
                   14593:                 $protocol = 'http';
                   14594:             }
                   14595: 	    $alldns{$host} = $protocol;
1.979     raeburn  14596:         }
1.1379    raeburn  14597:         close($config);
1.869     albertel 14598:     }
                   14599:     while (%alldns) {
1.1263    raeburn  14600: 	my ($dns) = sort { $b cmp $a } keys(%alldns);
1.979     raeburn  14601: 	my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
1.1345    raeburn  14602:         my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar,30,0);
1.979     raeburn  14603:         delete($alldns{$dns});
1.852     albertel 14604: 	next if ($response->is_error());
1.1379    raeburn  14605:         if ($url eq '/adm/dns/loncapaCRL') {
                   14606:             return &$func($response);
                   14607:         } else {
                   14608: 	    my @content = split("\n",$response->content);
                   14609: 	    unless ($nocache) {
                   14610: 	        &do_cache_new('dns',$url,\@content,30*24*60*60);
                   14611: 	    }
                   14612: 	    &$func(\@content,$hashref);
                   14613:             return;
                   14614:         }
                   14615:     }
                   14616:     my $which = (split('/',$url,4))[3];
                   14617:     if ($which eq 'loncapaCRL') {
                   14618:         my $diskfile = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
                   14619:         if (-e $diskfile) {
                   14620:             &logthis("unable to contact DNS, on disk file $diskfile not updated");
                   14621:         } else {
                   14622:             &logthis("unable to contact DNS, no on disk file $diskfile available");
                   14623:         }
                   14624:     } else {
                   14625:         &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
                   14626:         if (open(my $config,"<","$perlvar{'lonTabDir'}/dns_$which.tab")) {
                   14627:             my @content = <$config>;
                   14628:             close($config);
                   14629:             &$func(\@content,$hashref);
                   14630:         }
1.852     albertel 14631:     }
1.1210    raeburn  14632:     return;
                   14633: }
                   14634: 
                   14635: # ------------------------------------------------------Get DNS checksums file
1.1211    raeburn  14636: sub parse_dns_checksums_tab {
1.1210    raeburn  14637:     my ($lines,$hashref) = @_;
1.1264    raeburn  14638:     my $lonhost = $perlvar{'lonHostID'};
                   14639:     my $machine_dom = &Apache::lonnet::host_domain($lonhost);
1.1210    raeburn  14640:     my $loncaparev = &get_server_loncaparev($machine_dom);
1.1264    raeburn  14641:     my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
                   14642:     my $webconfdir = '/etc/httpd/conf';
                   14643:     if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
                   14644:         $webconfdir = '/etc/apache2';
                   14645:     } elsif ($distro =~ /^sles(\d+)$/) {
                   14646:         if ($1 >= 10) {
                   14647:             $webconfdir = '/etc/apache2';
                   14648:         }
                   14649:     } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
                   14650:         if ($1 >= 10.0) {
                   14651:             $webconfdir = '/etc/apache2';
                   14652:         }
                   14653:     }
1.1210    raeburn  14654:     my ($release,$timestamp) = split(/\-/,$loncaparev);
                   14655:     my (%chksum,%revnum);
                   14656:     if (ref($lines) eq 'ARRAY') {
                   14657:         chomp(@{$lines});
1.1238    raeburn  14658:         my $version = shift(@{$lines});
                   14659:         if ($version eq $release) {  
1.1210    raeburn  14660:             foreach my $line (@{$lines}) {
1.1238    raeburn  14661:                 my ($file,$version,$shasum) = split(/,/,$line);
1.1264    raeburn  14662:                 if ($file =~ m{^/etc/httpd/conf}) {
                   14663:                     if ($webconfdir eq '/etc/apache2') {
                   14664:                         $file =~ s{^\Q/etc/httpd/conf/\E}{$webconfdir/};
                   14665:                     }
                   14666:                 }
1.1238    raeburn  14667:                 $chksum{$file} = $shasum;
                   14668:                 $revnum{$file} = $version;
1.1210    raeburn  14669:             }
                   14670:             if (ref($hashref) eq 'HASH') {
                   14671:                 %{$hashref} = (
                   14672:                                 sums     => \%chksum,
                   14673:                                 versions => \%revnum,
                   14674:                               );
                   14675:             }
                   14676:         }
                   14677:     }
1.869     albertel 14678:     return;
1.852     albertel 14679: }
1.1210    raeburn  14680: 
                   14681: sub fetch_dns_checksums {
1.1238    raeburn  14682:     my %checksums;
                   14683:     my $machine_dom = &Apache::lonnet::host_domain($perlvar{'lonHostID'});
1.1260    raeburn  14684:     my $loncaparev = &get_server_loncaparev($machine_dom,$perlvar{'lonHostID'});
1.1238    raeburn  14685:     my ($release,$timestamp) = split(/\-/,$loncaparev);
                   14686:     &get_dns("/adm/dns/checksums/$release",\&parse_dns_checksums_tab,1,1,
1.1211    raeburn  14687:              \%checksums);
1.1210    raeburn  14688:     return \%checksums;
                   14689: }
                   14690: 
1.1379    raeburn  14691: sub fetch_crl_pemfile {
                   14692:     return &get_dns("/adm/dns/loncapaCRL",\&save_crl_pem,1,1);
                   14693: }
                   14694: 
                   14695: sub save_crl_pem {
                   14696:     my ($response) = @_;
1.1380    raeburn  14697:     my ($msg,$hadchanges);
1.1379    raeburn  14698:     if (ref($response)) {
                   14699:         my $now = time;
                   14700:         my $lonca = $perlvar{'lonCertificateDirectory'}.'/'.$perlvar{'lonnetCertificateAuthority'};
                   14701:         my $tmpcrl = $tmpdir.'/'.$perlvar{'lonnetCertRevocationList'}.'_'.$now.'.'.$$.'.tmp';
                   14702:         if (open(my $fh,'>',"$tmpcrl")) {
                   14703:             print $fh $response->content;
                   14704:             close($fh);
                   14705:             if (-e $lonca) {
                   14706:                 if (open(PIPE,"openssl crl -in $tmpcrl -inform pem -CAfile $lonca -noout 2>&1 |")) {
                   14707:                     my $check = <PIPE>;
                   14708:                     close(PIPE);
                   14709:                     chomp($check);
                   14710:                     if ($check eq 'verify OK') {
                   14711:                         my $dest = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
1.1380    raeburn  14712:                         my $backup;
1.1379    raeburn  14713:                         if (-e $dest) {
1.1380    raeburn  14714:                             if (&File::Copy::move($dest,"$dest.bak")) {
                   14715:                                 $backup = 'ok';
                   14716:                             }
1.1379    raeburn  14717:                         }
                   14718:                         if (&File::Copy::move($tmpcrl,$dest)) {
                   14719:                             $msg = 'ok';
1.1380    raeburn  14720:                             if ($backup) {
                   14721:                                 my (%oldnums,%newnums);
                   14722:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest.bak |grep 'Serial Number' |")) {
                   14723:                                     while (<PIPE>) {
                   14724:                                         $oldnums{(split(/:/))[1]} = 1;
                   14725:                                     }
                   14726:                                     close(PIPE);
                   14727:                                 }
                   14728:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest |grep 'Serial Number' |")) {
                   14729:                                     while(<PIPE>) {
                   14730:                                         $newnums{(split(/:/))[1]} = 1;
                   14731:                                     }
                   14732:                                     close(PIPE);
                   14733:                                 }
                   14734:                                 foreach my $key (sort {$b <=> $a } (keys(%newnums))) {
                   14735:                                     unless (exists($oldnums{$key})) {
                   14736:                                         $hadchanges = 1;
                   14737:                                         last;
                   14738:                                     }
                   14739:                                 }
                   14740:                                 unless ($hadchanges) {
                   14741:                                     foreach my $key (sort {$b <=> $a } (keys(%oldnums))) {
                   14742:                                         unless (exists($newnums{$key})) {
                   14743:                                             $hadchanges = 1;
                   14744:                                             last;
                   14745:                                         }
                   14746:                                     }
                   14747:                                 }
                   14748:                             }
1.1379    raeburn  14749:                         }
                   14750:                     } else {
                   14751:                         unlink($tmpcrl);
                   14752:                     }
                   14753:                 } else {
                   14754:                     unlink($tmpcrl);
                   14755:                 }
                   14756:             } else {
                   14757:                 unlink($tmpcrl);
                   14758:             }
                   14759:         }
                   14760:     }
1.1380    raeburn  14761:     return ($msg,$hadchanges);
1.1379    raeburn  14762: }
                   14763: 
1.327     albertel 14764: # ------------------------------------------------------------ Read domain file
                   14765: {
1.852     albertel 14766:     my $loaded;
1.846     albertel 14767:     my %domain;
                   14768: 
1.852     albertel 14769:     sub parse_domain_tab {
                   14770: 	my ($lines) = @_;
                   14771: 	foreach my $line (@$lines) {
                   14772: 	    next if ($line =~ /^(\#|\s*$ )/x);
1.403     www      14773: 
1.846     albertel 14774: 	    chomp($line);
1.852     albertel 14775: 	    my ($name,@elements) = split(/:/,$line,9);
1.846     albertel 14776: 	    my %this_domain;
                   14777: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
                   14778: 			       'lang_def', 'city', 'longi', 'lati',
                   14779: 			       'primary') {
                   14780: 		$this_domain{$field} = shift(@elements);
                   14781: 	    }
                   14782: 	    $domain{$name} = \%this_domain;
1.852     albertel 14783: 	}
                   14784:     }
1.864     albertel 14785: 
                   14786:     sub reset_domain_info {
                   14787: 	undef($loaded);
                   14788: 	undef(%domain);
                   14789:     }
                   14790: 
1.852     albertel 14791:     sub load_domain_tab {
1.1293    raeburn  14792: 	my ($ignore_cache,$nocache) = @_;
                   14793: 	&get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache,$nocache);
1.852     albertel 14794: 	my $fh;
1.1359    raeburn  14795: 	if (open($fh,"<",$perlvar{'lonTabDir'}.'/domain.tab')) {
1.852     albertel 14796: 	    my @lines = <$fh>;
                   14797: 	    &parse_domain_tab(\@lines);
1.448     albertel 14798: 	}
1.852     albertel 14799: 	close($fh);
                   14800: 	$loaded = 1;
1.327     albertel 14801:     }
1.846     albertel 14802: 
                   14803:     sub domain {
1.852     albertel 14804: 	&load_domain_tab() if (!$loaded);
                   14805: 
1.846     albertel 14806: 	my ($name,$what) = @_;
                   14807: 	return if ( !exists($domain{$name}) );
                   14808: 
                   14809: 	if (!$what) {
                   14810: 	    return $domain{$name}{'description'};
                   14811: 	}
                   14812: 	return $domain{$name}{$what};
                   14813:     }
1.974     raeburn  14814: 
                   14815:     sub domain_info {
                   14816:         &load_domain_tab() if (!$loaded);
                   14817:         return %domain;
                   14818:     }
                   14819: 
1.327     albertel 14820: }
                   14821: 
                   14822: 
1.1       albertel 14823: # ------------------------------------------------------------- Read hosts file
                   14824: {
1.838     albertel 14825:     my %hostname;
1.844     albertel 14826:     my %hostdom;
1.845     albertel 14827:     my %libserv;
1.852     albertel 14828:     my $loaded;
1.888     albertel 14829:     my %name_to_host;
1.1074    raeburn  14830:     my %internetdom;
1.1107    raeburn  14831:     my %LC_dns_serv;
1.852     albertel 14832: 
                   14833:     sub parse_hosts_tab {
                   14834: 	my ($file) = @_;
                   14835: 	foreach my $configline (@$file) {
                   14836: 	    next if ($configline =~ /^(\#|\s*$ )/x);
1.1107    raeburn  14837:             chomp($configline);
                   14838: 	    if ($configline =~ /^\^/) {
                   14839:                 if ($configline =~ /^\^([\w.\-]+)/) {
                   14840:                     $LC_dns_serv{$1} = 1;
                   14841:                 }
                   14842:                 next;
                   14843:             }
1.1074    raeburn  14844: 	    my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
1.852     albertel 14845: 	    $name=~s/\s//g;
                   14846: 	    if ($id && $domain && $role && $name) {
1.1351    raeburn  14847:                 if ((exists($hostname{$id})) && ($hostname{$id} ne '')) {
                   14848:                     my $curr = $hostname{$id};
                   14849:                     my $skip;
                   14850:                     if (ref($name_to_host{$curr}) eq 'ARRAY') {
                   14851:                         if (($curr eq $name) && (@{$name_to_host{$curr}} == 1)) {
                   14852:                             $skip = 1;
                   14853:                         } else {
                   14854:                             @{$name_to_host{$curr}} = grep { $_ ne $id } @{$name_to_host{$curr}};
                   14855:                         }
                   14856:                     }
                   14857:                     unless ($skip) {
                   14858:                         push(@{$name_to_host{$name}},$id);
                   14859:                     }
                   14860:                 } else {
                   14861:                     push(@{$name_to_host{$name}},$id);
                   14862:                 }
1.852     albertel 14863: 		$hostname{$id}=$name;
                   14864: 		$hostdom{$id}=$domain;
                   14865: 		if ($role eq 'library') { $libserv{$id}=$name; }
1.969     raeburn  14866:                 if (defined($protocol)) {
                   14867:                     if ($protocol eq 'https') {
                   14868:                         $protocol{$id} = $protocol;
                   14869:                     } else {
                   14870:                         $protocol{$id} = 'http'; 
                   14871:                     }
1.968     raeburn  14872:                 } else {
1.969     raeburn  14873:                     $protocol{$id} = 'http';
1.968     raeburn  14874:                 }
1.1074    raeburn  14875:                 if (defined($intdom)) {
                   14876:                     $internetdom{$id} = $intdom;
                   14877:                 }
1.852     albertel 14878: 	    }
                   14879: 	}
                   14880:     }
1.864     albertel 14881:     
                   14882:     sub reset_hosts_info {
1.897     albertel 14883: 	&purge_remembered();
1.864     albertel 14884: 	&reset_domain_info();
                   14885: 	&reset_hosts_ip_info();
1.1339    raeburn  14886:         undef(%internetdom);
1.892     albertel 14887: 	undef(%name_to_host);
1.864     albertel 14888: 	undef(%hostname);
                   14889: 	undef(%hostdom);
                   14890: 	undef(%libserv);
                   14891: 	undef($loaded);
                   14892:     }
1.1       albertel 14893: 
1.852     albertel 14894:     sub load_hosts_tab {
1.1293    raeburn  14895: 	my ($ignore_cache,$nocache) = @_;
                   14896: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache,$nocache);
1.1359    raeburn  14897: 	open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab");
1.852     albertel 14898: 	my @config = <$config>;
                   14899: 	&parse_hosts_tab(\@config);
                   14900: 	close($config);
                   14901: 	$loaded=1;
1.1       albertel 14902:     }
1.852     albertel 14903: 
1.838     albertel 14904:     sub hostname {
1.852     albertel 14905: 	&load_hosts_tab() if (!$loaded);
                   14906: 
1.838     albertel 14907: 	my ($lonid) = @_;
                   14908: 	return $hostname{$lonid};
                   14909:     }
1.845     albertel 14910: 
1.838     albertel 14911:     sub all_hostnames {
1.852     albertel 14912: 	&load_hosts_tab() if (!$loaded);
                   14913: 
1.838     albertel 14914: 	return %hostname;
                   14915:     }
1.845     albertel 14916: 
1.888     albertel 14917:     sub all_names {
1.1293    raeburn  14918:         my ($ignore_cache,$nocache) = @_;
                   14919: 	&load_hosts_tab($ignore_cache,$nocache) if (!$loaded);
1.888     albertel 14920: 
                   14921: 	return %name_to_host;
                   14922:     }
                   14923: 
1.974     raeburn  14924:     sub all_host_domain {
                   14925:         &load_hosts_tab() if (!$loaded);
                   14926:         return %hostdom;
                   14927:     }
                   14928: 
1.1339    raeburn  14929:     sub all_host_intdom {
                   14930:         &load_hosts_tab() if (!$loaded);
                   14931:         return %internetdom;
                   14932:     }
                   14933: 
1.845     albertel 14934:     sub is_library {
1.852     albertel 14935: 	&load_hosts_tab() if (!$loaded);
                   14936: 
1.845     albertel 14937: 	return exists($libserv{$_[0]});
                   14938:     }
                   14939: 
                   14940:     sub all_library {
1.852     albertel 14941: 	&load_hosts_tab() if (!$loaded);
                   14942: 
1.845     albertel 14943: 	return %libserv;
                   14944:     }
                   14945: 
1.1062    droeschl 14946:     sub unique_library {
                   14947: 	#2x reverse removes all hostnames that appear more than once
                   14948:         my %unique = reverse &all_library();
                   14949:         return reverse %unique;
                   14950:     }
                   14951: 
1.841     albertel 14952:     sub get_servers {
1.852     albertel 14953: 	&load_hosts_tab() if (!$loaded);
                   14954: 
1.841     albertel 14955: 	my ($domain,$type) = @_;
                   14956: 	my %possible_hosts = ($type eq 'library') ? %libserv
                   14957: 	                                          : %hostname;
                   14958: 	my %result;
1.842     albertel 14959: 	if (ref($domain) eq 'ARRAY') {
                   14960: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843     albertel 14961: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842     albertel 14962: 		    $result{$host} = $hostname;
                   14963: 		}
                   14964: 	    }
                   14965: 	} else {
                   14966: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
                   14967: 		if ($hostdom{$host} eq $domain) {
                   14968: 		    $result{$host} = $hostname;
                   14969: 		}
1.841     albertel 14970: 	    }
                   14971: 	}
                   14972: 	return %result;
                   14973:     }
1.845     albertel 14974: 
1.1062    droeschl 14975:     sub get_unique_servers {
                   14976:         my %unique = reverse &get_servers(@_);
                   14977: 	return reverse %unique;
                   14978:     }
                   14979: 
1.844     albertel 14980:     sub host_domain {
1.852     albertel 14981: 	&load_hosts_tab() if (!$loaded);
                   14982: 
1.844     albertel 14983: 	my ($lonid) = @_;
                   14984: 	return $hostdom{$lonid};
                   14985:     }
                   14986: 
1.841     albertel 14987:     sub all_domains {
1.852     albertel 14988: 	&load_hosts_tab() if (!$loaded);
                   14989: 
1.841     albertel 14990: 	my %seen;
                   14991: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
                   14992: 	return @uniq;
                   14993:     }
1.1074    raeburn  14994: 
                   14995:     sub internet_dom {
                   14996:         &load_hosts_tab() if (!$loaded);
                   14997: 
                   14998:         my ($lonid) = @_;
                   14999:         return $internetdom{$lonid};
                   15000:     }
1.1107    raeburn  15001: 
                   15002:     sub is_LC_dns {
                   15003:         &load_hosts_tab() if (!$loaded);
                   15004: 
                   15005:         my ($hostname) = @_;
                   15006:         return exists($LC_dns_serv{$hostname});
                   15007:     }
                   15008: 
1.1       albertel 15009: }
                   15010: 
1.847     albertel 15011: { 
                   15012:     my %iphost;
1.856     albertel 15013:     my %name_to_ip;
                   15014:     my %lonid_to_ip;
1.869     albertel 15015: 
1.847     albertel 15016:     sub get_hosts_from_ip {
                   15017: 	my ($ip) = @_;
                   15018: 	my %iphosts = &get_iphost();
                   15019: 	if (ref($iphosts{$ip})) {
                   15020: 	    return @{$iphosts{$ip}};
                   15021: 	}
                   15022: 	return;
1.839     albertel 15023:     }
1.864     albertel 15024:     
                   15025:     sub reset_hosts_ip_info {
                   15026: 	undef(%iphost);
                   15027: 	undef(%name_to_ip);
                   15028: 	undef(%lonid_to_ip);
                   15029:     }
1.856     albertel 15030: 
                   15031:     sub get_host_ip {
                   15032: 	my ($lonid) = @_;
                   15033: 	if (exists($lonid_to_ip{$lonid})) {
                   15034: 	    return $lonid_to_ip{$lonid};
                   15035: 	}
                   15036: 	my $name=&hostname($lonid);
                   15037:    	my $ip = gethostbyname($name);
                   15038: 	return if (!$ip || length($ip) ne 4);
                   15039: 	$ip=inet_ntoa($ip);
                   15040: 	$name_to_ip{$name}   = $ip;
                   15041: 	$lonid_to_ip{$lonid} = $ip;
                   15042: 	return $ip;
                   15043:     }
1.847     albertel 15044:     
                   15045:     sub get_iphost {
1.1293    raeburn  15046: 	my ($ignore_cache,$nocache) = @_;
1.894     albertel 15047: 
1.869     albertel 15048: 	if (!$ignore_cache) {
                   15049: 	    if (%iphost) {
                   15050: 		return %iphost;
                   15051: 	    }
                   15052: 	    my ($ip_info,$cached)=
                   15053: 		&Apache::lonnet::is_cached_new('iphost','iphost');
                   15054: 	    if ($cached) {
                   15055: 		%iphost      = %{$ip_info->[0]};
                   15056: 		%name_to_ip  = %{$ip_info->[1]};
                   15057: 		%lonid_to_ip = %{$ip_info->[2]};
                   15058: 		return %iphost;
                   15059: 	    }
                   15060: 	}
1.894     albertel 15061: 
                   15062: 	# get yesterday's info for fallback
                   15063: 	my %old_name_to_ip;
                   15064: 	my ($ip_info,$cached)=
                   15065: 	    &Apache::lonnet::is_cached_new('iphost','iphost');
                   15066: 	if ($cached) {
                   15067: 	    %old_name_to_ip = %{$ip_info->[1]};
                   15068: 	}
                   15069: 
1.1293    raeburn  15070: 	my %name_to_host = &all_names($ignore_cache,$nocache);
1.888     albertel 15071: 	foreach my $name (keys(%name_to_host)) {
1.847     albertel 15072: 	    my $ip;
                   15073: 	    if (!exists($name_to_ip{$name})) {
                   15074: 		$ip = gethostbyname($name);
                   15075: 		if (!$ip || length($ip) ne 4) {
1.894     albertel 15076: 		    if (defined($old_name_to_ip{$name})) {
                   15077: 			$ip = $old_name_to_ip{$name};
                   15078: 			&logthis("Can't find $name defaulting to old $ip");
                   15079: 		    } else {
                   15080: 			&logthis("Name $name no IP found");
                   15081: 			next;
                   15082: 		    }
                   15083: 		} else {
                   15084: 		    $ip=inet_ntoa($ip);
1.847     albertel 15085: 		}
                   15086: 		$name_to_ip{$name} = $ip;
                   15087: 	    } else {
                   15088: 		$ip = $name_to_ip{$name};
1.653     albertel 15089: 	    }
1.888     albertel 15090: 	    foreach my $id (@{ $name_to_host{$name} }) {
                   15091: 		$lonid_to_ip{$id} = $ip;
                   15092: 	    }
                   15093: 	    push(@{$iphost{$ip}},@{$name_to_host{$name}});
1.598     albertel 15094: 	}
1.1293    raeburn  15095:         unless ($nocache) {
                   15096: 	    &do_cache_new('iphost','iphost',
                   15097: 		          [\%iphost,\%name_to_ip,\%lonid_to_ip],
                   15098: 		          48*60*60);
                   15099:         }
1.869     albertel 15100: 
1.847     albertel 15101: 	return %iphost;
1.598     albertel 15102:     }
                   15103: 
1.992     raeburn  15104:     #
                   15105:     #  Given a DNS returns the loncapa host name for that DNS 
                   15106:     # 
                   15107:     sub host_from_dns {
                   15108:         my ($dns) = @_;
                   15109:         my @hosts;
                   15110:         my $ip;
                   15111: 
1.993     raeburn  15112:         if (exists($name_to_ip{$dns})) {
1.992     raeburn  15113:             $ip = $name_to_ip{$dns};
                   15114:         }
                   15115:         if (!$ip) {
                   15116:             $ip = gethostbyname($dns); # Initial translation to IP is in net order.
                   15117:             if (length($ip) == 4) { 
                   15118: 	        $ip   = &IO::Socket::inet_ntoa($ip);
                   15119:             }
                   15120:         }
                   15121:         if ($ip) {
                   15122: 	    @hosts = get_hosts_from_ip($ip);
                   15123: 	    return $hosts[0];
                   15124:         }
                   15125:         return undef;
1.986     foxr     15126:     }
1.992     raeburn  15127: 
1.1074    raeburn  15128:     sub get_internet_names {
                   15129:         my ($lonid) = @_;
                   15130:         return if ($lonid eq '');
                   15131:         my ($idnref,$cached)=
                   15132:             &Apache::lonnet::is_cached_new('internetnames',$lonid);
                   15133:         if ($cached) {
                   15134:             return $idnref;
                   15135:         }
                   15136:         my $ip = &get_host_ip($lonid);
                   15137:         my @hosts = &get_hosts_from_ip($ip);
                   15138:         my %iphost = &get_iphost();
                   15139:         my (@idns,%seen);
                   15140:         foreach my $id (@hosts) {
                   15141:             my $dom = &host_domain($id);
                   15142:             my $prim_id = &domain($dom,'primary');
                   15143:             my $prim_ip = &get_host_ip($prim_id);
                   15144:             next if ($seen{$prim_ip});
                   15145:             if (ref($iphost{$prim_ip}) eq 'ARRAY') {
                   15146:                 foreach my $id (@{$iphost{$prim_ip}}) {
                   15147:                     my $intdom = &internet_dom($id);
                   15148:                     unless (grep(/^\Q$intdom\E$/,@idns)) {
                   15149:                         push(@idns,$intdom);
                   15150:                     }
                   15151:                 }
                   15152:             }
                   15153:             $seen{$prim_ip} = 1;
                   15154:         }
1.1219    raeburn  15155:         return &do_cache_new('internetnames',$lonid,\@idns,12*60*60);
1.1074    raeburn  15156:     }
                   15157: 
1.986     foxr     15158: }
                   15159: 
1.1079    raeburn  15160: sub all_loncaparevs {
1.1249    raeburn  15161:     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  15162: }
                   15163: 
1.1227    raeburn  15164: # ---------------------------------------------------------- Read loncaparev table
                   15165: {
                   15166:     sub load_loncaparevs { 
                   15167:         if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
1.1359    raeburn  15168:             if (open(my $config,"<","$perlvar{'lonTabDir'}/loncaparevs.tab")) {
1.1227    raeburn  15169:                 while (my $configline=<$config>) {
                   15170:                     chomp($configline);
                   15171:                     my ($hostid,$loncaparev)=split(/:/,$configline);
                   15172:                     $loncaparevs{$hostid}=$loncaparev;
                   15173:                 }
                   15174:                 close($config);
                   15175:             }
                   15176:         }
                   15177:     }
                   15178: }
                   15179: 
                   15180: # ---------------------------------------------------------- Read serverhostID table
                   15181: {
                   15182:     sub load_serverhomeIDs {
                   15183:         if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
1.1359    raeburn  15184:             if (open(my $config,"<","$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
1.1227    raeburn  15185:                 while (my $configline=<$config>) {
                   15186:                     chomp($configline);
                   15187:                     my ($name,$id)=split(/:/,$configline);
                   15188:                     $serverhomeIDs{$name}=$id;
                   15189:                 }
                   15190:                 close($config);
                   15191:             }
                   15192:         }
                   15193:     }
                   15194: }
                   15195: 
                   15196: 
1.862     albertel 15197: BEGIN {
                   15198: 
                   15199: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
                   15200:     unless ($readit) {
                   15201: {
                   15202:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
                   15203:     %perlvar = (%perlvar,%{$configvars});
                   15204: }
                   15205: 
                   15206: 
1.1       albertel 15207: # ------------------------------------------------------ Read spare server file
                   15208: {
1.1359    raeburn  15209:     open(my $config,"<","$perlvar{'lonTabDir'}/spare.tab");
1.1       albertel 15210: 
                   15211:     while (my $configline=<$config>) {
                   15212:        chomp($configline);
1.284     matthew  15213:        if ($configline) {
1.784     albertel 15214: 	   my ($host,$type) = split(':',$configline,2);
1.785     albertel 15215: 	   if (!defined($type) || $type eq '') { $type = 'default' };
1.784     albertel 15216: 	   push(@{ $spareid{$type} }, $host);
1.1       albertel 15217:        }
                   15218:     }
1.448     albertel 15219:     close($config);
1.1       albertel 15220: }
1.11      www      15221: # ------------------------------------------------------------ Read permissions
                   15222: {
1.1359    raeburn  15223:     open(my $config,"<","$perlvar{'lonTabDir'}/roles.tab");
1.11      www      15224: 
                   15225:     while (my $configline=<$config>) {
1.448     albertel 15226: 	chomp($configline);
                   15227: 	if ($configline) {
                   15228: 	    my ($role,$perm)=split(/ /,$configline);
                   15229: 	    if ($perm ne '') { $pr{$role}=$perm; }
                   15230: 	}
1.11      www      15231:     }
1.448     albertel 15232:     close($config);
1.11      www      15233: }
                   15234: 
                   15235: # -------------------------------------------- Read plain texts for permissions
                   15236: {
1.1359    raeburn  15237:     open(my $config,"<","$perlvar{'lonTabDir'}/rolesplain.tab");
1.11      www      15238: 
                   15239:     while (my $configline=<$config>) {
1.448     albertel 15240: 	chomp($configline);
                   15241: 	if ($configline) {
1.742     raeburn  15242: 	    my ($short,@plain)=split(/:/,$configline);
                   15243:             %{$prp{$short}} = ();
                   15244: 	    if (@plain > 0) {
                   15245:                 $prp{$short}{'std'} = $plain[0];
                   15246:                 for (my $i=1; $i<@plain; $i++) {
                   15247:                     $prp{$short}{'alt'.$i} = $plain[$i];  
                   15248:                 }
                   15249:             }
1.448     albertel 15250: 	}
1.135     www      15251:     }
1.448     albertel 15252:     close($config);
1.135     www      15253: }
                   15254: 
                   15255: # ---------------------------------------------------------- Read package table
                   15256: {
1.1359    raeburn  15257:     open(my $config,"<","$perlvar{'lonTabDir'}/packages.tab");
1.135     www      15258: 
                   15259:     while (my $configline=<$config>) {
1.483     albertel 15260: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448     albertel 15261: 	chomp($configline);
                   15262: 	my ($short,$plain)=split(/:/,$configline);
                   15263: 	my ($pack,$name)=split(/\&/,$short);
                   15264: 	if ($plain ne '') {
                   15265: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
                   15266: 	    $packagetab{$short}=$plain; 
                   15267: 	}
1.11      www      15268:     }
1.448     albertel 15269:     close($config);
1.329     matthew  15270: }
                   15271: 
1.1073    raeburn  15272: # ---------------------------------------------------------- Read loncaparev table
1.1227    raeburn  15273: 
                   15274: &load_loncaparevs();
1.1073    raeburn  15275: 
1.1074    raeburn  15276: # ---------------------------------------------------------- Read serverhostID table
                   15277: 
1.1227    raeburn  15278: &load_serverhomeIDs();
                   15279: 
                   15280: # ---------------------------------------------------------- Read releaseslist XML
1.1079    raeburn  15281: {
                   15282:     my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
                   15283:     if (-e $file) {
                   15284:         my $parser = HTML::LCParser->new($file);
                   15285:         while (my $token = $parser->get_token()) {
                   15286:             if ($token->[0] eq 'S') {
                   15287:                 my $item = $token->[1];
                   15288:                 my $name = $token->[2]{'name'};
                   15289:                 my $value = $token->[2]{'value'};
1.1285    raeburn  15290:                 my $valuematch = $token->[2]{'valuematch'};
1.1303    raeburn  15291:                 my $namematch = $token->[2]{'namematch'};
                   15292:                 if ($item eq 'parameter') {
                   15293:                     if (($namematch ne '') || (($name ne '') && ($value ne '' || $valuematch ne ''))) {
                   15294:                         my $release = $parser->get_text();
                   15295:                         $release =~ s/(^\s*|\s*$ )//gx;
                   15296:                         $needsrelease{$item.':'.$name.':'.$value.':'.$valuematch.':'.$namematch} = $release;
                   15297:                     }
                   15298:                 } elsif ($item ne '' && $name ne '') {
1.1079    raeburn  15299:                     my $release = $parser->get_text();
                   15300:                     $release =~ s/(^\s*|\s*$ )//gx;
1.1303    raeburn  15301:                     $needsrelease{$item.':'.$name.':'.$value} = $release;
1.1079    raeburn  15302:                 }
                   15303:             }
                   15304:         }
                   15305:     }
1.1073    raeburn  15306: }
                   15307: 
1.1138    raeburn  15308: # ---------------------------------------------------------- Read managers table
                   15309: {
                   15310:     if (-e "$perlvar{'lonTabDir'}/managers.tab") {
1.1359    raeburn  15311:         if (open(my $config,"<","$perlvar{'lonTabDir'}/managers.tab")) {
1.1138    raeburn  15312:             while (my $configline=<$config>) {
                   15313:                 chomp($configline);
                   15314:                 next if ($configline =~ /^\#/);
                   15315:                 if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
                   15316:                     $managerstab{$configline} = 1;
                   15317:                 }
                   15318:             }
                   15319:             close($config);
                   15320:         }
                   15321:     }
                   15322: }
                   15323: 
1.329     matthew  15324: # ------------- set up temporary directory
                   15325: {
1.1117    foxr     15326:     $tmpdir = LONCAPA::tempdir();
1.329     matthew  15327: 
1.11      www      15328: }
                   15329: 
1.1405    raeburn  15330: # ------------- set default texengine (domain default overrides this)
                   15331: {
                   15332:     $deftex = LONCAPA::texengine();
                   15333: }
                   15334: 
1.1416    raeburn  15335: # ------------- set default minimum length for passwords for internal auth users
                   15336: {
                   15337:     $passwdmin = LONCAPA::passwd_min();
                   15338: }
                   15339: 
1.794     albertel 15340: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
                   15341: 				'compress_threshold'=> 20_000,
                   15342:  			        });
1.185     www      15343: 
1.281     www      15344: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186     www      15345: $dumpcount=0;
1.958     www      15346: $locknum=0;
1.22      www      15347: 
1.163     harris41 15348: &logtouch();
1.672     albertel 15349: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195     www      15350: $readit=1;
1.564     albertel 15351:     {
                   15352: 	use integer;
                   15353: 	my $test=(2**32)+1;
1.568     albertel 15354: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564     albertel 15355: 	&logthis(" Detected 64bit platform ($_64bit)");
                   15356:     }
1.195     www      15357: }
1.1       albertel 15358: }
1.179     www      15359: 
1.1       albertel 15360: 1;
1.191     harris41 15361: __END__
                   15362: 
1.243     albertel 15363: =pod
                   15364: 
1.191     harris41 15365: =head1 NAME
                   15366: 
1.243     albertel 15367: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191     harris41 15368: 
                   15369: =head1 SYNOPSIS
                   15370: 
1.243     albertel 15371: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191     harris41 15372: 
                   15373:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
                   15374: 
1.243     albertel 15375: Common parameters:
                   15376: 
                   15377: =over 4
                   15378: 
                   15379: =item *
                   15380: 
                   15381: $uname : an internal username (if $cname expecting a course Id specifically)
                   15382: 
                   15383: =item *
                   15384: 
                   15385: $udom : a domain (if $cdom expecting a course's domain specifically)
                   15386: 
                   15387: =item *
                   15388: 
                   15389: $symb : a resource instance identifier
                   15390: 
                   15391: =item *
                   15392: 
                   15393: $namespace : the name of a .db file that contains the data needed or
                   15394: being set.
                   15395: 
                   15396: =back
                   15397: 
1.394     bowersj2 15398: =head1 OVERVIEW
1.191     harris41 15399: 
1.394     bowersj2 15400: lonnet provides subroutines which interact with the
                   15401: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
                   15402: about classes, users, and resources.
1.243     albertel 15403: 
                   15404: For many of these objects you can also use this to store data about
                   15405: them or modify them in various ways.
1.191     harris41 15406: 
1.394     bowersj2 15407: =head2 Symbs
1.191     harris41 15408: 
1.394     bowersj2 15409: To identify a specific instance of a resource, LON-CAPA uses symbols
                   15410: or "symbs"X<symb>. These identifiers are built from the URL of the
                   15411: map, the resource number of the resource in the map, and the URL of
                   15412: the resource itself. The latter is somewhat redundant, but might help
                   15413: if maps change.
                   15414: 
                   15415: An example is
                   15416: 
                   15417:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
                   15418: 
                   15419: The respective map entry is
                   15420: 
                   15421:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
                   15422:   title="Problem 2">
                   15423:  </resource>
                   15424: 
                   15425: Symbs are used by the random number generator, as well as to store and
                   15426: restore data specific to a certain instance of for example a problem.
                   15427: 
                   15428: =head2 Storing And Retrieving Data
                   15429: 
                   15430: X<store()>X<cstore()>X<restore()>Three of the most important functions
                   15431: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
                   15432: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
                   15433: is is the non-critical message twin of cstore. These functions are for
                   15434: handlers to store a perl hash to a user's permanent data space in an
                   15435: easy manner, and to retrieve it again on another call. It is expected
                   15436: that a handler would use this once at the beginning to retrieve data,
                   15437: and then again once at the end to send only the new data back.
                   15438: 
                   15439: The data is stored in the user's data directory on the user's
                   15440: homeserver under the ID of the course.
                   15441: 
                   15442: The hash that is returned by restore will have all of the previous
                   15443: value for all of the elements of the hash.
                   15444: 
                   15445: Example:
                   15446: 
                   15447:  #creating a hash
                   15448:  my %hash;
                   15449:  $hash{'foo'}='bar';
                   15450: 
                   15451:  #storing it
                   15452:  &Apache::lonnet::cstore(\%hash);
                   15453: 
                   15454:  #changing a value
                   15455:  $hash{'foo'}='notbar';
                   15456: 
                   15457:  #adding a new value
                   15458:  $hash{'bar'}='foo';
                   15459:  &Apache::lonnet::cstore(\%hash);
                   15460: 
                   15461:  #retrieving the hash
                   15462:  my %history=&Apache::lonnet::restore();
                   15463: 
                   15464:  #print the hash
                   15465:  foreach my $key (sort(keys(%history))) {
                   15466:    print("\%history{$key} = $history{$key}");
                   15467:  }
                   15468: 
                   15469: Will print out:
1.191     harris41 15470: 
1.394     bowersj2 15471:  %history{1:foo} = bar
                   15472:  %history{1:keys} = foo:timestamp
                   15473:  %history{1:timestamp} = 990455579
                   15474:  %history{2:bar} = foo
                   15475:  %history{2:foo} = notbar
                   15476:  %history{2:keys} = foo:bar:timestamp
                   15477:  %history{2:timestamp} = 990455580
                   15478:  %history{bar} = foo
                   15479:  %history{foo} = notbar
                   15480:  %history{timestamp} = 990455580
                   15481:  %history{version} = 2
                   15482: 
                   15483: Note that the special hash entries C<keys>, C<version> and
                   15484: C<timestamp> were added to the hash. C<version> will be equal to the
                   15485: total number of versions of the data that have been stored. The
                   15486: C<timestamp> attribute will be the UNIX time the hash was
                   15487: stored. C<keys> is available in every historical section to list which
                   15488: keys were added or changed at a specific historical revision of a
                   15489: hash.
                   15490: 
                   15491: B<Warning>: do not store the hash that restore returns directly. This
                   15492: will cause a mess since it will restore the historical keys as if the
                   15493: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191     harris41 15494: 
1.394     bowersj2 15495: Calling convention:
1.191     harris41 15496: 
1.1225    raeburn  15497:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname);
1.1269    raeburn  15498:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$laststore);
1.191     harris41 15499: 
1.394     bowersj2 15500: For more detailed information, see lonnet specific documentation.
1.191     harris41 15501: 
1.394     bowersj2 15502: =head1 RETURN MESSAGES
1.191     harris41 15503: 
1.394     bowersj2 15504: =over 4
1.191     harris41 15505: 
1.394     bowersj2 15506: =item * B<con_lost>: unable to contact remote host
1.191     harris41 15507: 
1.394     bowersj2 15508: =item * B<con_delayed>: unable to contact remote host, message will be delivered
                   15509: when the connection is brought back up
1.191     harris41 15510: 
1.394     bowersj2 15511: =item * B<con_failed>: unable to contact remote host and unable to save message
                   15512: for later delivery
1.191     harris41 15513: 
1.967     bisitz   15514: =item * B<error:>: an error a occurred, a description of the error follows the :
1.191     harris41 15515: 
1.394     bowersj2 15516: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243     albertel 15517: that was requested
1.191     harris41 15518: 
1.243     albertel 15519: =back
1.191     harris41 15520: 
1.243     albertel 15521: =head1 PUBLIC SUBROUTINES
1.191     harris41 15522: 
1.243     albertel 15523: =head2 Session Environment Functions
1.191     harris41 15524: 
1.243     albertel 15525: =over 4
1.191     harris41 15526: 
1.394     bowersj2 15527: =item * 
                   15528: X<appenv()>
1.949     raeburn  15529: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
1.394     bowersj2 15530: the user envirnoment file, and will be restored for each access this
1.620     albertel 15531: user makes during this session, also modifies the %env for the current
1.949     raeburn  15532: process. Optional rolesarrayref - if defined contains a reference to an array
                   15533: of roles which are exempt from the restriction on modifying user.role entries 
                   15534: in the user's environment.db and in %env.    
1.191     harris41 15535: 
                   15536: =item *
1.394     bowersj2 15537: X<delenv()>
1.987     raeburn  15538: B<delenv($delthis,$regexp)>: removes all items from the session
                   15539: environment file that begin with $delthis. If the 
                   15540: optional second arg - $regexp - is true, $delthis is treated as a 
                   15541: regular expression, otherwise \Q$delthis\E is used. 
                   15542: The values are also deleted from the current processes %env.
1.191     harris41 15543: 
1.795     albertel 15544: =item * get_env_multiple($name) 
                   15545: 
                   15546: gets $name from the %env hash, it seemlessly handles the cases where multiple
                   15547: values may be defined and end up as an array ref.
                   15548: 
                   15549: returns an array of values
                   15550: 
1.243     albertel 15551: =back
                   15552: 
                   15553: =head2 User Information
1.191     harris41 15554: 
1.243     albertel 15555: =over 4
1.191     harris41 15556: 
                   15557: =item *
1.394     bowersj2 15558: X<queryauthenticate()>
                   15559: B<queryauthenticate($uname,$udom)>: try to determine user's current 
1.191     harris41 15560: authentication scheme
                   15561: 
                   15562: =item *
1.394     bowersj2 15563: X<authenticate()>
1.1073    raeburn  15564: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
1.394     bowersj2 15565: authenticate user from domain's lib servers (first use the current
                   15566: one). C<$upass> should be the users password.
1.1073    raeburn  15567: $checkdefauth is optional (value is 1 if a check should be made to
                   15568:    authenticate user using default authentication method, and allow
                   15569:    account creation if username does not have account in the domain).
                   15570: $clientcancheckhost is optional (value is 1 if checking whether the
                   15571:    server can host will occur on the client side in lonauth.pm).   
1.191     harris41 15572: 
                   15573: =item *
1.394     bowersj2 15574: X<homeserver()>
                   15575: B<homeserver($uname,$udom)>: find the server which has
                   15576: the user's directory and files (there must be only one), this caches
                   15577: the answer, and also caches if there is a borken connection.
1.191     harris41 15578: 
                   15579: =item *
1.394     bowersj2 15580: X<idget()>
1.1300    raeburn  15581: B<idget($udom,$idsref,$namespace)>: find the usernames behind either 
                   15582: a list of student/employee IDs or clicker IDs
                   15583: (student/employee IDs are a unique resource in a domain, there must be 
                   15584: only 1 ID per username, and only 1 username per ID in a specific domain).
                   15585: clickerIDs are not necessarily unique, as students might share clickers.
                   15586: (returns hash: id=>name,id=>name)
1.191     harris41 15587: 
                   15588: =item *
1.394     bowersj2 15589: X<idrget()>
                   15590: B<idrget($udom,@unames)>: find the IDs behind a list of
                   15591: usernames (returns hash: name=>id,name=>id)
1.191     harris41 15592: 
                   15593: =item *
1.394     bowersj2 15594: X<idput()>
1.1300    raeburn  15595: B<idput($udom,$idsref,$uhome,$namespace)>: store away a list of 
                   15596: names and associated student/employee IDs or clicker IDs.
                   15597: 
                   15598: =item *
                   15599: X<iddel()>
                   15600: B<iddel($udom,$idshashref,$uhome,$namespace)>: delete unwanted 
                   15601: student/employee ID or clicker ID username look-ups from domain.
                   15602: The homeserver ($uhome) and namespace ($namespace) are optional.
                   15603: If no $uhome is provided, it will be determined usig &homeserver()
                   15604: for each user.  If no $namespace is provided, the default is ids.
                   15605: 
                   15606: =item *
                   15607: X<updateclickers()>
                   15608: B<updateclickers($udom,$action,$idshashref,$uhome,$critical)>: update 
                   15609: clicker ID-to-username look-ups in clickers.db on library server.
                   15610: Permitted actions are add or del (i.e., add or delete). The 
                   15611: clickers.db contains clickerID as keys (escaped), and each corresponding
                   15612: value is an escaped comma-separated list of usernames (for whom the
                   15613: library server is the homeserver), who registered that particular ID.
                   15614: If $critical is true, the update will be sent via &critical, otherwise
                   15615: &reply() will be used.
1.191     harris41 15616: 
                   15617: =item *
1.394     bowersj2 15618: X<rolesinit()>
1.1169    droeschl 15619: B<rolesinit($udom,$username)>: get user privileges.
                   15620: returns user role, first access and timer interval hashes
1.243     albertel 15621: 
                   15622: =item *
1.1171    droeschl 15623: X<privileged()>
                   15624: B<privileged($username,$domain)>: returns a true if user has a
                   15625: privileged and active role (i.e. su or dc), false otherwise.
                   15626: 
                   15627: =item *
1.551     albertel 15628: X<getsection()>
                   15629: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243     albertel 15630: course $cname, return section name/number or '' for "not in course"
                   15631: and '-1' for "no section"
                   15632: 
                   15633: =item *
1.394     bowersj2 15634: X<userenvironment()>
                   15635: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243     albertel 15636: passed in @what from the requested user's environment, returns a hash
                   15637: 
1.858     raeburn  15638: =item * 
                   15639: X<userlog_query()>
1.859     albertel 15640: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
                   15641: activity.log file. %filters defines filters applied when parsing the
                   15642: log file. These can be start or end timestamps, or the type of action
                   15643: - log to look for Login or Logout events, check for Checkin or
                   15644: Checkout, role for role selection. The response is in the form
                   15645: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
                   15646: escaped strings of the action recorded in the activity.log file.
1.858     raeburn  15647: 
1.243     albertel 15648: =back
                   15649: 
                   15650: =head2 User Roles
                   15651: 
                   15652: =over 4
                   15653: 
                   15654: =item *
                   15655: 
1.1284    raeburn  15656: allowed($priv,$uri,$symb,$role,$clientip,$noblockcheck) : check for a user privilege; 
                   15657: returns codes for allowed actions.
                   15658: 
                   15659: The first argument is required, all others are optional.
                   15660: 
                   15661: $priv is the privilege being checked.
                   15662: $uri contains additional information about what is being checked for access (e.g.,
                   15663: URL, course ID etc.). 
                   15664: $symb is the unique resource instance identifier in a course; if needed,
                   15665: but not provided, it will be retrieved via a call to &symbread(). 
                   15666: $role is the role for which a priv is being checked (only used if priv is evb). 
                   15667: $clientip is the user's IP address (only used when checking for access to portfolio 
                   15668: files).
                   15669: $noblockcheck, if true, skips calls to &has_comm_blocking() for the bre priv. This 
                   15670: prevents recursive calls to &allowed.
                   15671: 
1.243     albertel 15672:  F: full access
                   15673:  U,I,K: authentication modes (cxx only)
                   15674:  '': forbidden
                   15675:  1: user needs to choose course
                   15676:  2: browse allowed
1.766     albertel 15677:  A: passphrase authentication needed
1.1284    raeburn  15678:  B: access temporarily blocked because of a blocking event in a course.
1.1402    raeburn  15679:  D: access blocked because access is required via session initiated via deep-link 
1.243     albertel 15680: 
                   15681: =item *
                   15682: 
1.1192    raeburn  15683: constructaccess($url,$setpriv) : check for access to construction space URL
                   15684: 
                   15685: See if the owner domain and name in the URL match those in the
                   15686: expected environment.  If so, return three element list
                   15687: ($ownername,$ownerdomain,$ownerhome).
                   15688: 
                   15689: Otherwise return the null string.
                   15690: 
                   15691: If second argument 'setpriv' is true, it assigns the privileges,
                   15692: and returns the same three element list, unless the owner has
                   15693: blocked "ad hoc" Domain Coordinator access to the Author Space,
                   15694: in which case the null string is returned.
                   15695: 
                   15696: =item *
                   15697: 
1.1326    raeburn  15698: definerole($rolename,$sysrole,$domrole,$courole,$uname,$udom) : define role;
                   15699: define a custom role rolename set privileges in format of lonTabs/roles.tab
                   15700: for system, domain, and course level. $uname and $udom are optional (current
                   15701: user's username and domain will be used when either of $uname or $udom are absent.
1.243     albertel 15702: 
                   15703: =item *
                   15704: 
1.988     raeburn  15705: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash 
                   15706: (rolesplain.tab); plain text explanation of a user role term.
1.1008    raeburn  15707: $type is Course (default) or Community.
1.988     raeburn  15708: If $forcedefault evaluates to true, text returned will be default 
                   15709: text for $type. Otherwise, if this is a course, the text returned 
                   15710: will be a custom name for the role (if defined in the course's 
                   15711: environment).  If no custom name is defined the default is returned.
                   15712:    
1.832     raeburn  15713: =item *
                   15714: 
1.1219    raeburn  15715: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv) :
1.858     raeburn  15716: All arguments are optional. Returns a hash of a roles, either for
                   15717: co-author/assistant author roles for a user's Construction Space
1.906     albertel 15718: (default), or if $context is 'userroles', roles for the user himself,
1.933     raeburn  15719: In the hash, keys are set to colon-separated $uname,$udom,$role, and
                   15720: (optionally) if $withsec is true, a fourth colon-separated item - $section.
                   15721: For each key, value is set to colon-separated start and end times for
                   15722: the role.  If no username and domain are specified, will default to
1.934     raeburn  15723: current user/domain. Types, roles, and roledoms are references to arrays
1.858     raeburn  15724: of role statuses (active, future or previous), roles 
                   15725: (e.g., cc,in, st etc.) and domains of the roles which can be used
                   15726: to restrict the list of roles reported. If no array ref is 
                   15727: provided for types, will default to return only active roles.
1.834     albertel 15728: 
1.1195    raeburn  15729: =item *
                   15730: 
                   15731: in_course($udom,$uname,$cdom,$cnum,$type,$hideprivileged) : determine if
1.1196    raeburn  15732: user: $uname:$udom has a role in the course: $cdom_$cnum. 
                   15733: 
                   15734: Additional optional arguments are: $type (if role checking is to be restricted 
                   15735: to certain user status types -- previous (expired roles), active (currently
1.1195    raeburn  15736: available roles) or future (roles available in the future), and
                   15737: $hideprivileged -- if true will not report course roles for users who
1.1219    raeburn  15738: have active Domain Coordinator role in course's domain or in additional
                   15739: domains (specified in 'Domains to check for privileged users' in course
                   15740: environment -- set via:  Course Settings -> Classlists and staff listing).
                   15741: 
                   15742: =item *
                   15743: 
                   15744: privileged($username,$domain,$possdomains,$possroles) : returns 1 if user
                   15745: $username:$domain is a privileged user (e.g., Domain Coordinator or Super User)
                   15746: $possdomains and $possroles are optional array refs -- to domains to check and
                   15747: roles to check.  If $possdomains is not specified, a dump will be done of the
                   15748: users' roles.db to check for a dc or su role in any domain. This can be
                   15749: time consuming if &privileged is called repeatedly (e.g., when displaying a
                   15750: classlist), so in such cases, supplying a $possdomains array is preferred, as
                   15751: this then allows &privileged_by_domain() to be used, which caches the identity
                   15752: of privileged users, eliminating the need for repeated calls to &dump().
                   15753: 
                   15754: =item *
                   15755: 
                   15756: privileged_by_domain($possdomains,$roles) : returns a hash of a hash of a hash,
                   15757: where the outer hash keys are domains specified in the $possdomains array ref,
                   15758: next inner hash keys are privileged roles specified in the $roles array ref,
                   15759: and the innermost hash contains key = value pairs for username:domain = end:start
                   15760: for active or future "privileged" users with that role in that domain. To avoid
                   15761: repeated dumps of domain roles -- via &get_domain_roles() -- contents of the
                   15762: innerhash are cached using priv_$role and $dom as the identifiers.
1.1195    raeburn  15763: 
1.243     albertel 15764: =back
                   15765: 
                   15766: =head2 User Modification
                   15767: 
                   15768: =over 4
                   15769: 
                   15770: =item *
                   15771: 
1.957     raeburn  15772: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
1.243     albertel 15773: user for the level given by URL.  Optional start and end dates (leave empty
                   15774: string or zero for "no date")
1.191     harris41 15775: 
                   15776: =item *
                   15777: 
1.243     albertel 15778: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
                   15779: change a users, password, possible return values are: ok,
                   15780: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
                   15781: refused
1.191     harris41 15782: 
                   15783: =item *
                   15784: 
1.243     albertel 15785: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191     harris41 15786: 
                   15787: =item *
                   15788: 
1.1058    raeburn  15789: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
                   15790:            $forceid,$desiredhome,$email,$inststatus,$candelete) :
                   15791: 
                   15792: will update user information (firstname,middlename,lastname,generation,
                   15793: permanentemail), and if forceid is true, student/employee ID also.
                   15794: A user's institutional affiliation(s) can also be updated.
                   15795: User information fields will not be overwritten with empty entries 
                   15796: unless the field is included in the $candelete array reference.
                   15797: This array is included when a single user is modified via "Manage Users",
                   15798: or when Autoupdate.pl is run by cron in a domain.
1.191     harris41 15799: 
                   15800: =item *
                   15801: 
1.286     matthew  15802: modifystudent
                   15803: 
1.957     raeburn  15804: modify a student's enrollment and identification information.
1.1235    raeburn  15805: The course id is resolved based on the current user's environment.  
                   15806: This means the invoking user must be a course coordinator or otherwise
1.286     matthew  15807: associated with a course.
                   15808: 
1.297     matthew  15809: This call is essentially a wrapper for lonnet::modifyuser and
                   15810: lonnet::modify_student_enrollment
1.286     matthew  15811: 
                   15812: Inputs: 
                   15813: 
                   15814: =over 4
                   15815: 
1.957     raeburn  15816: =item B<$udom> Student's loncapa domain
1.286     matthew  15817: 
1.957     raeburn  15818: =item B<$uname> Student's loncapa login name
1.286     matthew  15819: 
1.964     bisitz   15820: =item B<$uid> Student/Employee ID
1.286     matthew  15821: 
1.957     raeburn  15822: =item B<$umode> Student's authentication mode
1.286     matthew  15823: 
1.957     raeburn  15824: =item B<$upass> Student's password
1.286     matthew  15825: 
1.957     raeburn  15826: =item B<$first> Student's first name
1.286     matthew  15827: 
1.957     raeburn  15828: =item B<$middle> Student's middle name
1.286     matthew  15829: 
1.957     raeburn  15830: =item B<$last> Student's last name
1.286     matthew  15831: 
1.957     raeburn  15832: =item B<$gene> Student's generation
1.286     matthew  15833: 
1.957     raeburn  15834: =item B<$usec> Student's section in course
1.286     matthew  15835: 
                   15836: =item B<$end> Unix time of the roles expiration
                   15837: 
                   15838: =item B<$start> Unix time of the roles start date
                   15839: 
                   15840: =item B<$forceid> If defined, allow $uid to be changed
                   15841: 
                   15842: =item B<$desiredhome> server to use as home server for student
                   15843: 
1.957     raeburn  15844: =item B<$email> Student's permanent e-mail address
                   15845: 
                   15846: =item B<$type> Type of enrollment (auto or manual)
                   15847: 
1.963     raeburn  15848: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto    
                   15849: 
                   15850: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
1.957     raeburn  15851: 
1.963     raeburn  15852: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
1.957     raeburn  15853: 
1.963     raeburn  15854: =item B<$context> role change context (shown in User Management Logs display in a course)
1.957     raeburn  15855: 
1.1216    raeburn  15856: =item B<$inststatus> institutional status of user - : separated string of escaped status types
                   15857: 
                   15858: =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  15859: 
1.286     matthew  15860: =back
1.297     matthew  15861: 
                   15862: =item *
                   15863: 
                   15864: modify_student_enrollment
                   15865: 
1.1235    raeburn  15866: Change a student's enrollment status in a class.  The environment variable
1.297     matthew  15867: 'role.request.course' must be defined for this function to proceed.
                   15868: 
                   15869: Inputs:
                   15870: 
                   15871: =over 4
                   15872: 
1.1235    raeburn  15873: =item $udom, student's domain
1.297     matthew  15874: 
1.1235    raeburn  15875: =item $uname, student's name
1.297     matthew  15876: 
1.1235    raeburn  15877: =item $uid, student's user id
1.297     matthew  15878: 
1.1235    raeburn  15879: =item $first, student's first name
1.297     matthew  15880: 
                   15881: =item $middle
                   15882: 
                   15883: =item $last
                   15884: 
                   15885: =item $gene
                   15886: 
                   15887: =item $usec
                   15888: 
                   15889: =item $end
                   15890: 
                   15891: =item $start
                   15892: 
1.957     raeburn  15893: =item $type
                   15894: 
                   15895: =item $locktype
                   15896: 
                   15897: =item $cid
                   15898: 
                   15899: =item $selfenroll
                   15900: 
                   15901: =item $context
                   15902: 
1.1216    raeburn  15903: =item $credits, number of credits student will earn from this class
                   15904: 
1.1314    raeburn  15905: =item $instsec, institutional course section code for student
                   15906: 
1.297     matthew  15907: =back
                   15908: 
1.191     harris41 15909: 
                   15910: =item *
                   15911: 
1.243     albertel 15912: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
                   15913: custom role; give a custom role to a user for the level given by URL.  Specify
                   15914: name and domain of role author, and role name
1.191     harris41 15915: 
                   15916: =item *
                   15917: 
1.243     albertel 15918: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191     harris41 15919: 
                   15920: =item *
                   15921: 
1.243     albertel 15922: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
                   15923: 
                   15924: =back
                   15925: 
                   15926: =head2 Course Infomation
                   15927: 
                   15928: =over 4
1.191     harris41 15929: 
                   15930: =item *
                   15931: 
1.1118    foxr     15932: coursedescription($courseid,$options) : returns a hash of information about the
1.631     albertel 15933: specified course id, including all environment settings for the
                   15934: course, the description of the course will be in the hash under the
                   15935: key 'description'
1.191     harris41 15936: 
1.1118    foxr     15937: $options is an optional parameter that if supplied is a hash reference that controls
                   15938: what how this function works.  It has the following key/values:
                   15939: 
                   15940: =over 4
                   15941: 
                   15942: =item freshen_cache
                   15943: 
                   15944: If defined, and the environment cache for the course is valid, it is 
                   15945: returned in the returned hash.
                   15946: 
                   15947: =item one_time
                   15948: 
                   15949: If defined, the last cache time is set to _now_
                   15950: 
                   15951: =item user
                   15952: 
                   15953: If defined, the supplied username is used instead of the current user.
                   15954: 
                   15955: 
                   15956: =back
                   15957: 
1.191     harris41 15958: =item *
                   15959: 
1.624     albertel 15960: resdata($name,$domain,$type,@which) : request for current parameter
                   15961: setting for a specific $type, where $type is either 'course' or 'user',
                   15962: @what should be a list of parameters to ask about. This routine caches
1.1235    raeburn  15963: answers for 10 minutes.
1.243     albertel 15964: 
1.877     foxr     15965: =item *
                   15966: 
                   15967: get_courseresdata($courseid, $domain) : dump the entire course resource
                   15968: data base, returning a hash that is keyed by the resource name and has
                   15969: values that are the resource value.  I believe that the timestamps and
                   15970: versions are also returned.
                   15971: 
1.1236    raeburn  15972: get_numsuppfiles($cnum,$cdom) : retrieve number of files in a course's
                   15973: supplemental content area. This routine caches the number of files for 
                   15974: 10 minutes.
                   15975: 
1.243     albertel 15976: =back
                   15977: 
                   15978: =head2 Course Modification
                   15979: 
                   15980: =over 4
1.191     harris41 15981: 
                   15982: =item *
                   15983: 
1.243     albertel 15984: writecoursepref($courseid,%prefs) : write preferences (environment
                   15985: database) for a course
1.191     harris41 15986: 
                   15987: =item *
                   15988: 
1.1011    raeburn  15989: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
                   15990: 
                   15991: =item *
                   15992: 
1.1038    raeburn  15993: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
1.243     albertel 15994: 
1.1167    droeschl 15995: =item *
                   15996: 
                   15997: is_course($courseid), is_course($cdom, $cnum)
                   15998: 
                   15999: Accepts either a combined $courseid (in the form of domain_courseid) or the
                   16000: two component version $cdom, $cnum. It checks if the specified course exists.
                   16001: 
                   16002: Returns:
                   16003:     undef if the course doesn't exist, otherwise
                   16004:     in scalar context the combined courseid.
                   16005:     in list context the two components of the course identifier, domain and 
                   16006:     courseid.    
                   16007: 
1.243     albertel 16008: =back
                   16009: 
1.1401    raeburn  16010: =head2 Bubblesheet Configuration
                   16011: 
                   16012: =over 4
                   16013: 
                   16014: =item *
                   16015: 
                   16016: get_scantron_config($which)
                   16017: 
                   16018: $which - the name of the configuration to parse from the file.
                   16019: 
                   16020: Parses and returns the bubblesheet configuration line selected as a
                   16021: hash of configuration file fields.
                   16022: 
                   16023: 
                   16024: Returns:
                   16025:     If the named configuration is not in the file, an empty
                   16026:     hash is returned.
                   16027: 
                   16028:     a hash with the fields
                   16029:       name         - internal name for the this configuration setup
                   16030:       description  - text to display to operator that describes this config
                   16031:       CODElocation - if 0 or the string 'none'
                   16032:                           - no CODE exists for this config
                   16033:                      if -1 || the string 'letter'
                   16034:                           - a CODE exists for this config and is
                   16035:                             a string of letters
                   16036:                      Unsupported value (but planned for future support)
                   16037:                           if a positive integer
                   16038:                                - The CODE exists as the first n items from
                   16039:                                  the question section of the form
                   16040:                           if the string 'number'
                   16041:                                - The CODE exists for this config and is
                   16042:                                  a string of numbers
                   16043:       CODEstart   - (only matter if a CODE exists) column in the line where
                   16044:                      the CODE starts
                   16045:       CODElength  - length of the CODE
                   16046:       IDstart     - column where the student/employee ID starts
                   16047:       IDlength    - length of the student/employee ID info
                   16048:       Qstart      - column where the information from the bubbled
                   16049:                     'questions' start
                   16050:       Qlength     - number of columns comprising a single bubble line from
                   16051:                     the sheet. (usually either 1 or 10)
                   16052:       Qon         - either a single character representing the character used
                   16053:                     to signal a bubble was chosen in the positional setup, or
                   16054:                     the string 'letter' if the letter of the chosen bubble is
                   16055:                     in the final, or 'number' if a number representing the
                   16056:                     chosen bubble is in the file (1->A 0->J)
                   16057:       Qoff        - the character used to represent that a bubble was
                   16058:                     left blank
                   16059:       PaperID     - if the scanning process generates a unique number for each
                   16060:                     sheet scanned the column that this ID number starts in
                   16061:       PaperIDlength - number of columns that comprise the unique ID number
                   16062:                       for the sheet of paper
                   16063:       FirstName   - column that the first name starts in
                   16064:       FirstNameLength - number of columns that the first name spans
                   16065:       LastName    - column that the last name starts in
                   16066:       LastNameLength - number of columns that the last name spans
                   16067:       BubblesPerRow - number of bubbles available in each row used to
                   16068:                       bubble an answer. (If not specified, 10 assumed).
                   16069: 
                   16070: 
                   16071: =item *
                   16072: 
                   16073: get_scantronformat_file($cdom)
                   16074: 
                   16075: $cdom - the course's domain (optional); if not supplied, uses
                   16076: domain for current $env{'request.course.id'}.
                   16077: 
                   16078: Returns an array containing lines from the scantron format file for
                   16079: the domain of the course.
                   16080: 
                   16081: If a url for a custom.tab file is listed in domain's configuration.db,
                   16082: lines are from this file.
                   16083: 
                   16084: Otherwise, if a default.tab has been published in RES space by the
                   16085: domainconfig user, lines are from this file.
                   16086: 
                   16087: Otherwise, fall back to getting lines from the legacy file on the
                   16088: local server:  /home/httpd/lonTabs/default_scantronformat.tab
                   16089: 
                   16090: =back
                   16091: 
1.243     albertel 16092: =head2 Resource Subroutines
                   16093: 
                   16094: =over 4
1.191     harris41 16095: 
                   16096: =item *
                   16097: 
1.243     albertel 16098: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191     harris41 16099: 
                   16100: =item *
                   16101: 
1.243     albertel 16102: repcopy($filename) : subscribes to the requested file, and attempts to
                   16103: replicate from the owning library server, Might return
1.607     raeburn  16104: 'unavailable', 'not_found', 'forbidden', 'ok', or
                   16105: 'bad_request', also attempts to grab the metadata for the
1.243     albertel 16106: resource. Expects the local filesystem pathname
                   16107: (/home/httpd/html/res/....)
                   16108: 
                   16109: =back
                   16110: 
                   16111: =head2 Resource Information
                   16112: 
                   16113: =over 4
1.191     harris41 16114: 
                   16115: =item *
                   16116: 
1.1228    raeburn  16117: EXT($varname,$symb,$udom,$uname,$usection,$recurse,$cid) : evaluates 
                   16118: and returns the value of a variety of different possible values,
                   16119: $varname should be a request string, and the other parameters can be
                   16120: used to specify who and what one is asking about. Ordinarily, $cid 
                   16121: does not need to be specified, as it is retrived from 
                   16122: $env{'request.course.id'}, but &Apache::lonnet::EXT() is called
                   16123: within lonuserstate::loadmap() when initializing a course, before
                   16124: $env{'request.course.id'} has been set, so it needs to be provided
                   16125: in that one case.
1.243     albertel 16126: 
                   16127: Possible values for $varname are environment.lastname (or other item
                   16128: from the envirnment hash), user.name (or someother aspect about the
                   16129: user), resource.0.maxtries (or some other part and parameter of a
                   16130: resource)
1.204     albertel 16131: 
                   16132: =item *
                   16133: 
1.243     albertel 16134: directcondval($number) : get current value of a condition; reads from a state
                   16135: string
1.204     albertel 16136: 
                   16137: =item *
                   16138: 
1.243     albertel 16139: condval($condidx) : value of condition index based on state
1.204     albertel 16140: 
                   16141: =item *
                   16142: 
1.1362    raeburn  16143: metadata($uri,$what,$toolsymb,$liburi,$prefix,$depthcount) : request a
1.243     albertel 16144: resource's metadata, $what should be either a specific key, or either
                   16145: 'keys' (to get a list of possible keys) or 'packages' to get a list of
1.1362    raeburn  16146: packages that this resource currently uses, the last 3 arguments are 
                   16147: only used internally for recursive metadata.
                   16148: 
                   16149: the toolsymb is only used where the uri is for an external tool (for which
                   16150: the uri as well as the symb are guaranteed to be unique).
1.243     albertel 16151: 
1.1371    raeburn  16152: this function automatically caches all requests except any made recursively
                   16153: to retrieve a list of metadata keys for an imported library file ($liburi is 
                   16154: defined).
1.191     harris41 16155: 
                   16156: =item *
                   16157: 
1.243     albertel 16158: metadata_query($query,$custom,$customshow) : make a metadata query against the
                   16159: network of library servers; returns file handle of where SQL and regex results
                   16160: will be stored for query
1.191     harris41 16161: 
                   16162: =item *
                   16163: 
1.1282    raeburn  16164: symbread($filename,$donotrecurse,$ignorecachednull,$checkforblock,$possibles) : 
                   16165: return symbolic list entry (all arguments optional). 
                   16166: 
                   16167: Args: filename is the filename (including path) for the file for which a symb 
                   16168: is required; donotrecurse, if true will prevent calls to allowed() being made 
                   16169: to check access status if more than one resource was found in the bighash 
                   16170: (see rev. 1.249) to avoid an infinite loop if an ambiguous resource is part of 
                   16171: a randompick); ignorecachednull, if true will prevent a symb of '' being 
                   16172: returned if $env{$cache_str} is defined as ''; checkforblock if true will
                   16173: cause possible symbs to be checked to determine if they are subject to content
                   16174: blocking, if so they will not be included as possible symbs; possibles is a
                   16175: ref to a hash, which, as a side effect, will be populated with all possible 
                   16176: symbs (content blocking not tested).
                   16177:  
1.243     albertel 16178: returns the data handle
1.191     harris41 16179: 
                   16180: =item *
                   16181: 
1.1190    raeburn  16182: symbverify($symb,$thisfn,$encstate) : verifies that $symb actually exists
                   16183: and is a possible symb for the URL in $thisfn, and if is an encrypted
1.582     albertel 16184: resource that the user accessed using /enc/ returns a 1 on success, 0
1.1190    raeburn  16185: on failure, user must be in a course, as it assumes the existence of
                   16186: the course initial hash, and uses $env('request.course.id'}.  The third
                   16187: arg is an optional reference to a scalar.  If this arg is passed in the 
                   16188: call to symbverify, it will be set to 1 if the symb has been set to be 
                   16189: encrypted; otherwise it will be null.  
1.191     harris41 16190: 
                   16191: =item *
                   16192: 
1.243     albertel 16193: symbclean($symb) : removes versions numbers from a symb, returns the
                   16194: cleaned symb
1.191     harris41 16195: 
                   16196: =item *
                   16197: 
1.243     albertel 16198: is_on_map($uri) : checks if the $uri is somewhere on the current
                   16199: course map, user must be in a course for it to work.
1.191     harris41 16200: 
                   16201: =item *
                   16202: 
1.243     albertel 16203: numval($salt) : return random seed value (addend for rndseed)
1.191     harris41 16204: 
                   16205: =item *
                   16206: 
1.243     albertel 16207: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
                   16208: a random seed, all arguments are optional, if they aren't sent it uses the
                   16209: environment to derive them. Note: if symb isn't sent and it can't get one
                   16210: from &symbread it will use the current time as its return value
1.191     harris41 16211: 
                   16212: =item *
                   16213: 
1.243     albertel 16214: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
                   16215: unfakeable, receipt
1.191     harris41 16216: 
                   16217: =item *
                   16218: 
1.620     albertel 16219: receipt() : API to ireceipt working off of env values; given out to users
1.191     harris41 16220: 
                   16221: =item *
                   16222: 
1.243     albertel 16223: countacc($url) : count the number of accesses to a given URL
1.191     harris41 16224: 
                   16225: =item *
                   16226: 
1.243     albertel 16227: 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 16228: 
                   16229: =item *
                   16230: 
1.243     albertel 16231: 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 16232: 
                   16233: =item *
                   16234: 
1.243     albertel 16235: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191     harris41 16236: 
                   16237: =item *
                   16238: 
1.243     albertel 16239: devalidate($symb) : devalidate temporary spreadsheet calculations,
                   16240: forcing spreadsheet to reevaluate the resource scores next time.
                   16241: 
1.1195    raeburn  16242: =item * 
                   16243: 
1.1196    raeburn  16244: can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group) : determine if current user can edit a particular resource,
                   16245: when viewing in course context.
1.1195    raeburn  16246: 
1.1196    raeburn  16247:  input: six args -- filename (decluttered), course number, course domain,
                   16248:                     url, symb (if registered) and group (if this is a 
                   16249:                     group item -- e.g., bulletin board, group page etc.).
1.1195    raeburn  16250: 
1.1196    raeburn  16251:  output: array of five scalars --
1.1195    raeburn  16252:          $cfile -- url for file editing if editable on current server
                   16253:          $home -- homeserver of resource (i.e., for author if published,
                   16254:                                           or course if uploaded.).
                   16255:          $switchserver --  1 if server switch will be needed.
1.1196    raeburn  16256:          $forceedit -- 1 if icon/link should be to go to edit mode 
                   16257:          $forceview -- 1 if icon/link should be to go to view mode
1.1195    raeburn  16258: 
                   16259: =item *
                   16260: 
                   16261: is_course_upload($file,$cnum,$cdom)
                   16262: 
                   16263: Used in course context to determine if current file was uploaded to 
                   16264: the course (i.e., would be found in /userfiles/docs on the course's 
                   16265: homeserver.
                   16266: 
                   16267:   input: 3 args -- filename (decluttered), course number and course domain.
                   16268:   output: boolean -- 1 if file was uploaded.
                   16269: 
1.243     albertel 16270: =back
                   16271: 
                   16272: =head2 Storing/Retreiving Data
                   16273: 
                   16274: =over 4
1.191     harris41 16275: 
                   16276: =item *
                   16277: 
1.1269    raeburn  16278: store($storehash,$symb,$namespace,$udom,$uname,$laststore) : stores hash
                   16279: permanently for this url; hashref needs to be given and should be a \%hashname;
                   16280: the remaining args aren't required and if they aren't passed or are '' they will
                   16281: be derived from the env (with the exception of $laststore, which is an 
                   16282: optional arg used when a user's submission is stored in grading).
                   16283: $laststore is $version=$timestamp, where $version is the most recent version
                   16284: number retrieved for the corresponding $symb in the $namespace db file, and
                   16285: $timestamp is the timestamp for that transaction (UNIX time).
                   16286: $laststore is currently only passed when cstore() is called by 
                   16287: structuretags::finalize_storage().
1.191     harris41 16288: 
                   16289: =item *
                   16290: 
1.1269    raeburn  16291: cstore($storehash,$symb,$namespace,$udom,$uname,$laststore) : same as store
                   16292: but uses critical subroutine
1.191     harris41 16293: 
                   16294: =item *
                   16295: 
1.243     albertel 16296: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
                   16297: all args are optional
1.191     harris41 16298: 
                   16299: =item *
                   16300: 
1.717     albertel 16301: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
                   16302: dumps the complete (or key matching regexp) namespace into a hash
                   16303: ($udom, $uname, $regexp, $range are optional) for a namespace that is
                   16304: normally &store()ed into
                   16305: 
                   16306: $range should be either an integer '100' (give me the first 100
                   16307:                                            matching records)
                   16308:               or be  two integers sperated by a - with no spaces
                   16309:                  '30-50' (give me the 30th through the 50th matching
                   16310:                           records)
                   16311: 
                   16312: 
                   16313: =item *
                   16314: 
1.1269    raeburn  16315: putstore($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog) :
1.717     albertel 16316: replaces a &store() version of data with a replacement set of data
                   16317: for a particular resource in a namespace passed in the $storehash hash 
1.1269    raeburn  16318: reference. If $tolog is true, the transaction is logged in the courselog
                   16319: with an action=PUTSTORE.
1.717     albertel 16320: 
                   16321: =item *
                   16322: 
1.243     albertel 16323: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
                   16324: works very similar to store/cstore, but all data is stored in a
                   16325: temporary location and can be reset using tmpreset, $storehash should
                   16326: be a hash reference, returns nothing on success
1.191     harris41 16327: 
                   16328: =item *
                   16329: 
1.243     albertel 16330: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
                   16331: similar to restore, but all data is stored in a temporary location and
                   16332: can be reset using tmpreset. Returns a hash of values on success,
                   16333: error string otherwise.
1.191     harris41 16334: 
                   16335: =item *
                   16336: 
1.243     albertel 16337: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
                   16338: deltes all keys for $symb form the temporary storage hash.
1.191     harris41 16339: 
                   16340: =item *
                   16341: 
1.243     albertel 16342: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   16343: reference filled in from namesp ($udom and $uname are optional)
1.191     harris41 16344: 
                   16345: =item *
                   16346: 
1.243     albertel 16347: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
                   16348: namesp ($udom and $uname are optional)
1.191     harris41 16349: 
                   16350: =item *
                   16351: 
1.702     albertel 16352: dump($namespace,$udom,$uname,$regexp,$range) : 
1.243     albertel 16353: dumps the complete (or key matching regexp) namespace into a hash
1.702     albertel 16354: ($udom, $uname, $regexp, $range are optional)
1.449     matthew  16355: 
1.702     albertel 16356: $range should be either an integer '100' (give me the first 100
                   16357:                                            matching records)
                   16358:               or be  two integers sperated by a - with no spaces
                   16359:                  '30-50' (give me the 30th through the 50th matching
                   16360:                           records)
1.449     matthew  16361: =item *
                   16362: 
                   16363: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
                   16364: $store can be a scalar, an array reference, or if the amount to be 
                   16365: incremented is > 1, a hash reference.
                   16366: 
                   16367: ($udom and $uname are optional)
1.191     harris41 16368: 
                   16369: =item *
                   16370: 
1.243     albertel 16371: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
                   16372: ($udom and $uname are optional)
1.191     harris41 16373: 
                   16374: =item *
                   16375: 
1.243     albertel 16376: cput($namespace,$storehash,$udom,$uname) : critical put
                   16377: ($udom and $uname are optional)
1.191     harris41 16378: 
                   16379: =item *
                   16380: 
1.748     albertel 16381: newput($namespace,$storehash,$udom,$uname) :
                   16382: 
                   16383: Attempts to store the items in the $storehash, but only if they don't
                   16384: currently exist, if this succeeds you can be certain that you have 
                   16385: successfully created a new key value pair in the $namespace db.
                   16386: 
                   16387: 
                   16388: Args:
                   16389:  $namespace: name of database to store values to
                   16390:  $storehash: hashref to store to the db
                   16391:  $udom: (optional) domain of user containing the db
                   16392:  $uname: (optional) name of user caontaining the db
                   16393: 
                   16394: Returns:
                   16395:  'ok' -> succeeded in storing all keys of $storehash
                   16396:  'key_exists: <key>' -> failed to anything out of $storehash, as at
                   16397:                         least <key> already existed in the db (other
                   16398:                         requested keys may also already exist)
1.967     bisitz   16399:  'error: <msg>' -> unable to tie the DB or other error occurred
1.748     albertel 16400:  'con_lost' -> unable to contact request server
                   16401:  'refused' -> action was not allowed by remote machine
                   16402: 
                   16403: 
                   16404: =item *
                   16405: 
1.243     albertel 16406: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   16407: reference filled in from namesp (encrypts the return communication)
                   16408: ($udom and $uname are optional)
1.191     harris41 16409: 
                   16410: =item *
                   16411: 
1.243     albertel 16412: log($udom,$name,$home,$message) : write to permanent log for user; use
                   16413: critical subroutine
                   16414: 
1.806     raeburn  16415: =item *
                   16416: 
1.860     raeburn  16417: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
                   16418: array reference filled in from namespace found in domain level on either
                   16419: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806     raeburn  16420: 
                   16421: =item *
                   16422: 
1.860     raeburn  16423: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
                   16424: domain level either on specified domain server ($uhome) or primary domain 
                   16425: server ($udom and $uhome are optional)
1.806     raeburn  16426: 
1.943     raeburn  16427: =item * 
                   16428: 
1.1240    raeburn  16429: get_domain_defaults($target_domain,$ignore_cache) : returns hash with defaults 
                   16430: for: authentication, language, quotas, timezone, date locale, and portal URL in
                   16431: the target domain.
                   16432: 
                   16433: May also include additional key => value pairs for the following groups:
                   16434: 
                   16435: =over
                   16436: 
                   16437: =item
                   16438: disk quotas (MB allocated by default to portfolios and authoring spaces).
                   16439: 
                   16440: =over
                   16441: 
                   16442: =item defaultquota, authorquota
                   16443: 
                   16444: =back
                   16445: 
                   16446: =item
                   16447: tools (availability of aboutme page, blog, webDAV access for authoring spaces,
                   16448: portfolio for users).
                   16449: 
                   16450: =over
                   16451: 
                   16452: =item
                   16453: aboutme, blog, webdav, portfolio
                   16454: 
                   16455: =back
                   16456: 
                   16457: =item
                   16458: requestcourses: ability to request courses, and how requests are processed.
                   16459: 
                   16460: =over
                   16461: 
                   16462: =item
1.1305    raeburn  16463: official, unofficial, community, textbook, placement
1.1240    raeburn  16464: 
                   16465: =back
                   16466: 
                   16467: =item
                   16468: inststatus: types of institutional affiliation, and order in which they are displayed.
                   16469: 
                   16470: =over
                   16471: 
                   16472: =item
1.1256    raeburn  16473: inststatustypes, inststatusorder, inststatusguest
1.1240    raeburn  16474: 
                   16475: =back
                   16476: 
                   16477: =item
                   16478: coursedefaults: can PDF forms can be created, default credits for courses, default quotas (MB)
                   16479: for course's uploaded content.
                   16480: 
                   16481: =over
                   16482: 
                   16483: =item
1.1246    raeburn  16484: canuse_pdfforms, officialcredits, unofficialcredits, textbookcredits, officialquota, unofficialquota, 
1.1305    raeburn  16485: communityquota, textbookquota, placementquota
1.1240    raeburn  16486: 
                   16487: =back
                   16488: 
                   16489: =item
                   16490: usersessions: set options for hosting of your users in other domains, and hosting of users from other domains
                   16491: on your servers.
                   16492: 
                   16493: =over
                   16494: 
                   16495: =item 
                   16496: remotesessions, hostedsessions
                   16497: 
                   16498: =back
                   16499: 
                   16500: =back
                   16501: 
                   16502: In cases where a domain coordinator has never used the "Set Domain Configuration"
                   16503: utility to create a configuration.db file on a domain's primary library server 
                   16504: only the following domain defaults: auth_def, auth_arg_def, lang_def
                   16505: -- corresponding values are authentication type (internal, krb4, krb5,
                   16506: or localauth), initial password or a kerberos realm, language (e.g., en-us) -- 
                   16507: will be available. Values are retrieved from cache (if current), unless the
                   16508: optional $ignore_cache arg is true, or from domain's configuration.db (if available),
                   16509: or lastly from values in lonTabs/dns_domain,tab, or lonTabs/domain.tab.
                   16510: 
                   16511: Typical usage:
1.943     raeburn  16512: 
1.1240    raeburn  16513: %domdefaults = &get_domain_defaults($target_domain);
1.943     raeburn  16514: 
1.243     albertel 16515: =back
                   16516: 
                   16517: =head2 Network Status Functions
                   16518: 
                   16519: =over 4
1.191     harris41 16520: 
                   16521: =item *
                   16522: 
1.1137    raeburn  16523: dirlist() : return directory list based on URI (first arg).
                   16524: 
                   16525: Inputs: 1 required, 5 optional.
                   16526: 
                   16527: =over
                   16528: 
                   16529: =item 
                   16530: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
                   16531: 
                   16532: =item
                   16533: $userdomain - domain of user/course to be listed. Extracted from $uri if absent. 
                   16534: 
                   16535: =item
                   16536: $username -  username of user/course to be listed. Extracted from $uri if absent. 
                   16537: 
                   16538: =item
                   16539: $getpropath - boolean: 1 if prepend path using &propath(). 
                   16540: 
                   16541: =item
                   16542: $getuserdir - boolean: 1 if prepend path for "userfiles".
                   16543: 
                   16544: =item 
                   16545: $alternateRoot - path to prepend in place of path from $uri.
                   16546: 
                   16547: =back
                   16548: 
                   16549: Returns: Array of up to two items.
                   16550: 
                   16551: =over
                   16552: 
                   16553: a reference to an array of files/subdirectories
                   16554: 
                   16555: =over
                   16556: 
                   16557: Each element in the array of files/subdirectories is a & separated list of
                   16558: item name and the result of running stat on the item.  If dirlist was requested
                   16559: for a file instead of a directory, the item name will be ''. For a directory 
                   16560: listing, if the item is a metadata file, the element will end &N&M 
                   16561: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
                   16562: default copyright set (1).  
                   16563: 
                   16564: =back
                   16565: 
                   16566: a scalar containing error condition (if encountered).
                   16567: 
                   16568: =over
                   16569: 
                   16570: =item 
                   16571: no_host (no homeserver identified for $username:$domain).
                   16572: 
                   16573: =item 
                   16574: no_such_host (server contacted for listing not identified as valid host).
                   16575: 
                   16576: =item 
                   16577: con_lost (connection to remote server failed).
                   16578: 
                   16579: =item 
                   16580: refused (invalid $username:$domain received on lond side).
                   16581: 
                   16582: =item 
                   16583: no_such_dir (directory at specified path on lond side does not exist). 
                   16584: 
                   16585: =item 
                   16586: empty (directory at specified path on lond side is empty).
                   16587: 
                   16588: =over
                   16589: 
                   16590: This is currently not encountered because the &ls3, &ls2, 
                   16591: &ls (_handler) routines on the lond side do not filter out
                   16592: . and .. from a directory listing. 
                   16593: 
                   16594: =back
                   16595: 
                   16596: =back
                   16597: 
                   16598: =back
1.191     harris41 16599: 
                   16600: =item *
                   16601: 
1.243     albertel 16602: spareserver() : find server with least workload from spare.tab
                   16603: 
1.986     foxr     16604: 
                   16605: =item *
                   16606: 
                   16607: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
                   16608: if there is no corresponding loncapa host.
                   16609: 
1.243     albertel 16610: =back
                   16611: 
1.986     foxr     16612: 
1.243     albertel 16613: =head2 Apache Request
                   16614: 
                   16615: =over 4
1.191     harris41 16616: 
                   16617: =item *
                   16618: 
1.243     albertel 16619: ssi($url,%hash) : server side include, does a complete request cycle on url to
                   16620: localhost, posts hash
                   16621: 
                   16622: =back
                   16623: 
                   16624: =head2 Data to String to Data
                   16625: 
                   16626: =over 4
1.191     harris41 16627: 
                   16628: =item *
                   16629: 
1.243     albertel 16630: hash2str(%hash) : convert a hash into a string complete with escaping and '='
                   16631: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191     harris41 16632: 
                   16633: =item *
                   16634: 
1.243     albertel 16635: hashref2str($hashref) : convert a hashref into a string complete with
                   16636: escaping and '=' and '&' separators, supports elements that are
                   16637: arrayrefs and hashrefs
1.191     harris41 16638: 
                   16639: =item *
                   16640: 
1.243     albertel 16641: arrayref2str($arrayref) : convert an arrayref into a string complete
                   16642: with escaping and '&' separators, supports elements that are arrayrefs
                   16643: and hashrefs
1.191     harris41 16644: 
                   16645: =item *
                   16646: 
1.243     albertel 16647: str2hash($string) : convert string to hash using unescaping and
                   16648: splitting on '=' and '&', supports elements that are arrayrefs and
                   16649: hashrefs
1.191     harris41 16650: 
                   16651: =item *
                   16652: 
1.243     albertel 16653: str2array($string) : convert string to hash using unescaping and
                   16654: splitting on '&', supports elements that are arrayrefs and hashrefs
                   16655: 
                   16656: =back
                   16657: 
                   16658: =head2 Logging Routines
                   16659: 
                   16660: 
                   16661: These routines allow one to make log messages in the lonnet.log and
                   16662: lonnet.perm logfiles.
1.191     harris41 16663: 
1.1119    foxr     16664: =over 4
                   16665: 
1.191     harris41 16666: =item *
                   16667: 
1.243     albertel 16668: logtouch() : make sure the logfile, lonnet.log, exists
1.191     harris41 16669: 
                   16670: =item *
                   16671: 
1.243     albertel 16672: logthis() : append message to the normal lonnet.log file, it gets
                   16673: preiodically rolled over and deleted.
1.191     harris41 16674: 
                   16675: =item *
                   16676: 
1.243     albertel 16677: logperm() : append a permanent message to lonnet.perm.log, this log
                   16678: file never gets deleted by any automated portion of the system, only
                   16679: messages of critical importance should go in here.
                   16680: 
1.1119    foxr     16681: 
1.243     albertel 16682: =back
                   16683: 
                   16684: =head2 General File Helper Routines
                   16685: 
                   16686: =over 4
1.191     harris41 16687: 
                   16688: =item *
                   16689: 
1.481     raeburn  16690: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
                   16691: (a) files in /uploaded
                   16692:   (i) If a local copy of the file exists - 
                   16693:       compares modification date of local copy with last-modified date for 
                   16694:       definitive version stored on home server for course. If local copy is 
                   16695:       stale, requests a new version from the home server and stores it. 
                   16696:       If the original has been removed from the home server, then local copy 
                   16697:       is unlinked.
                   16698:   (ii) If local copy does not exist -
                   16699:       requests the file from the home server and stores it. 
                   16700:   
                   16701:   If $caller is 'uploadrep':  
                   16702:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
                   16703:     for request for files originally uploaded via DOCS. 
                   16704:      - returns 'ok' if fresh local copy now available, -1 otherwise.
                   16705:   
                   16706:   Otherwise:
                   16707:      This indicates a call from the content generation phase of the request.
                   16708:      -  returns the entire contents of the file or -1.
                   16709:      
                   16710: (b) files in /res
                   16711:    - returns the entire contents of a file or -1; 
                   16712:    it properly subscribes to and replicates the file if neccessary.
1.191     harris41 16713: 
1.712     albertel 16714: 
                   16715: =item *
                   16716: 
                   16717: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
                   16718:                   reference
                   16719: 
                   16720: returns either a stat() list of data about the file or an empty list
                   16721: if the file doesn't exist or couldn't find out about it (connection
                   16722: problems or user unknown)
                   16723: 
1.191     harris41 16724: =item *
                   16725: 
1.243     albertel 16726: filelocation($dir,$file) : returns file system location of a file
                   16727: based on URI; meant to be "fairly clean" absolute reference, $dir is a
                   16728: directory that relative $file lookups are to looked in ($dir of /a/dir
                   16729: and a file of ../bob will become /a/bob)
1.191     harris41 16730: 
                   16731: =item *
                   16732: 
                   16733: hreflocation($dir,$file) : returns file system location or a URL; same as
                   16734: filelocation except for hrefs
                   16735: 
                   16736: =item *
                   16737: 
1.1261    raeburn  16738: declutter() : declutters URLs -- remove beginning slashes, 'res' etc.
                   16739: also removes beginning /home/httpd/html unless /priv/ follows it.
1.191     harris41 16740: 
1.243     albertel 16741: =back
                   16742: 
1.608     albertel 16743: =head2 Usererfile file routines (/uploaded*)
                   16744: 
                   16745: =over 4
                   16746: 
                   16747: =item *
                   16748: 
                   16749: userfileupload(): main rotine for putting a file in a user or course's
                   16750:                   filespace, arguments are,
                   16751: 
1.620     albertel 16752:  formname - required - this is the name of the element in $env where the
1.608     albertel 16753:            filename, and the contents of the file to create/modifed exist
1.620     albertel 16754:            the filename is in $env{'form.'.$formname.'.filename'} and the
                   16755:            contents of the file is located in $env{'form.'.$formname}
1.1090    raeburn  16756:  context - if coursedoc, store the file in the course of the active role
                   16757:              of the current user; 
                   16758:            if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
                   16759:            if 'canceloverwrite': delete file in tmp/overwrites directory
1.608     albertel 16760:  subdir - required - subdirectory to put the file in under ../userfiles/
                   16761:          if undefined, it will be placed in "unknown"
                   16762: 
                   16763:  (This routine calls clean_filename() to remove any dangerous
                   16764:  characters from the filename, and then calls finuserfileupload() to
                   16765:  complete the transaction)
                   16766: 
                   16767:  returns either the url of the uploaded file (/uploaded/....) if successful
                   16768:  and /adm/notfound.html if unsuccessful
                   16769: 
                   16770: =item *
                   16771: 
                   16772: clean_filename(): routine for cleaing a filename up for storage in
                   16773:                  userfile space, argument is:
                   16774: 
                   16775:  filename - proposed filename
                   16776: 
                   16777: returns: the new clean filename
                   16778: 
                   16779: =item *
                   16780: 
1.1090    raeburn  16781: finishuserfileupload(): routine that creates and sends the file to
1.608     albertel 16782: userspace, probably shouldn't be called directly
                   16783: 
                   16784:   docuname: username or courseid of destination for the file
                   16785:   docudom: domain of user/course of destination for the file
                   16786:   formname: same as for userfileupload()
1.1090    raeburn  16787:   fname: filename (including subdirectories) for the file
                   16788:   parser: if 'parse', will parse (html) file to extract references to objects, links etc.
1.1401    raeburn  16789:           if hashref, and context is scantron, will convert csv format to standard format
1.1090    raeburn  16790:   allfiles: reference to hash used to store objects found by parser
                   16791:   codebase: reference to hash used for codebases of java objects found by parser
                   16792:   thumbwidth: width (pixels) of thumbnail to be created for uploaded image
                   16793:   thumbheight: height (pixels) of thumbnail to be created for uploaded image
                   16794:   resizewidth: width to be used to resize image using resizeImage from ImageMagick
                   16795:   resizeheight: height to be used to resize image using resizeImage from ImageMagick
                   16796:   context: if 'overwrite', will move the uploaded file from its temporary location to
                   16797:             userfiles to facilitate overwriting a previously uploaded file with same name.
1.1095    raeburn  16798:   mimetype: reference to scalar to accommodate mime type determined
                   16799:             from File::MMagic if $parser = parse.
1.608     albertel 16800: 
                   16801:  returns either the url of the uploaded file (/uploaded/....) if successful
1.1090    raeburn  16802:  and /adm/notfound.html if unsuccessful (or an error message if context 
                   16803:  was 'overwrite').
                   16804:  
1.608     albertel 16805: 
                   16806: =item *
                   16807: 
                   16808: renameuserfile(): renames an existing userfile to a new name
                   16809: 
                   16810:   Args:
                   16811:    docuname: username or courseid of destination for the file
                   16812:    docudom: domain of user/course of destination for the file
                   16813:    old: current file name (including any subdirs under userfiles)
                   16814:    new: desired file name (including any subdirs under userfiles)
                   16815: 
                   16816: =item *
                   16817: 
                   16818: mkdiruserfile(): creates a directory is a userfiles dir
                   16819: 
                   16820:   Args:
                   16821:    docuname: username or courseid of destination for the file
                   16822:    docudom: domain of user/course of destination for the file
                   16823:    dir: dir to create (including any subdirs under userfiles)
                   16824: 
                   16825: =item *
                   16826: 
                   16827: removeuserfile(): removes a file that exists in userfiles
                   16828: 
                   16829:   Args:
                   16830:    docuname: username or courseid of destination for the file
                   16831:    docudom: domain of user/course of destination for the file
                   16832:    fname: filname to delete (including any subdirs under userfiles)
                   16833: 
                   16834: =item *
                   16835: 
                   16836: removeuploadedurl(): convience function for removeuserfile()
                   16837: 
                   16838:   Args:
                   16839:    url:  a full /uploaded/... url to delete
                   16840: 
1.747     albertel 16841: =item * 
                   16842: 
                   16843: get_portfile_permissions():
                   16844:   Args:
                   16845:     domain: domain of user or course contain the portfolio files
                   16846:     user: name of user or num of course contain the portfolio files
                   16847:   Returns:
                   16848:     hashref of a dump of the proper file_permissions.db
                   16849:    
                   16850: 
                   16851: =item * 
                   16852: 
                   16853: get_access_controls():
                   16854: 
                   16855: Args:
                   16856:   current_permissions: the hash ref returned from get_portfile_permissions()
                   16857:   group: (optional) the group you want the files associated with
                   16858:   file: (optional) the file you want access info on
                   16859: 
                   16860: Returns:
1.749     raeburn  16861:     a hash (keys are file names) of hashes containing
                   16862:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
                   16863:         values are XML containing access control settings (see below) 
1.747     albertel 16864: 
                   16865: Internal notes:
                   16866: 
1.749     raeburn  16867:  access controls are stored in file_permissions.db as key=value pairs.
                   16868:     key -> path to file/file_name\0uniqueID:scope_end_start
                   16869:         where scope -> public,guest,course,group,domains or users.
                   16870:               end -> UNIX time for end of access (0 -> no end date)
                   16871:               start -> UNIX time for start of access
                   16872: 
                   16873:     value -> XML description of access control
                   16874:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
                   16875:             <start></start>
                   16876:             <end></end>
                   16877: 
                   16878:             <password></password>  for scope type = guest
                   16879: 
                   16880:             <domain></domain>     for scope type = course or group
                   16881:             <number></number>
                   16882:             <roles id="">
                   16883:              <role></role>
                   16884:              <access></access>
                   16885:              <section></section>
                   16886:              <group></group>
                   16887:             </roles>
                   16888: 
                   16889:             <dom></dom>         for scope type = domains
                   16890: 
                   16891:             <users>             for scope type = users
                   16892:              <user>
                   16893:               <uname></uname>
                   16894:               <udom></udom>
                   16895:              </user>
                   16896:             </users>
                   16897:            </scope> 
                   16898:               
                   16899:  Access data is also aggregated for each file in an additional key=value pair:
                   16900:  key -> path to file/file_name\0accesscontrol 
                   16901:  value -> reference to hash
                   16902:           hash contains key = value pairs
                   16903:           where key = uniqueID:scope_end_start
                   16904:                 value = UNIX time record was last updated
                   16905: 
                   16906:           Used to improve speed of look-ups of access controls for each file.  
                   16907:  
                   16908:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
                   16909: 
1.1198    raeburn  16910: =item *
                   16911: 
1.749     raeburn  16912: modify_access_controls():
                   16913: 
                   16914: Modifies access controls for a portfolio file
                   16915: Args
                   16916: 1. file name
                   16917: 2. reference to hash of required changes,
                   16918: 3. domain
                   16919: 4. username
                   16920:   where domain,username are the domain of the portfolio owner 
                   16921:   (either a user or a course) 
                   16922: 
                   16923: Returns:
                   16924: 1. result of additions or updates ('ok' or 'error', with error message). 
                   16925: 2. result of deletions ('ok' or 'error', with error message).
                   16926: 3. reference to hash of any new or updated access controls.
                   16927: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
                   16928:    key = integer (inbound ID)
1.1198    raeburn  16929:    value = uniqueID
                   16930: 
                   16931: =item *
                   16932: 
                   16933: get_timebased_id():
                   16934: 
                   16935: Attempts to get a unique timestamp-based suffix for use with items added to a 
                   16936: course via the Course Editor (e.g., folders, composite pages, 
                   16937: group bulletin boards).
                   16938: 
                   16939: Args: (first three required; six others optional)
                   16940: 
                   16941: 1. prefix (alphanumeric): of keys in hash, e.g., suppsequence, docspage,
                   16942:    docssequence, or name of group
                   16943: 
                   16944: 2. keyid (alphanumeric): name of temporary locking key in hash,
                   16945:    e.g., num, boardids
                   16946: 
                   16947: 3. namespace: name of gdbm file used to store suffixes already assigned;  
                   16948:    file will be named nohist_namespace.db
                   16949: 
                   16950: 4. cdom: domain of course; default is current course domain from %env
                   16951: 
                   16952: 5. cnum: course number; default is current course number from %env
                   16953: 
                   16954: 6. idtype: set to concat if an additional digit is to be appended to the 
                   16955:    unix timestamp to form the suffix, if the plain timestamp is already
                   16956:    in use.  Default is to not do this, but simply increment the unix 
                   16957:    timestamp by 1 until a unique key is obtained.
                   16958: 
                   16959: 7. who: holder of locking key; defaults to user:domain for user.
                   16960: 
                   16961: 8. locktries: number of attempts to obtain a lock (sleep of 1s before 
                   16962:    retrying); default is 3.
                   16963: 
                   16964: 9. maxtries: number of attempts to obtain a unique suffix; default is 20.  
                   16965: 
                   16966: Returns:
                   16967: 
                   16968: 1. suffix obtained (numeric)
                   16969: 
                   16970: 2. result of deleting locking key (ok if deleted, or lock never obtained)
                   16971: 
                   16972: 3. error: contains (localized) error message if an error occurred.
                   16973: 
1.747     albertel 16974: 
1.608     albertel 16975: =back
                   16976: 
1.243     albertel 16977: =head2 HTTP Helper Routines
                   16978: 
                   16979: =over 4
                   16980: 
1.191     harris41 16981: =item *
                   16982: 
                   16983: escape() : unpack non-word characters into CGI-compatible hex codes
                   16984: 
                   16985: =item *
                   16986: 
                   16987: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
                   16988: 
1.243     albertel 16989: =back
                   16990: 
                   16991: =head1 PRIVATE SUBROUTINES
                   16992: 
                   16993: =head2 Underlying communication routines (Shouldn't call)
                   16994: 
                   16995: =over 4
                   16996: 
                   16997: =item *
                   16998: 
                   16999: subreply() : tries to pass a message to lonc, returns con_lost if incapable
                   17000: 
                   17001: =item *
                   17002: 
                   17003: reply() : uses subreply to send a message to remote machine, logs all failures
                   17004: 
                   17005: =item *
                   17006: 
                   17007: critical() : passes a critical message to another server; if cannot
                   17008: get through then place message in connection buffer directory and
                   17009: returns con_delayed, if incapable of saving message, returns
                   17010: con_failed
                   17011: 
                   17012: =item *
                   17013: 
                   17014: reconlonc() : tries to reconnect lonc client processes.
                   17015: 
                   17016: =back
                   17017: 
                   17018: =head2 Resource Access Logging
                   17019: 
                   17020: =over 4
                   17021: 
                   17022: =item *
                   17023: 
                   17024: flushcourselogs() : flush (save) buffer logs and access logs
                   17025: 
                   17026: =item *
                   17027: 
                   17028: courselog($what) : save message for course in hash
                   17029: 
                   17030: =item *
                   17031: 
                   17032: courseacclog($what) : save message for course using &courselog().  Perform
                   17033: special processing for specific resource types (problems, exams, quizzes, etc).
                   17034: 
1.191     harris41 17035: =item *
                   17036: 
                   17037: goodbye() : flush course logs and log shutting down; it is called in srm.conf
                   17038: as a PerlChildExitHandler
1.243     albertel 17039: 
                   17040: =back
                   17041: 
                   17042: =head2 Other
                   17043: 
                   17044: =over 4
                   17045: 
                   17046: =item *
                   17047: 
                   17048: symblist($mapname,%newhash) : update symbolic storage links
1.191     harris41 17049: 
                   17050: =back
                   17051: 
                   17052: =cut
1.877     foxr     17053: 

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