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

1.1       albertel    1: # The LearningOnline Network
                      2: # TCP networking package
1.12      www         3: #
1.966   ! raeburn     4: # $Id: lonnet.pm,v 1.965 2008/08/27 02:29:31 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.1       albertel   30: package Apache::lonnet;
                     31: 
                     32: use strict;
1.8       www        33: use LWP::UserAgent();
1.486     www        34: use HTTP::Date;
                     35: # use Date::Parse;
1.871     albertel   36: use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir
                     37:             $_64bit %env);
                     38: 
                     39: my (%badServerCache, $memcache, %courselogs, %accesshash, %domainrolehash,
                     40:     %userrolehash, $processmarker, $dumpcount, %coursedombuf,
                     41:     %coursenumbuf, %coursehombuf, %coursedescrbuf, %courseinstcodebuf,
1.958     www        42:     %courseownerbuf, %coursetypebuf,$locknum);
1.403     www        43: 
1.1       albertel   44: use IO::Socket;
1.31      www        45: use GDBM_File;
1.208     albertel   46: use HTML::LCParser;
1.88      www        47: use Fcntl qw(:flock);
1.870     albertel   48: use Storable qw(thaw nfreeze);
1.539     albertel   49: use Time::HiRes qw( gettimeofday tv_interval );
1.599     albertel   50: use Cache::Memcached;
1.676     albertel   51: use Digest::MD5;
1.790     albertel   52: use Math::Random;
1.807     albertel   53: use LONCAPA qw(:DEFAULT :match);
1.740     www        54: use LONCAPA::Configuration;
1.676     albertel   55: 
1.195     www        56: my $readit;
1.550     foxr       57: my $max_connection_retries = 10;     # Or some such value.
1.1       albertel   58: 
1.619     albertel   59: require Exporter;
                     60: 
                     61: our @ISA = qw (Exporter);
                     62: our @EXPORT = qw(%env);
                     63: 
1.449     matthew    64: =pod
                     65: 
                     66: =head1 Package Variables
                     67: 
                     68: These are largely undocumented, so if you decipher one please note it here.
                     69: 
                     70: =over 4
                     71: 
                     72: =item $processmarker
                     73: 
                     74: Contains the time this process was started and this servers host id.
                     75: 
                     76: =item $dumpcount
                     77: 
                     78: Counts the number of times a message log flush has been attempted (regardless
                     79: of success) by this process.  Used as part of the filename when messages are
                     80: delayed.
                     81: 
                     82: =back
                     83: 
                     84: =cut
                     85: 
                     86: 
1.1       albertel   87: # --------------------------------------------------------------------- Logging
1.729     www        88: {
                     89:     my $logid;
                     90:     sub instructor_log {
1.957     raeburn    91: 	my ($hash_name,$storehash,$delflag,$uname,$udom,$cnum,$cdom)=@_;
                     92:         if (($cnum eq '') || ($cdom eq '')) {
                     93:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                     94:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                     95:         }
1.729     www        96: 	$logid++;
1.957     raeburn    97:         my $now = time();
                     98: 	my $id=$now.'00000'.$$.'00000'.$logid;
1.729     www        99: 	return &Apache::lonnet::put('nohist_'.$hash_name,
1.730     www       100: 				    { $id => {
                    101: 					'exe_uname' => $env{'user.name'},
                    102: 					'exe_udom'  => $env{'user.domain'},
1.957     raeburn   103: 					'exe_time'  => $now,
1.730     www       104: 					'exe_ip'    => $ENV{'REMOTE_ADDR'},
                    105: 					'delflag'   => $delflag,
                    106: 					'logentry'  => $storehash,
                    107: 					'uname'     => $uname,
                    108: 					'udom'      => $udom,
                    109: 				    }
1.957     raeburn   110: 				  },$cdom,$cnum);
1.729     www       111:     }
                    112: }
1.1       albertel  113: 
1.163     harris41  114: sub logtouch {
                    115:     my $execdir=$perlvar{'lonDaemons'};
1.448     albertel  116:     unless (-e "$execdir/logs/lonnet.log") {	
                    117: 	open(my $fh,">>$execdir/logs/lonnet.log");
1.163     harris41  118: 	close $fh;
                    119:     }
                    120:     my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
                    121:     chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
                    122: }
                    123: 
1.1       albertel  124: sub logthis {
                    125:     my $message=shift;
                    126:     my $execdir=$perlvar{'lonDaemons'};
                    127:     my $now=time;
                    128:     my $local=localtime($now);
1.448     albertel  129:     if (open(my $fh,">>$execdir/logs/lonnet.log")) {
                    130: 	print $fh "$local ($$): $message\n";
                    131: 	close($fh);
                    132:     }
1.1       albertel  133:     return 1;
                    134: }
                    135: 
                    136: sub logperm {
                    137:     my $message=shift;
                    138:     my $execdir=$perlvar{'lonDaemons'};
                    139:     my $now=time;
                    140:     my $local=localtime($now);
1.448     albertel  141:     if (open(my $fh,">>$execdir/logs/lonnet.perm.log")) {
                    142: 	print $fh "$now:$message:$local\n";
                    143: 	close($fh);
                    144:     }
1.1       albertel  145:     return 1;
                    146: }
                    147: 
1.850     albertel  148: sub create_connection {
1.853     albertel  149:     my ($hostname,$lonid) = @_;
1.851     albertel  150:     my $client=IO::Socket::UNIX->new(Peer    => $perlvar{'lonSockCreate'},
1.850     albertel  151: 				     Type    => SOCK_STREAM,
                    152: 				     Timeout => 10);
                    153:     return 0 if (!$client);
1.890     albertel  154:     print $client (join(':',$hostname,$lonid,&machine_ids($hostname))."\n");
1.850     albertel  155:     my $result = <$client>;
                    156:     chomp($result);
                    157:     return 1 if ($result eq 'done');
                    158:     return 0;
                    159: }
                    160: 
                    161: 
1.1       albertel  162: # -------------------------------------------------- Non-critical communication
                    163: sub subreply {
                    164:     my ($cmd,$server)=@_;
1.838     albertel  165:     my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
1.549     foxr      166:     #
                    167:     #  With loncnew process trimming, there's a timing hole between lonc server
                    168:     #  process exit and the master server picking up the listen on the AF_UNIX
                    169:     #  socket.  In that time interval, a lock file will exist:
                    170: 
                    171:     my $lockfile=$peerfile.".lock";
                    172:     while (-e $lockfile) {	# Need to wait for the lockfile to disappear.
                    173: 	sleep(1);
                    174:     }
                    175:     # At this point, either a loncnew parent is listening or an old lonc
1.550     foxr      176:     # or loncnew child is listening so we can connect or everything's dead.
1.549     foxr      177:     #
1.550     foxr      178:     #   We'll give the connection a few tries before abandoning it.  If
                    179:     #   connection is not possible, we'll con_lost back to the client.
                    180:     #   
                    181:     my $client;
                    182:     for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
                    183: 	$client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
                    184: 				      Type    => SOCK_STREAM,
                    185: 				      Timeout => 10);
1.869     albertel  186: 	if ($client) {
1.550     foxr      187: 	    last;		# Connected!
1.850     albertel  188: 	} else {
1.853     albertel  189: 	    &create_connection(&hostname($server),$server);
1.550     foxr      190: 	}
1.850     albertel  191:         sleep(1);		# Try again later if failed connection.
1.550     foxr      192:     }
                    193:     my $answer;
                    194:     if ($client) {
1.704     albertel  195: 	print $client "sethost:$server:$cmd\n";
1.550     foxr      196: 	$answer=<$client>;
                    197: 	if (!$answer) { $answer="con_lost"; }
                    198: 	chomp($answer);
                    199:     } else {
                    200: 	$answer = 'con_lost';	# Failed connection.
                    201:     }
1.1       albertel  202:     return $answer;
                    203: }
                    204: 
                    205: sub reply {
                    206:     my ($cmd,$server)=@_;
1.838     albertel  207:     unless (defined(&hostname($server))) { return 'no_such_host'; }
1.1       albertel  208:     my $answer=subreply($cmd,$server);
1.65      www       209:     if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
1.672     albertel  210:        &logthis("<font color=\"blue\">WARNING:".
1.12      www       211:                 " $cmd to $server returned $answer</font>");
                    212:     }
1.1       albertel  213:     return $answer;
                    214: }
                    215: 
                    216: # ----------------------------------------------------------- Send USR1 to lonc
                    217: 
                    218: sub reconlonc {
1.891     albertel  219:     my ($lonid) = @_;
                    220:     my $hostname = &hostname($lonid);
                    221:     if ($lonid) {
                    222: 	my $peerfile="$perlvar{'lonSockDir'}/$hostname";
                    223: 	if ($hostname && -e $peerfile) {
                    224: 	    &logthis("Trying to reconnect lonc for $lonid ($hostname)");
                    225: 	    my $client=IO::Socket::UNIX->new(Peer    => $peerfile,
                    226: 					     Type    => SOCK_STREAM,
                    227: 					     Timeout => 10);
                    228: 	    if ($client) {
                    229: 		print $client ("reset_retries\n");
                    230: 		my $answer=<$client>;
                    231: 		#reset just this one.
                    232: 	    }
                    233: 	}
                    234: 	return;
                    235:     }
                    236: 
1.836     www       237:     &logthis("Trying to reconnect lonc");
1.1       albertel  238:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
1.448     albertel  239:     if (open(my $fh,"<$loncfile")) {
1.1       albertel  240: 	my $loncpid=<$fh>;
                    241:         chomp($loncpid);
                    242:         if (kill 0 => $loncpid) {
                    243: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
                    244:             kill USR1 => $loncpid;
                    245:             sleep 1;
1.836     www       246:          } else {
1.12      www       247: 	    &logthis(
1.672     albertel  248:                "<font color=\"blue\">WARNING:".
1.12      www       249:                " lonc at pid $loncpid not responding, giving up</font>");
1.1       albertel  250:         }
                    251:     } else {
1.836     www       252: 	&logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
1.1       albertel  253:     }
                    254: }
                    255: 
                    256: # ------------------------------------------------------ Critical communication
1.12      www       257: 
1.1       albertel  258: sub critical {
                    259:     my ($cmd,$server)=@_;
1.838     albertel  260:     unless (&hostname($server)) {
1.672     albertel  261:         &logthis("<font color=\"blue\">WARNING:".
1.89      www       262:                " Critical message to unknown server ($server)</font>");
                    263:         return 'no_such_host';
                    264:     }
1.1       albertel  265:     my $answer=reply($cmd,$server);
                    266:     if ($answer eq 'con_lost') {
                    267: 	&reconlonc("$perlvar{'lonSockDir'}/$server");
1.589     albertel  268: 	my $answer=reply($cmd,$server);
1.1       albertel  269:         if ($answer eq 'con_lost') {
                    270:             my $now=time;
                    271:             my $middlename=$cmd;
1.5       www       272:             $middlename=substr($middlename,0,16);
1.1       albertel  273:             $middlename=~s/\W//g;
                    274:             my $dfilename=
1.305     www       275:       "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
                    276:             $dumpcount++;
1.1       albertel  277:             {
1.448     albertel  278: 		my $dfh;
                    279: 		if (open($dfh,">$dfilename")) {
                    280: 		    print $dfh "$cmd\n"; 
                    281: 		    close($dfh);
                    282: 		}
1.1       albertel  283:             }
                    284:             sleep 2;
                    285:             my $wcmd='';
                    286:             {
1.448     albertel  287: 		my $dfh;
                    288: 		if (open($dfh,"<$dfilename")) {
                    289: 		    $wcmd=<$dfh>; 
                    290: 		    close($dfh);
                    291: 		}
1.1       albertel  292:             }
                    293:             chomp($wcmd);
1.7       www       294:             if ($wcmd eq $cmd) {
1.672     albertel  295: 		&logthis("<font color=\"blue\">WARNING: ".
1.12      www       296:                          "Connection buffer $dfilename: $cmd</font>");
1.1       albertel  297:                 &logperm("D:$server:$cmd");
                    298: 	        return 'con_delayed';
                    299:             } else {
1.672     albertel  300:                 &logthis("<font color=\"red\">CRITICAL:"
1.12      www       301:                         ." Critical connection failed: $server $cmd</font>");
1.1       albertel  302:                 &logperm("F:$server:$cmd");
                    303:                 return 'con_failed';
                    304:             }
                    305:         }
                    306:     }
                    307:     return $answer;
1.405     albertel  308: }
                    309: 
1.755     albertel  310: # ------------------------------------------- check if return value is an error
                    311: 
                    312: sub error {
                    313:     my ($result) = @_;
1.756     albertel  314:     if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
1.755     albertel  315: 	if ($2 == 2) { return undef; }
                    316: 	return $1;
                    317:     }
                    318:     return undef;
                    319: }
                    320: 
1.783     albertel  321: sub convert_and_load_session_env {
                    322:     my ($lonidsdir,$handle)=@_;
                    323:     my @profile;
                    324:     {
1.917     albertel  325: 	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    326: 	if (!$opened) {
1.915     albertel  327: 	    return 0;
                    328: 	}
1.783     albertel  329: 	flock($idf,LOCK_SH);
                    330: 	@profile=<$idf>;
                    331: 	close($idf);
                    332:     }
                    333:     my %temp_env;
                    334:     foreach my $line (@profile) {
1.786     albertel  335: 	if ($line !~ m/=/) {
                    336: 	    return 0;
                    337: 	}
1.783     albertel  338: 	chomp($line);
                    339: 	my ($envname,$envvalue)=split(/=/,$line,2);
                    340: 	$temp_env{&unescape($envname)} = &unescape($envvalue);
                    341:     }
                    342:     unlink("$lonidsdir/$handle.id");
                    343:     if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
                    344: 	    0640)) {
                    345: 	%disk_env = %temp_env;
                    346: 	@env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
                    347: 	untie(%disk_env);
                    348:     }
1.786     albertel  349:     return 1;
1.783     albertel  350: }
                    351: 
1.374     www       352: # ------------------------------------------- Transfer profile into environment
1.780     albertel  353: my $env_loaded;
                    354: sub transfer_profile_to_env {
1.788     albertel  355:     my ($lonidsdir,$handle,$force_transfer) = @_;
                    356:     if (!$force_transfer && $env_loaded) { return; } 
1.374     www       357: 
1.720     albertel  358:     if (!defined($lonidsdir)) {
                    359: 	$lonidsdir = $perlvar{'lonIDsDir'};
                    360:     }
                    361:     if (!defined($handle)) {
                    362:         ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
                    363:     }
                    364: 
1.786     albertel  365:     my $convert;
                    366:     {
1.917     albertel  367:     	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    368: 	if (!$opened) {
1.915     albertel  369: 	    return;
                    370: 	}
1.786     albertel  371: 	flock($idf,LOCK_SH);
                    372: 	if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
                    373: 		&GDBM_READER(),0640)) {
                    374: 	    @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
                    375: 	    untie(%disk_env);
                    376: 	} else {
                    377: 	    $convert = 1;
                    378: 	}
                    379:     }
                    380:     if ($convert) {
                    381: 	if (!&convert_and_load_session_env($lonidsdir,$handle)) {
                    382: 	    &logthis("Failed to load session, or convert session.");
                    383: 	}
1.374     www       384:     }
1.783     albertel  385: 
1.786     albertel  386:     my %remove;
1.783     albertel  387:     while ( my $envname = each(%env) ) {
1.433     matthew   388:         if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
                    389:             if ($time < time-300) {
1.783     albertel  390:                 $remove{$key}++;
1.433     matthew   391:             }
                    392:         }
                    393:     }
1.783     albertel  394: 
1.619     albertel  395:     $env{'user.environment'} = "$lonidsdir/$handle.id";
1.780     albertel  396:     $env_loaded=1;
1.783     albertel  397:     foreach my $expired_key (keys(%remove)) {
1.433     matthew   398:         &delenv($expired_key);
1.374     www       399:     }
1.1       albertel  400: }
                    401: 
1.916     albertel  402: # ---------------------------------------------------- Check for valid session 
                    403: sub check_for_valid_session {
                    404:     my ($r) = @_;
                    405:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
                    406:     my $lonid=$cookies{'lonID'};
                    407:     return undef if (!$lonid);
                    408: 
                    409:     my $handle=&LONCAPA::clean_handle($lonid->value);
                    410:     my $lonidsdir=$r->dir_config('lonIDsDir');
                    411:     return undef if (!-e "$lonidsdir/$handle.id");
                    412: 
1.917     albertel  413:     my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    414:     return undef if (!$opened);
1.916     albertel  415: 
                    416:     flock($idf,LOCK_SH);
                    417:     my %disk_env;
                    418:     if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
                    419: 	    &GDBM_READER(),0640)) {
                    420: 	return undef;	
                    421:     }
                    422: 
                    423:     if (!defined($disk_env{'user.name'})
                    424: 	|| !defined($disk_env{'user.domain'})) {
                    425: 	return undef;
                    426:     }
                    427:     return $handle;
                    428: }
                    429: 
1.830     albertel  430: sub timed_flock {
                    431:     my ($file,$lock_type) = @_;
                    432:     my $failed=0;
                    433:     eval {
                    434: 	local $SIG{__DIE__}='DEFAULT';
                    435: 	local $SIG{ALRM}=sub {
                    436: 	    $failed=1;
                    437: 	    die("failed lock");
                    438: 	};
                    439: 	alarm(13);
                    440: 	flock($file,$lock_type);
                    441: 	alarm(0);
                    442:     };
                    443:     if ($failed) {
                    444: 	return undef;
                    445:     } else {
                    446: 	return 1;
                    447:     }
                    448: }
                    449: 
1.5       www       450: # ---------------------------------------------------------- Append Environment
                    451: 
                    452: sub appenv {
1.949     raeburn   453:     my ($newenv,$roles) = @_;
                    454:     if (ref($newenv) eq 'HASH') {
                    455:         foreach my $key (keys(%{$newenv})) {
                    456:             my $refused = 0;
                    457: 	    if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
                    458:                 $refused = 1;
                    459:                 if (ref($roles) eq 'ARRAY') {
                    460:                     my ($type,$role) = ($key =~ /^user\.(role|priv)\.([^.]+)\./);
                    461:                     if (grep(/^\Q$role\E$/,@{$roles})) {
                    462:                         $refused = 0;
                    463:                     }
                    464:                 }
                    465:             }
                    466:             if ($refused) {
                    467:                 &logthis("<font color=\"blue\">WARNING: ".
                    468:                          "Attempt to modify environment ".$key." to ".$newenv->{$key}
                    469:                          .'</font>');
                    470: 	        delete($newenv->{$key});
                    471:             } else {
                    472:                 $env{$key}=$newenv->{$key};
                    473:             }
                    474:         }
                    475:         my $opened = open(my $env_file,'+<',$env{'user.environment'});
                    476:         if ($opened
                    477: 	    && &timed_flock($env_file,LOCK_EX)
                    478: 	    &&
                    479: 	    tie(my %disk_env,'GDBM_File',$env{'user.environment'},
                    480: 	        (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
                    481: 	    while (my ($key,$value) = each(%{$newenv})) {
                    482: 	        $disk_env{$key} = $value;
                    483: 	    }
                    484: 	    untie(%disk_env);
1.35      www       485:         }
1.191     harris41  486:     }
1.56      www       487:     return 'ok';
                    488: }
                    489: # ----------------------------------------------------- Delete from Environment
                    490: 
                    491: sub delenv {
                    492:     my $delthis=shift;
                    493:     if (($delthis=~/user\.role/) || ($delthis=~/user\.priv/)) {
1.672     albertel  494:         &logthis("<font color=\"blue\">WARNING: ".
1.56      www       495:                 "Attempt to delete from environment ".$delthis);
                    496:         return 'error';
                    497:     }
1.917     albertel  498:     my $opened = open(my $env_file,'+<',$env{'user.environment'});
                    499:     if ($opened
1.915     albertel  500: 	&& &timed_flock($env_file,LOCK_EX)
1.830     albertel  501: 	&&
                    502: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
                    503: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783     albertel  504: 	foreach my $key (keys(%disk_env)) {
                    505: 	    if ($key=~/^$delthis/) { 
1.915     albertel  506: 		delete($env{$key});
                    507: 		delete($disk_env{$key});
                    508: 	    }
1.448     albertel  509: 	}
1.783     albertel  510: 	untie(%disk_env);
1.5       www       511:     }
                    512:     return 'ok';
1.369     albertel  513: }
                    514: 
1.790     albertel  515: sub get_env_multiple {
                    516:     my ($name) = @_;
                    517:     my @values;
                    518:     if (defined($env{$name})) {
                    519:         # exists is it an array
                    520:         if (ref($env{$name})) {
                    521:             @values=@{ $env{$name} };
                    522:         } else {
                    523:             $values[0]=$env{$name};
                    524:         }
                    525:     }
                    526:     return(@values);
                    527: }
                    528: 
1.958     www       529: # ------------------------------------------------------------------- Locking
                    530: 
                    531: sub set_lock {
                    532:     my ($text)=@_;
                    533:     $locknum++;
                    534:     my $id=$$.'-'.$locknum;
                    535:     &appenv({'session.locks' => $env{'session.locks'}.','.$id,
                    536:              'session.lock.'.$id => $text});
                    537:     return $id;
                    538: }
                    539: 
                    540: sub get_locks {
                    541:     my $num=0;
                    542:     my %texts=();
                    543:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                    544:        if ($lock=~/\w/) {
                    545:           $num++;
                    546:           $texts{$lock}=$env{'session.lock.'.$lock};
                    547:        }
                    548:    }
                    549:    return ($num,%texts);
                    550: }
                    551: 
                    552: sub remove_lock {
                    553:     my ($id)=@_;
                    554:     my $newlocks='';
                    555:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                    556:        if (($lock=~/\w/) && ($lock ne $id)) {
                    557:           $newlocks.=','.$lock;
                    558:        }
                    559:     }
                    560:     &appenv({'session.locks' => $newlocks});
                    561:     &delenv('session.lock.'.$id);
                    562: }
                    563: 
                    564: sub remove_all_locks {
                    565:     my $activelocks=$env{'session.locks'};
                    566:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                    567:        if ($lock=~/\w/) {
                    568:           &remove_lock($lock);
                    569:        }
                    570:     }
                    571: }
                    572: 
                    573: 
1.369     albertel  574: # ------------------------------------------ Find out current server userload
                    575: sub userload {
                    576:     my $numusers=0;
                    577:     {
                    578: 	opendir(LONIDS,$perlvar{'lonIDsDir'});
                    579: 	my $filename;
                    580: 	my $curtime=time;
                    581: 	while ($filename=readdir(LONIDS)) {
1.925     albertel  582: 	    next if ($filename eq '.' || $filename eq '..');
                    583: 	    next if ($filename =~ /publicuser_\d+\.id/);
1.404     albertel  584: 	    my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
1.437     albertel  585: 	    if ($curtime-$mtime < 1800) { $numusers++; }
1.369     albertel  586: 	}
                    587: 	closedir(LONIDS);
                    588:     }
                    589:     my $userloadpercent=0;
                    590:     my $maxuserload=$perlvar{'lonUserLoadLim'};
                    591:     if ($maxuserload) {
1.371     albertel  592: 	$userloadpercent=100*$numusers/$maxuserload;
1.369     albertel  593:     }
1.372     albertel  594:     $userloadpercent=sprintf("%.2f",$userloadpercent);
1.369     albertel  595:     return $userloadpercent;
1.283     www       596: }
                    597: 
                    598: # ------------------------------------------ Fight off request when overloaded
                    599: 
                    600: sub overloaderror {
                    601:     my ($r,$checkserver)=@_;
                    602:     unless ($checkserver) { $checkserver=$perlvar{'lonHostID'}; }
                    603:     my $loadavg;
                    604:     if ($checkserver eq $perlvar{'lonHostID'}) {
1.448     albertel  605:        open(my $loadfile,'/proc/loadavg');
1.283     www       606:        $loadavg=<$loadfile>;
                    607:        $loadavg =~ s/\s.*//g;
1.285     matthew   608:        $loadavg = 100*$loadavg/$perlvar{'lonLoadLim'};
1.448     albertel  609:        close($loadfile);
1.283     www       610:     } else {
                    611:        $loadavg=&reply('load',$checkserver);
                    612:     }
1.285     matthew   613:     my $overload=$loadavg-100;
1.283     www       614:     if ($overload>0) {
1.285     matthew   615: 	$r->err_headers_out->{'Retry-After'}=$overload;
1.283     www       616:         $r->log_error('Overload of '.$overload.' on '.$checkserver);
1.554     www       617:         return 413;
1.283     www       618:     }    
                    619:     return '';
1.5       www       620: }
1.1       albertel  621: 
                    622: # ------------------------------ Find server with least workload from spare.tab
1.11      www       623: 
1.1       albertel  624: sub spareserver {
1.670     albertel  625:     my ($loadpercent,$userloadpercent,$want_server_name) = @_;
1.784     albertel  626:     my $spare_server;
1.370     albertel  627:     if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
1.784     albertel  628:     my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent 
                    629:                                                      :  $userloadpercent;
                    630:     
                    631:     foreach my $try_server (@{ $spareid{'primary'} }) {
                    632: 	($spare_server, $lowest_load) =
                    633: 	    &compare_server_load($try_server, $spare_server, $lowest_load);
                    634:     }
                    635: 
                    636:     my $found_server = ($spare_server ne '' && $lowest_load < 100);
                    637: 
                    638:     if (!$found_server) {
                    639: 	foreach my $try_server (@{ $spareid{'default'} }) {
                    640: 	    ($spare_server, $lowest_load) =
                    641: 		&compare_server_load($try_server, $spare_server, $lowest_load);
                    642: 	}
                    643:     }
                    644: 
                    645:     if (!$want_server_name) {
1.838     albertel  646: 	$spare_server="http://".&hostname($spare_server);
1.784     albertel  647:     }
                    648:     return $spare_server;
                    649: }
                    650: 
                    651: sub compare_server_load {
                    652:     my ($try_server, $spare_server, $lowest_load) = @_;
                    653: 
                    654:     my $loadans     = &reply('load',    $try_server);
                    655:     my $userloadans = &reply('userload',$try_server);
                    656: 
                    657:     if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
                    658: 	next; #didn't get a number from the server
                    659:     }
                    660: 
                    661:     my $load;
                    662:     if ($loadans =~ /\d/) {
                    663: 	if ($userloadans =~ /\d/) {
                    664: 	    #both are numbers, pick the bigger one
                    665: 	    $load = ($loadans > $userloadans) ? $loadans 
                    666: 		                              : $userloadans;
1.411     albertel  667: 	} else {
1.784     albertel  668: 	    $load = $loadans;
1.411     albertel  669: 	}
1.784     albertel  670:     } else {
                    671: 	$load = $userloadans;
                    672:     }
                    673: 
                    674:     if (($load =~ /\d/) && ($load < $lowest_load)) {
                    675: 	$spare_server = $try_server;
                    676: 	$lowest_load  = $load;
1.370     albertel  677:     }
1.784     albertel  678:     return ($spare_server,$lowest_load);
1.202     matthew   679: }
1.914     albertel  680: 
                    681: # --------------------------- ask offload servers if user already has a session
                    682: sub find_existing_session {
                    683:     my ($udom,$uname) = @_;
                    684:     foreach my $try_server (@{ $spareid{'primary'} },
                    685: 			    @{ $spareid{'default'} }) {
                    686: 	return $try_server if (&has_user_session($try_server, $udom, $uname));
                    687:     }
                    688:     return;
                    689: }
                    690: 
                    691: # -------------------------------- ask if server already has a session for user
                    692: sub has_user_session {
                    693:     my ($lonid,$udom,$uname) = @_;
                    694:     my $result = &reply(join(':','userhassession',
                    695: 			     map {&escape($_)} ($udom,$uname)),$lonid);
                    696:     return 1 if ($result eq 'ok');
                    697: 
                    698:     return 0;
                    699: }
                    700: 
1.202     matthew   701: # --------------------------------------------- Try to change a user's password
                    702: 
                    703: sub changepass {
1.799     raeburn   704:     my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
1.202     matthew   705:     $currentpass = &escape($currentpass);
                    706:     $newpass     = &escape($newpass);
1.799     raeburn   707:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context",
1.202     matthew   708: 		       $server);
                    709:     if (! $answer) {
                    710: 	&logthis("No reply on password change request to $server ".
                    711: 		 "by $uname in domain $udom.");
                    712:     } elsif ($answer =~ "^ok") {
                    713:         &logthis("$uname in $udom successfully changed their password ".
                    714: 		 "on $server.");
                    715:     } elsif ($answer =~ "^pwchange_failure") {
                    716: 	&logthis("$uname in $udom was unable to change their password ".
                    717: 		 "on $server.  The action was blocked by either lcpasswd ".
                    718: 		 "or pwchange");
                    719:     } elsif ($answer =~ "^non_authorized") {
                    720:         &logthis("$uname in $udom did not get their password correct when ".
                    721: 		 "attempting to change it on $server.");
                    722:     } elsif ($answer =~ "^auth_mode_error") {
                    723:         &logthis("$uname in $udom attempted to change their password despite ".
                    724: 		 "not being locally or internally authenticated on $server.");
                    725:     } elsif ($answer =~ "^unknown_user") {
                    726:         &logthis("$uname in $udom attempted to change their password ".
                    727: 		 "on $server but were unable to because $server is not ".
                    728: 		 "their home server.");
                    729:     } elsif ($answer =~ "^refused") {
                    730: 	&logthis("$server refused to change $uname in $udom password because ".
                    731: 		 "it was sent an unencrypted request to change the password.");
                    732:     }
                    733:     return $answer;
1.1       albertel  734: }
                    735: 
1.169     harris41  736: # ----------------------- Try to determine user's current authentication scheme
                    737: 
                    738: sub queryauthenticate {
                    739:     my ($uname,$udom)=@_;
1.456     albertel  740:     my $uhome=&homeserver($uname,$udom);
                    741:     if (!$uhome) {
                    742: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
                    743: 	return 'no_host';
                    744:     }
                    745:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
                    746:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
                    747: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.169     harris41  748:     }
1.456     albertel  749:     return $answer;
1.169     harris41  750: }
                    751: 
1.1       albertel  752: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11      www       753: 
1.1       albertel  754: sub authenticate {
1.952     raeburn   755:     my ($uname,$upass,$udom,$checkdefauth)=@_;
1.807     albertel  756:     $upass=&escape($upass);
                    757:     $uname= &LONCAPA::clean_username($uname);
1.836     www       758:     my $uhome=&homeserver($uname,$udom,1);
1.952     raeburn   759:     my $newhome;
1.836     www       760:     if ((!$uhome) || ($uhome eq 'no_host')) {
                    761: # Maybe the machine was offline and only re-appeared again recently?
                    762:         &reconlonc();
                    763: # One more
1.952     raeburn   764: 	$uhome=&homeserver($uname,$udom,1);
                    765:         if (($uhome eq 'no_host') && $checkdefauth) {
                    766:             if (defined(&domain($udom,'primary'))) {
                    767:                 $newhome=&domain($udom,'primary');
                    768:             }
                    769:             if ($newhome ne '') {
                    770:                 $uhome = $newhome;
                    771:             }
                    772:         }
1.836     www       773: 	if ((!$uhome) || ($uhome eq 'no_host')) {
                    774: 	    &logthis("User $uname at $udom is unknown in authenticate");
1.952     raeburn   775: 	    return 'no_host';
                    776:         }
1.1       albertel  777:     }
1.952     raeburn   778:     my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth",$uhome);
1.471     albertel  779:     if ($answer eq 'authorized') {
1.952     raeburn   780:         if ($newhome) {
                    781:             &logthis("User $uname at $udom authorized by $uhome, but needs account");
                    782:             return 'no_account_on_host'; 
                    783:         } else {
                    784:             &logthis("User $uname at $udom authorized by $uhome");
                    785:             return $uhome;
                    786:         }
1.471     albertel  787:     }
                    788:     if ($answer eq 'non_authorized') {
                    789: 	&logthis("User $uname at $udom rejected by $uhome");
                    790: 	return 'no_host'; 
1.9       www       791:     }
1.471     albertel  792:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.1       albertel  793:     return 'no_host';
                    794: }
                    795: 
                    796: # ---------------------- Find the homebase for a user from domain's lib servers
1.11      www       797: 
1.599     albertel  798: my %homecache;
1.1       albertel  799: sub homeserver {
1.230     stredwic  800:     my ($uname,$udom,$ignoreBadCache)=@_;
1.1       albertel  801:     my $index="$uname:$udom";
1.426     albertel  802: 
1.599     albertel  803:     if (exists($homecache{$index})) { return $homecache{$index}; }
1.841     albertel  804: 
                    805:     my %servers = &get_servers($udom,'library');
                    806:     foreach my $tryserver (keys(%servers)) {
1.230     stredwic  807:         next if ($ignoreBadCache ne 'true' && 
1.231     stredwic  808: 		 exists($badServerCache{$tryserver}));
1.841     albertel  809: 
                    810: 	my $answer=reply("home:$udom:$uname",$tryserver);
                    811: 	if ($answer eq 'found') {
                    812: 	    delete($badServerCache{$tryserver}); 
                    813: 	    return $homecache{$index}=$tryserver;
                    814: 	} elsif ($answer eq 'no_host') {
                    815: 	    $badServerCache{$tryserver}=1;
                    816: 	}
1.1       albertel  817:     }    
                    818:     return 'no_host';
1.70      www       819: }
                    820: 
                    821: # ------------------------------------- Find the usernames behind a list of IDs
                    822: 
                    823: sub idget {
                    824:     my ($udom,@ids)=@_;
                    825:     my %returnhash=();
                    826:     
1.841     albertel  827:     my %servers = &get_servers($udom,'library');
                    828:     foreach my $tryserver (keys(%servers)) {
                    829: 	my $idlist=join('&',@ids);
                    830: 	$idlist=~tr/A-Z/a-z/; 
                    831: 	my $reply=&reply("idget:$udom:".$idlist,$tryserver);
                    832: 	my @answer=();
                    833: 	if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
                    834: 	    @answer=split(/\&/,$reply);
                    835: 	}                    ;
                    836: 	my $i;
                    837: 	for ($i=0;$i<=$#ids;$i++) {
                    838: 	    if ($answer[$i]) {
                    839: 		$returnhash{$ids[$i]}=$answer[$i];
                    840: 	    } 
                    841: 	}
                    842:     } 
1.70      www       843:     return %returnhash;
                    844: }
                    845: 
                    846: # ------------------------------------- Find the IDs behind a list of usernames
                    847: 
                    848: sub idrget {
                    849:     my ($udom,@unames)=@_;
                    850:     my %returnhash=();
1.800     albertel  851:     foreach my $uname (@unames) {
                    852:         $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
1.191     harris41  853:     }
1.70      www       854:     return %returnhash;
                    855: }
                    856: 
                    857: # ------------------------------- Store away a list of names and associated IDs
                    858: 
                    859: sub idput {
                    860:     my ($udom,%ids)=@_;
                    861:     my %servers=();
1.800     albertel  862:     foreach my $uname (keys(%ids)) {
                    863: 	&cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
                    864:         my $uhom=&homeserver($uname,$udom);
1.70      www       865:         if ($uhom ne 'no_host') {
1.800     albertel  866:             my $id=&escape($ids{$uname});
1.70      www       867:             $id=~tr/A-Z/a-z/;
1.800     albertel  868:             my $esc_unam=&escape($uname);
1.70      www       869: 	    if ($servers{$uhom}) {
1.800     albertel  870: 		$servers{$uhom}.='&'.$id.'='.$esc_unam;
1.70      www       871:             } else {
1.800     albertel  872:                 $servers{$uhom}=$id.'='.$esc_unam;
1.70      www       873:             }
                    874:         }
1.191     harris41  875:     }
1.800     albertel  876:     foreach my $server (keys(%servers)) {
                    877:         &critical('idput:'.$udom.':'.$servers{$server},$server);
1.191     harris41  878:     }
1.344     www       879: }
                    880: 
1.806     raeburn   881: # ------------------------------------------- get items from domain db files   
                    882: 
                    883: sub get_dom {
1.860     raeburn   884:     my ($namespace,$storearr,$udom,$uhome)=@_;
1.806     raeburn   885:     my $items='';
                    886:     foreach my $item (@$storearr) {
                    887:         $items.=&escape($item).'&';
                    888:     }
                    889:     $items=~s/\&$//;
1.860     raeburn   890:     if (!$udom) {
                    891:         $udom=$env{'user.domain'};
                    892:         if (defined(&domain($udom,'primary'))) {
                    893:             $uhome=&domain($udom,'primary');
                    894:         } else {
1.874     albertel  895:             undef($uhome);
1.860     raeburn   896:         }
                    897:     } else {
                    898:         if (!$uhome) {
                    899:             if (defined(&domain($udom,'primary'))) {
                    900:                 $uhome=&domain($udom,'primary');
                    901:             }
                    902:         }
                    903:     }
                    904:     if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806     raeburn   905:         my $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
1.866     raeburn   906:         my %returnhash;
1.875     albertel  907:         if ($rep eq '' || $rep =~ /^error: 2 /) {
1.866     raeburn   908:             return %returnhash;
                    909:         }
1.806     raeburn   910:         my @pairs=split(/\&/,$rep);
                    911:         if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                    912:             return @pairs;
                    913:         }
                    914:         my $i=0;
                    915:         foreach my $item (@$storearr) {
                    916:             $returnhash{$item}=&thaw_unescape($pairs[$i]);
                    917:             $i++;
                    918:         }
                    919:         return %returnhash;
                    920:     } else {
1.880     banghart  921:         &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
1.806     raeburn   922:     }
                    923: }
                    924: 
                    925: # -------------------------------------------- put items in domain db files 
                    926: 
                    927: sub put_dom {
1.860     raeburn   928:     my ($namespace,$storehash,$udom,$uhome)=@_;
                    929:     if (!$udom) {
                    930:         $udom=$env{'user.domain'};
                    931:         if (defined(&domain($udom,'primary'))) {
                    932:             $uhome=&domain($udom,'primary');
                    933:         } else {
1.874     albertel  934:             undef($uhome);
1.860     raeburn   935:         }
                    936:     } else {
                    937:         if (!$uhome) {
                    938:             if (defined(&domain($udom,'primary'))) {
                    939:                 $uhome=&domain($udom,'primary');
                    940:             }
                    941:         }
                    942:     } 
                    943:     if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806     raeburn   944:         my $items='';
                    945:         foreach my $item (keys(%$storehash)) {
                    946:             $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
                    947:         }
                    948:         $items=~s/\&$//;
                    949:         return &reply("putdom:$udom:$namespace:$items",$uhome);
                    950:     } else {
1.860     raeburn   951:         &logthis("put_dom failed - no homeserver and/or domain");
1.806     raeburn   952:     }
                    953: }
                    954: 
1.837     raeburn   955: sub retrieve_inst_usertypes {
                    956:     my ($udom) = @_;
                    957:     my (%returnhash,@order);
1.846     albertel  958:     if (defined(&domain($udom,'primary'))) {
                    959:         my $uhome=&domain($udom,'primary');
1.837     raeburn   960:         my $rep=&reply("inst_usertypes:$udom",$uhome);
1.960     raeburn   961:         if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
                    962:             &logthis("get_dom failed - $rep returned from $uhome in domain: $udom");
                    963:             return (\%returnhash,\@order);
                    964:         }
1.837     raeburn   965:         my ($hashitems,$orderitems) = split(/:/,$rep); 
                    966:         my @pairs=split(/\&/,$hashitems);
                    967:         foreach my $item (@pairs) {
                    968:             my ($key,$value)=split(/=/,$item,2);
                    969:             $key = &unescape($key);
                    970:             next if ($key =~ /^error: 2 /);
                    971:             $returnhash{$key}=&thaw_unescape($value);
                    972:         }
                    973:         my @esc_order = split(/\&/,$orderitems);
                    974:         foreach my $item (@esc_order) {
                    975:             push(@order,&unescape($item));
                    976:         }
                    977:     } else {
                    978:         &logthis("get_dom failed - no primary domain server for $udom");
                    979:     }
                    980:     return (\%returnhash,\@order);
                    981: }
                    982: 
1.868     raeburn   983: sub is_domainimage {
                    984:     my ($url) = @_;
                    985:     if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo)/+-) {
                    986:         if (&domain($1) ne '') {
                    987:             return '1';
                    988:         }
                    989:     }
                    990:     return;
                    991: }
                    992: 
1.899     raeburn   993: sub inst_directory_query {
                    994:     my ($srch) = @_;
                    995:     my $udom = $srch->{'srchdomain'};
                    996:     my %results;
                    997:     my $homeserver = &domain($udom,'primary');
1.909     raeburn   998:     my $outcome;
1.899     raeburn   999:     if ($homeserver ne '') {
1.904     albertel 1000: 	my $queryid=&reply("querysend:instdirsearch:".
                   1001: 			   &escape($srch->{'srchby'}).':'.
                   1002: 			   &escape($srch->{'srchterm'}).':'.
                   1003: 			   &escape($srch->{'srchtype'}),$homeserver);
                   1004: 	my $host=&hostname($homeserver);
                   1005: 	if ($queryid !~/^\Q$host\E\_/) {
                   1006: 	    &logthis('instituional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
                   1007: 	    return;
                   1008: 	}
                   1009: 	my $response = &get_query_reply($queryid);
                   1010: 	my $maxtries = 5;
                   1011: 	my $tries = 1;
                   1012: 	while (($response=~/^timeout/) && ($tries < $maxtries)) {
                   1013: 	    $response = &get_query_reply($queryid);
                   1014: 	    $tries ++;
                   1015: 	}
                   1016: 
                   1017:         if (!&error($response) && $response ne 'refused') {
1.909     raeburn  1018:             if ($response eq 'unavailable') {
                   1019:                 $outcome = $response;
                   1020:             } else {
                   1021:                 $outcome = 'ok';
                   1022:                 my @matches = split(/\n/,$response);
                   1023:                 foreach my $match (@matches) {
                   1024:                     my ($key,$value) = split(/=/,$match);
                   1025:                     $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
                   1026:                 }
1.899     raeburn  1027:             }
                   1028:         }
                   1029:     }
1.909     raeburn  1030:     return ($outcome,%results);
1.899     raeburn  1031: }
                   1032: 
                   1033: sub usersearch {
                   1034:     my ($srch) = @_;
                   1035:     my $dom = $srch->{'srchdomain'};
                   1036:     my %results;
                   1037:     my %libserv = &all_library();
                   1038:     my $query = 'usersearch';
                   1039:     foreach my $tryserver (keys(%libserv)) {
                   1040:         if (&host_domain($tryserver) eq $dom) {
                   1041:             my $host=&hostname($tryserver);
                   1042:             my $queryid=
1.911     raeburn  1043:                 &reply("querysend:".&escape($query).':'.
                   1044:                        &escape($srch->{'srchby'}).':'.
1.899     raeburn  1045:                        &escape($srch->{'srchtype'}).':'.
                   1046:                        &escape($srch->{'srchterm'}),$tryserver);
                   1047:             if ($queryid !~/^\Q$host\E\_/) {
                   1048:                 &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
1.902     raeburn  1049:                 next;
1.899     raeburn  1050:             }
                   1051:             my $reply = &get_query_reply($queryid);
                   1052:             my $maxtries = 1;
                   1053:             my $tries = 1;
                   1054:             while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   1055:                 $reply = &get_query_reply($queryid);
                   1056:                 $tries ++;
                   1057:             }
                   1058:             if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   1059:                 &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') -  maxtries: '.$maxtries.' tries: '.$tries);
                   1060:             } else {
1.911     raeburn  1061:                 my @matches;
                   1062:                 if ($reply =~ /\n/) {
                   1063:                     @matches = split(/\n/,$reply);
                   1064:                 } else {
                   1065:                     @matches = split(/\&/,$reply);
                   1066:                 }
1.899     raeburn  1067:                 foreach my $match (@matches) {
                   1068:                     my ($uname,$udom,%userhash);
1.911     raeburn  1069:                     foreach my $entry (split(/:/,$match)) {
                   1070:                         my ($key,$value) =
                   1071:                             map {&unescape($_);} split(/=/,$entry);
1.899     raeburn  1072:                         $userhash{$key} = $value;
                   1073:                         if ($key eq 'username') {
                   1074:                             $uname = $value;
                   1075:                         } elsif ($key eq 'domain') {
                   1076:                             $udom = $value;
1.911     raeburn  1077:                         }
1.899     raeburn  1078:                     }
                   1079:                     $results{$uname.':'.$udom} = \%userhash;
                   1080:                 }
                   1081:             }
                   1082:         }
                   1083:     }
                   1084:     return %results;
                   1085: }
                   1086: 
1.912     raeburn  1087: sub get_instuser {
                   1088:     my ($udom,$uname,$id) = @_;
                   1089:     my $homeserver = &domain($udom,'primary');
                   1090:     my ($outcome,%results);
                   1091:     if ($homeserver ne '') {
                   1092:         my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
                   1093:                            &escape($id).':'.&escape($udom),$homeserver);
                   1094:         my $host=&hostname($homeserver);
                   1095:         if ($queryid !~/^\Q$host\E\_/) {
                   1096:             &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
                   1097:             return;
                   1098:         }
                   1099:         my $response = &get_query_reply($queryid);
                   1100:         my $maxtries = 5;
                   1101:         my $tries = 1;
                   1102:         while (($response=~/^timeout/) && ($tries < $maxtries)) {
                   1103:             $response = &get_query_reply($queryid);
                   1104:             $tries ++;
                   1105:         }
                   1106:         if (!&error($response) && $response ne 'refused') {
                   1107:             if ($response eq 'unavailable') {
                   1108:                 $outcome = $response;
                   1109:             } else {
                   1110:                 $outcome = 'ok';
                   1111:                 my @matches = split(/\n/,$response);
                   1112:                 foreach my $match (@matches) {
                   1113:                     my ($key,$value) = split(/=/,$match);
                   1114:                     $results{&unescape($key)} = &thaw_unescape($value);
                   1115:                 }
                   1116:             }
                   1117:         }
                   1118:     }
                   1119:     my %userinfo;
                   1120:     if (ref($results{$uname}) eq 'HASH') {
                   1121:         %userinfo = %{$results{$uname}};
                   1122:     } 
                   1123:     return ($outcome,%userinfo);
                   1124: }
                   1125: 
                   1126: sub inst_rulecheck {
1.923     raeburn  1127:     my ($udom,$uname,$id,$item,$rules) = @_;
1.912     raeburn  1128:     my %returnhash;
                   1129:     if ($udom ne '') {
                   1130:         if (ref($rules) eq 'ARRAY') {
                   1131:             @{$rules} = map {&escape($_);} (@{$rules});
                   1132:             my $rulestr = join(':',@{$rules});
                   1133:             my $homeserver=&domain($udom,'primary');
                   1134:             if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923     raeburn  1135:                 my $response;
                   1136:                 if ($item eq 'username') {                
                   1137:                     $response=&unescape(&reply('instrulecheck:'.&escape($udom).
                   1138:                                               ':'.&escape($uname).':'.$rulestr,
1.912     raeburn  1139:                                               $homeserver));
1.923     raeburn  1140:                 } elsif ($item eq 'id') {
                   1141:                     $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
                   1142:                                               ':'.&escape($id).':'.$rulestr,
                   1143:                                               $homeserver));
1.945     raeburn  1144:                 } elsif ($item eq 'selfcreate') {
                   1145:                     $response=&unescape(&reply('instselfcreatecheck:'.
1.943     raeburn  1146:                                                &escape($udom).':'.&escape($uname).
                   1147:                                               ':'.$rulestr,$homeserver));
1.923     raeburn  1148:                 }
1.912     raeburn  1149:                 if ($response ne 'refused') {
                   1150:                     my @pairs=split(/\&/,$response);
                   1151:                     foreach my $item (@pairs) {
                   1152:                         my ($key,$value)=split(/=/,$item,2);
                   1153:                         $key = &unescape($key);
                   1154:                         next if ($key =~ /^error: 2 /);
                   1155:                         $returnhash{$key}=&thaw_unescape($value);
                   1156:                     }
                   1157:                 }
                   1158:             }
                   1159:         }
                   1160:     }
                   1161:     return %returnhash;
                   1162: }
                   1163: 
                   1164: sub inst_userrules {
1.923     raeburn  1165:     my ($udom,$check) = @_;
1.912     raeburn  1166:     my (%ruleshash,@ruleorder);
                   1167:     if ($udom ne '') {
                   1168:         my $homeserver=&domain($udom,'primary');
                   1169:         if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923     raeburn  1170:             my $response;
                   1171:             if ($check eq 'id') {
                   1172:                 $response=&reply('instidrules:'.&escape($udom),
1.912     raeburn  1173:                                  $homeserver);
1.943     raeburn  1174:             } elsif ($check eq 'email') {
                   1175:                 $response=&reply('instemailrules:'.&escape($udom),
                   1176:                                  $homeserver);
1.923     raeburn  1177:             } else {
                   1178:                 $response=&reply('instuserrules:'.&escape($udom),
                   1179:                                  $homeserver);
                   1180:             }
1.912     raeburn  1181:             if (($response ne 'refused') && ($response ne 'error') && 
1.923     raeburn  1182:                 ($response ne 'unknown_cmd') && 
1.912     raeburn  1183:                 ($response ne 'no_such_host')) {
                   1184:                 my ($hashitems,$orderitems) = split(/:/,$response);
                   1185:                 my @pairs=split(/\&/,$hashitems);
                   1186:                 foreach my $item (@pairs) {
                   1187:                     my ($key,$value)=split(/=/,$item,2);
                   1188:                     $key = &unescape($key);
                   1189:                     next if ($key =~ /^error: 2 /);
                   1190:                     $ruleshash{$key}=&thaw_unescape($value);
                   1191:                 }
                   1192:                 my @esc_order = split(/\&/,$orderitems);
                   1193:                 foreach my $item (@esc_order) {
                   1194:                     push(@ruleorder,&unescape($item));
                   1195:                 }
                   1196:             }
                   1197:         }
                   1198:     }
                   1199:     return (\%ruleshash,\@ruleorder);
                   1200: }
                   1201: 
1.943     raeburn  1202: # ------------------------- Get Authentication and Language Defaults for Domain
                   1203: 
                   1204: sub get_domain_defaults {
                   1205:     my ($domain) = @_;
                   1206:     my $cachetime = 60*60*24;
                   1207:     my ($defauthtype,$defautharg,$deflang);
                   1208:     my ($result,$cached)=&is_cached_new('domdefaults',$domain);
                   1209:     if (defined($cached)) {
                   1210:         if (ref($result) eq 'HASH') {
                   1211:             return %{$result};
                   1212:         }
                   1213:     }
                   1214:     my %domdefaults;
                   1215:     my %domconfig =
                   1216:          &Apache::lonnet::get_dom('configuration',['defaults'],$domain);
                   1217:     if (ref($domconfig{'defaults'}) eq 'HASH') {
                   1218:         $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'}; 
                   1219:         $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
                   1220:         $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
                   1221:     } else {
                   1222:         $domdefaults{'lang_def'} = &domain($domain,'lang_def');
                   1223:         $domdefaults{'auth_def'} = &domain($domain,'auth_def');
                   1224:         $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
                   1225:     }
                   1226:     &Apache::lonnet::do_cache_new('domdefaults',$domain,\%domdefaults,
                   1227:                                   $cachetime);
                   1228:     return %domdefaults;
                   1229: }
                   1230: 
1.344     www      1231: # --------------------------------------------------- Assign a key to a student
                   1232: 
                   1233: sub assign_access_key {
1.364     www      1234: #
                   1235: # a valid key looks like uname:udom#comments
                   1236: # comments are being appended
                   1237: #
1.498     www      1238:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
                   1239:     $kdom=
1.620     albertel 1240:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
1.498     www      1241:     $knum=
1.620     albertel 1242:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
1.344     www      1243:     $cdom=
1.620     albertel 1244:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      1245:     $cnum=
1.620     albertel 1246:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
                   1247:     $udom=$env{'user.name'} unless (defined($udom));
                   1248:     $uname=$env{'user.domain'} unless (defined($uname));
1.498     www      1249:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
1.364     www      1250:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
1.479     albertel 1251:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
1.364     www      1252:                                                   # assigned to this person
                   1253:                                                   # - this should not happen,
1.345     www      1254:                                                   # unless something went wrong
                   1255:                                                   # the first time around
                   1256: # ready to assign
1.364     www      1257:         $logentry=$1.'; '.$logentry;
1.496     www      1258:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
1.498     www      1259:                                                  $kdom,$knum) eq 'ok') {
1.345     www      1260: # key now belongs to user
1.346     www      1261: 	    my $envkey='key.'.$cdom.'_'.$cnum;
1.345     www      1262:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
1.949     raeburn  1263:                 &appenv({'environment.'.$envkey => $ckey});
1.345     www      1264:                 return 'ok';
                   1265:             } else {
                   1266:                 return 
                   1267:   'error: Count not permanently assign key, will need to be re-entered later.';
                   1268: 	    }
                   1269:         } else {
                   1270:             return 'error: Could not assign key, try again later.';
                   1271:         }
1.364     www      1272:     } elsif (!$existing{$ckey}) {
1.345     www      1273: # the key does not exist
                   1274: 	return 'error: The key does not exist';
                   1275:     } else {
                   1276: # the key is somebody else's
                   1277: 	return 'error: The key is already in use';
                   1278:     }
1.344     www      1279: }
                   1280: 
1.364     www      1281: # ------------------------------------------ put an additional comment on a key
                   1282: 
                   1283: sub comment_access_key {
                   1284: #
                   1285: # a valid key looks like uname:udom#comments
                   1286: # comments are being appended
                   1287: #
                   1288:     my ($ckey,$cdom,$cnum,$logentry)=@_;
                   1289:     $cdom=
1.620     albertel 1290:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.364     www      1291:     $cnum=
1.620     albertel 1292:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.364     www      1293:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
                   1294:     if ($existing{$ckey}) {
                   1295:         $existing{$ckey}.='; '.$logentry;
                   1296: # ready to assign
1.367     www      1297:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
1.364     www      1298:                                                  $cdom,$cnum) eq 'ok') {
                   1299: 	    return 'ok';
                   1300:         } else {
                   1301: 	    return 'error: Count not store comment.';
                   1302:         }
                   1303:     } else {
                   1304: # the key does not exist
                   1305: 	return 'error: The key does not exist';
                   1306:     }
                   1307: }
                   1308: 
1.344     www      1309: # ------------------------------------------------------ Generate a set of keys
                   1310: 
                   1311: sub generate_access_keys {
1.364     www      1312:     my ($number,$cdom,$cnum,$logentry)=@_;
1.344     www      1313:     $cdom=
1.620     albertel 1314:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      1315:     $cnum=
1.620     albertel 1316:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.361     www      1317:     unless (&allowed('mky',$cdom)) { return 0; }
1.344     www      1318:     unless (($cdom) && ($cnum)) { return 0; }
                   1319:     if ($number>10000) { return 0; }
                   1320:     sleep(2); # make sure don't get same seed twice
                   1321:     srand(time()^($$+($$<<15))); # from "Programming Perl"
                   1322:     my $total=0;
                   1323:     for (my $i=1;$i<=$number;$i++) {
                   1324:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
                   1325:                   sprintf("%lx",int(100000*rand)).'-'.
                   1326:                   sprintf("%lx",int(100000*rand));
                   1327:        $newkey=~s/1/g/g; # folks mix up 1 and l
                   1328:        $newkey=~s/0/h/g; # and also 0 and O
                   1329:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
                   1330:        if ($existing{$newkey}) {
                   1331:            $i--;
                   1332:        } else {
1.364     www      1333: 	  if (&put('accesskeys',
                   1334:               { $newkey => '# generated '.localtime().
1.620     albertel 1335:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
1.364     www      1336:                            '; '.$logentry },
                   1337: 		   $cdom,$cnum) eq 'ok') {
1.344     www      1338:               $total++;
                   1339: 	  }
                   1340:        }
                   1341:     }
1.620     albertel 1342:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.344     www      1343:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
                   1344:     return $total;
                   1345: }
                   1346: 
                   1347: # ------------------------------------------------------- Validate an accesskey
                   1348: 
                   1349: sub validate_access_key {
                   1350:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
                   1351:     $cdom=
1.620     albertel 1352:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      1353:     $cnum=
1.620     albertel 1354:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
                   1355:     $udom=$env{'user.domain'} unless (defined($udom));
                   1356:     $uname=$env{'user.name'} unless (defined($uname));
1.345     www      1357:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1.479     albertel 1358:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
1.70      www      1359: }
                   1360: 
                   1361: # ------------------------------------- Find the section of student in a course
1.652     albertel 1362: sub devalidate_getsection_cache {
                   1363:     my ($udom,$unam,$courseid)=@_;
                   1364:     my $hashid="$udom:$unam:$courseid";
                   1365:     &devalidate_cache_new('getsection',$hashid);
                   1366: }
1.298     matthew  1367: 
1.815     albertel 1368: sub courseid_to_courseurl {
                   1369:     my ($courseid) = @_;
                   1370:     #already url style courseid
                   1371:     return $courseid if ($courseid =~ m{^/});
                   1372: 
                   1373:     if (exists($env{'course.'.$courseid.'.num'})) {
                   1374: 	my $cnum = $env{'course.'.$courseid.'.num'};
                   1375: 	my $cdom = $env{'course.'.$courseid.'.domain'};
                   1376: 	return "/$cdom/$cnum";
                   1377:     }
                   1378: 
                   1379:     my %courseinfo=&Apache::lonnet::coursedescription($courseid);
                   1380:     if (exists($courseinfo{'num'})) {
                   1381: 	return "/$courseinfo{'domain'}/$courseinfo{'num'}";
                   1382:     }
                   1383: 
                   1384:     return undef;
                   1385: }
                   1386: 
1.298     matthew  1387: sub getsection {
                   1388:     my ($udom,$unam,$courseid)=@_;
1.599     albertel 1389:     my $cachetime=1800;
1.551     albertel 1390: 
                   1391:     my $hashid="$udom:$unam:$courseid";
1.599     albertel 1392:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
1.551     albertel 1393:     if (defined($cached)) { return $result; }
                   1394: 
1.298     matthew  1395:     my %Pending; 
                   1396:     my %Expired;
                   1397:     #
                   1398:     # Each role can either have not started yet (pending), be active, 
                   1399:     #    or have expired.
                   1400:     #
                   1401:     # If there is an active role, we are done.
                   1402:     #
                   1403:     # If there is more than one role which has not started yet, 
                   1404:     #     choose the one which will start sooner
                   1405:     # If there is one role which has not started yet, return it.
                   1406:     #
                   1407:     # If there is more than one expired role, choose the one which ended last.
                   1408:     # If there is a role which has expired, return it.
                   1409:     #
1.815     albertel 1410:     $courseid = &courseid_to_courseurl($courseid);
1.817     raeburn  1411:     my %roleshash = &dump('roles',$udom,$unam,$courseid);
                   1412:     foreach my $key (keys(%roleshash)) {
1.479     albertel 1413:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1.298     matthew  1414:         my $section=$1;
                   1415:         if ($key eq $courseid.'_st') { $section=''; }
1.817     raeburn  1416:         my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
1.298     matthew  1417:         my $now=time;
1.548     albertel 1418:         if (defined($end) && $end && ($now > $end)) {
1.298     matthew  1419:             $Expired{$end}=$section;
                   1420:             next;
                   1421:         }
1.548     albertel 1422:         if (defined($start) && $start && ($now < $start)) {
1.298     matthew  1423:             $Pending{$start}=$section;
                   1424:             next;
                   1425:         }
1.599     albertel 1426:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
1.298     matthew  1427:     }
                   1428:     #
                   1429:     # Presumedly there will be few matching roles from the above
                   1430:     # loop and the sorting time will be negligible.
                   1431:     if (scalar(keys(%Pending))) {
                   1432:         my ($time) = sort {$a <=> $b} keys(%Pending);
1.599     albertel 1433:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
1.298     matthew  1434:     } 
                   1435:     if (scalar(keys(%Expired))) {
                   1436:         my @sorted = sort {$a <=> $b} keys(%Expired);
                   1437:         my $time = pop(@sorted);
1.599     albertel 1438:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
1.298     matthew  1439:     }
1.599     albertel 1440:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
1.298     matthew  1441: }
1.70      www      1442: 
1.599     albertel 1443: sub save_cache {
                   1444:     &purge_remembered();
1.722     albertel 1445:     #&Apache::loncommon::validate_page();
1.620     albertel 1446:     undef(%env);
1.780     albertel 1447:     undef($env_loaded);
1.599     albertel 1448: }
1.452     albertel 1449: 
1.599     albertel 1450: my $to_remember=-1;
                   1451: my %remembered;
                   1452: my %accessed;
                   1453: my $kicks=0;
                   1454: my $hits=0;
1.849     albertel 1455: sub make_key {
                   1456:     my ($name,$id) = @_;
1.872     albertel 1457:     if (length($id) > 65 
                   1458: 	&& length(&escape($id)) > 200) {
                   1459: 	$id=length($id).':'.&Digest::MD5::md5_hex($id);
                   1460:     }
1.849     albertel 1461:     return &escape($name.':'.$id);
                   1462: }
                   1463: 
1.599     albertel 1464: sub devalidate_cache_new {
                   1465:     my ($name,$id,$debug) = @_;
                   1466:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
1.849     albertel 1467:     $id=&make_key($name,$id);
1.599     albertel 1468:     $memcache->delete($id);
                   1469:     delete($remembered{$id});
                   1470:     delete($accessed{$id});
                   1471: }
                   1472: 
                   1473: sub is_cached_new {
                   1474:     my ($name,$id,$debug) = @_;
1.849     albertel 1475:     $id=&make_key($name,$id);
1.599     albertel 1476:     if (exists($remembered{$id})) {
                   1477: 	if ($debug) { &Apache::lonnet::logthis("Earyl return $id of $remembered{$id} "); }
                   1478: 	$accessed{$id}=[&gettimeofday()];
                   1479: 	$hits++;
                   1480: 	return ($remembered{$id},1);
                   1481:     }
                   1482:     my $value = $memcache->get($id);
                   1483:     if (!(defined($value))) {
                   1484: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
1.417     albertel 1485: 	return (undef,undef);
1.416     albertel 1486:     }
1.599     albertel 1487:     if ($value eq '__undef__') {
                   1488: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
                   1489: 	$value=undef;
                   1490:     }
                   1491:     &make_room($id,$value,$debug);
                   1492:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
                   1493:     return ($value,1);
                   1494: }
                   1495: 
                   1496: sub do_cache_new {
                   1497:     my ($name,$id,$value,$time,$debug) = @_;
1.849     albertel 1498:     $id=&make_key($name,$id);
1.599     albertel 1499:     my $setvalue=$value;
                   1500:     if (!defined($setvalue)) {
                   1501: 	$setvalue='__undef__';
                   1502:     }
1.623     albertel 1503:     if (!defined($time) ) {
                   1504: 	$time=600;
                   1505:     }
1.599     albertel 1506:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
1.910     albertel 1507:     my $result = $memcache->set($id,$setvalue,$time);
                   1508:     if (! $result) {
1.872     albertel 1509: 	&logthis("caching of id -> $id  failed");
1.910     albertel 1510: 	$memcache->disconnect_all();
1.872     albertel 1511:     }
1.600     albertel 1512:     # need to make a copy of $value
1.919     albertel 1513:     &make_room($id,$value,$debug);
1.599     albertel 1514:     return $value;
                   1515: }
                   1516: 
                   1517: sub make_room {
                   1518:     my ($id,$value,$debug)=@_;
1.919     albertel 1519: 
                   1520:     $remembered{$id}= (ref($value)) ? &Storable::dclone($value)
                   1521:                                     : $value;
1.599     albertel 1522:     if ($to_remember<0) { return; }
                   1523:     $accessed{$id}=[&gettimeofday()];
                   1524:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
                   1525:     my $to_kick;
                   1526:     my $max_time=0;
                   1527:     foreach my $other (keys(%accessed)) {
                   1528: 	if (&tv_interval($accessed{$other}) > $max_time) {
                   1529: 	    $to_kick=$other;
                   1530: 	    $max_time=&tv_interval($accessed{$other});
                   1531: 	}
                   1532:     }
                   1533:     delete($remembered{$to_kick});
                   1534:     delete($accessed{$to_kick});
                   1535:     $kicks++;
                   1536:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
1.541     albertel 1537:     return;
                   1538: }
                   1539: 
1.599     albertel 1540: sub purge_remembered {
1.604     albertel 1541:     #&logthis("Tossing ".scalar(keys(%remembered)));
                   1542:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
1.599     albertel 1543:     undef(%remembered);
                   1544:     undef(%accessed);
1.428     albertel 1545: }
1.70      www      1546: # ------------------------------------- Read an entry from a user's environment
                   1547: 
                   1548: sub userenvironment {
                   1549:     my ($udom,$unam,@what)=@_;
                   1550:     my %returnhash=();
                   1551:     my @answer=split(/\&/,
                   1552:                 &reply('get:'.$udom.':'.$unam.':environment:'.join('&',@what),
                   1553:                       &homeserver($unam,$udom)));
                   1554:     my $i;
                   1555:     for ($i=0;$i<=$#what;$i++) {
                   1556: 	$returnhash{$what[$i]}=&unescape($answer[$i]);
                   1557:     }
                   1558:     return %returnhash;
1.1       albertel 1559: }
                   1560: 
1.617     albertel 1561: # ---------------------------------------------------------- Get a studentphoto
                   1562: sub studentphoto {
                   1563:     my ($udom,$unam,$ext) = @_;
                   1564:     my $home=&Apache::lonnet::homeserver($unam,$udom);
1.706     raeburn  1565:     if (defined($env{'request.course.id'})) {
1.708     raeburn  1566:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.706     raeburn  1567:             if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
                   1568:                 return(&retrievestudentphoto($udom,$unam,$ext)); 
                   1569:             } else {
                   1570:                 my ($result,$perm_reqd)=
1.707     albertel 1571: 		    &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706     raeburn  1572:                 if ($result eq 'ok') {
                   1573:                     if (!($perm_reqd eq 'yes')) {
                   1574:                         return(&retrievestudentphoto($udom,$unam,$ext));
                   1575:                     }
                   1576:                 }
                   1577:             }
                   1578:         }
                   1579:     } else {
                   1580:         my ($result,$perm_reqd) = 
1.707     albertel 1581: 	    &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706     raeburn  1582:         if ($result eq 'ok') {
                   1583:             if (!($perm_reqd eq 'yes')) {
                   1584:                 return(&retrievestudentphoto($udom,$unam,$ext));
                   1585:             }
                   1586:         }
                   1587:     }
                   1588:     return '/adm/lonKaputt/lonlogo_broken.gif';
                   1589: }
                   1590: 
                   1591: sub retrievestudentphoto {
                   1592:     my ($udom,$unam,$ext,$type) = @_;
                   1593:     my $home=&Apache::lonnet::homeserver($unam,$udom);
                   1594:     my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
                   1595:     if ($ret eq 'ok') {
                   1596:         my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
                   1597:         if ($type eq 'thumbnail') {
                   1598:             $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext"; 
                   1599:         }
                   1600:         my $tokenurl=&Apache::lonnet::tokenwrapper($url);
                   1601:         return $tokenurl;
                   1602:     } else {
                   1603:         if ($type eq 'thumbnail') {
                   1604:             return '/adm/lonKaputt/genericstudent_tn.gif';
                   1605:         } else { 
                   1606:             return '/adm/lonKaputt/lonlogo_broken.gif';
                   1607:         }
1.617     albertel 1608:     }
                   1609: }
                   1610: 
1.263     www      1611: # -------------------------------------------------------------------- New chat
                   1612: 
                   1613: sub chatsend {
1.724     raeburn  1614:     my ($newentry,$anon,$group)=@_;
1.620     albertel 1615:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
                   1616:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   1617:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.263     www      1618:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
1.620     albertel 1619: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
1.724     raeburn  1620: 		   &escape($newentry)).':'.$group,$chome);
1.292     www      1621: }
                   1622: 
                   1623: # ------------------------------------------ Find current version of a resource
                   1624: 
                   1625: sub getversion {
                   1626:     my $fname=&clutter(shift);
                   1627:     unless ($fname=~/^\/res\//) { return -1; }
                   1628:     return &currentversion(&filelocation('',$fname));
                   1629: }
                   1630: 
                   1631: sub currentversion {
                   1632:     my $fname=shift;
1.599     albertel 1633:     my ($result,$cached)=&is_cached_new('resversion',$fname);
1.440     www      1634:     if (defined($cached)) { return $result; }
1.292     www      1635:     my $author=$fname;
                   1636:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   1637:     my ($udom,$uname)=split(/\//,$author);
                   1638:     my $home=homeserver($uname,$udom);
                   1639:     if ($home eq 'no_host') { 
                   1640:         return -1; 
                   1641:     }
                   1642:     my $answer=reply("currentversion:$fname",$home);
                   1643:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   1644: 	return -1;
                   1645:     }
1.599     albertel 1646:     return &do_cache_new('resversion',$fname,$answer,600);
1.263     www      1647: }
                   1648: 
1.1       albertel 1649: # ----------------------------- Subscribe to a resource, return URL if possible
1.11      www      1650: 
1.1       albertel 1651: sub subscribe {
                   1652:     my $fname=shift;
1.761     raeburn  1653:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1.532     albertel 1654:     $fname=~s/[\n\r]//g;
1.1       albertel 1655:     my $author=$fname;
                   1656:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   1657:     my ($udom,$uname)=split(/\//,$author);
                   1658:     my $home=homeserver($uname,$udom);
1.335     albertel 1659:     if ($home eq 'no_host') {
                   1660:         return 'not_found';
1.1       albertel 1661:     }
                   1662:     my $answer=reply("sub:$fname",$home);
1.64      www      1663:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   1664: 	$answer.=' by '.$home;
                   1665:     }
1.1       albertel 1666:     return $answer;
                   1667: }
                   1668:     
1.8       www      1669: # -------------------------------------------------------------- Replicate file
                   1670: 
                   1671: sub repcopy {
                   1672:     my $filename=shift;
1.23      www      1673:     $filename=~s/\/+/\//g;
1.607     raeburn  1674:     if ($filename=~m|^/home/httpd/html/adm/|) { return 'ok'; }
                   1675:     if ($filename=~m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1.538     albertel 1676:     if ($filename=~m|^/home/httpd/html/userfiles/| or
1.609     banghart 1677: 	$filename=~m -^/*(uploaded|editupload)/-) { 
1.538     albertel 1678: 	return &repcopy_userfile($filename);
                   1679:     }
1.532     albertel 1680:     $filename=~s/[\n\r]//g;
1.8       www      1681:     my $transname="$filename.in.transfer";
1.828     www      1682: # FIXME: this should flock
1.607     raeburn  1683:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
1.8       www      1684:     my $remoteurl=subscribe($filename);
1.64      www      1685:     if ($remoteurl =~ /^con_lost by/) {
                   1686: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.607     raeburn  1687:            return 'unavailable';
1.8       www      1688:     } elsif ($remoteurl eq 'not_found') {
1.441     albertel 1689: 	   #&logthis("Subscribe returned not_found: $filename");
1.607     raeburn  1690: 	   return 'not_found';
1.64      www      1691:     } elsif ($remoteurl =~ /^rejected by/) {
                   1692: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.607     raeburn  1693:            return 'forbidden';
1.20      www      1694:     } elsif ($remoteurl eq 'directory') {
1.607     raeburn  1695:            return 'ok';
1.8       www      1696:     } else {
1.290     www      1697:         my $author=$filename;
                   1698:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   1699:         my ($udom,$uname)=split(/\//,$author);
                   1700:         my $home=homeserver($uname,$udom);
                   1701:         unless ($home eq $perlvar{'lonHostID'}) {
1.8       www      1702:            my @parts=split(/\//,$filename);
                   1703:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
                   1704:            if ($path ne "$perlvar{'lonDocRoot'}/res") {
                   1705:                &logthis("Malconfiguration for replication: $filename");
1.607     raeburn  1706: 	       return 'bad_request';
1.8       www      1707:            }
                   1708:            my $count;
                   1709:            for ($count=5;$count<$#parts;$count++) {
                   1710:                $path.="/$parts[$count]";
                   1711:                if ((-e $path)!=1) {
                   1712: 		   mkdir($path,0777);
                   1713:                }
                   1714:            }
                   1715:            my $ua=new LWP::UserAgent;
                   1716:            my $request=new HTTP::Request('GET',"$remoteurl");
                   1717:            my $response=$ua->request($request,$transname);
                   1718:            if ($response->is_error()) {
                   1719: 	       unlink($transname);
                   1720:                my $message=$response->status_line;
1.672     albertel 1721:                &logthis("<font color=\"blue\">WARNING:"
1.12      www      1722:                        ." LWP get: $message: $filename</font>");
1.607     raeburn  1723:                return 'unavailable';
1.8       www      1724:            } else {
1.16      www      1725: 	       if ($remoteurl!~/\.meta$/) {
                   1726:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
                   1727:                   my $mresponse=$ua->request($mrequest,$filename.'.meta');
                   1728:                   if ($mresponse->is_error()) {
                   1729: 		      unlink($filename.'.meta');
                   1730:                       &logthis(
1.672     albertel 1731:                      "<font color=\"yellow\">INFO: No metadata: $filename</font>");
1.16      www      1732:                   }
                   1733: 	       }
1.8       www      1734:                rename($transname,$filename);
1.607     raeburn  1735:                return 'ok';
1.8       www      1736:            }
1.290     www      1737:        }
1.8       www      1738:     }
1.330     www      1739: }
                   1740: 
                   1741: # ------------------------------------------------ Get server side include body
                   1742: sub ssi_body {
1.381     albertel 1743:     my ($filelink,%form)=@_;
1.606     matthew  1744:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
                   1745:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
                   1746:     }
1.953     www      1747:     my $output='';
                   1748:     my $response;
                   1749:     if ($filelink=~/^http\:/) {
1.954     raeburn  1750:        ($output,$response)=&externalssi($filelink);
1.953     www      1751:     } else {
                   1752:        ($output,$response)=&ssi($filelink,%form);
                   1753:     }
1.778     albertel 1754:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
1.451     albertel 1755:     $output=~s/^.*?\<body[^\>]*\>//si;
1.930     albertel 1756:     $output=~s/\<\/body\s*\>.*?$//si;
1.953     www      1757:     if (wantarray) {
                   1758:         return ($output, $response);
                   1759:     } else {
                   1760:         return $output;
                   1761:     }
1.8       www      1762: }
                   1763: 
1.15      www      1764: # --------------------------------------------------------- Server Side Include
                   1765: 
1.782     albertel 1766: sub absolute_url {
                   1767:     my ($host_name) = @_;
                   1768:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
                   1769:     if ($host_name eq '') {
                   1770: 	$host_name = $ENV{'SERVER_NAME'};
                   1771:     }
                   1772:     return $protocol.$host_name;
                   1773: }
                   1774: 
1.942     foxr     1775: #
                   1776: #   Server side include.
                   1777: # Parameters:
                   1778: #  fn     Possibly encrypted resource name/id.
                   1779: #  form   Hash that describes how the rendering should be done
                   1780: #         and other things.
1.944     foxr     1781: # Returns:
1.950     raeburn  1782: #   Scalar context: The content of the response.
                   1783: #   Array context:  2 element list of the content and the full response object.
1.942     foxr     1784: #     
1.15      www      1785: sub ssi {
                   1786: 
1.944     foxr     1787:     my ($fn,%form)=@_;
1.15      www      1788:     my $ua=new LWP::UserAgent;
1.23      www      1789:     my $request;
1.711     albertel 1790: 
                   1791:     $form{'no_update_last_known'}=1;
1.895     albertel 1792:     &Apache::lonenc::check_encrypt(\$fn);
1.23      www      1793:     if (%form) {
1.782     albertel 1794:       $request=new HTTP::Request('POST',&absolute_url().$fn);
1.201     albertel 1795:       $request->content(join('&',map { &escape($_).'='.&escape($form{$_}) } keys %form));
1.23      www      1796:     } else {
1.782     albertel 1797:       $request=new HTTP::Request('GET',&absolute_url().$fn);
1.23      www      1798:     }
                   1799: 
1.15      www      1800:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
                   1801:     my $response=$ua->request($request);
                   1802: 
1.944     foxr     1803:     if (wantarray) {
                   1804: 	return ($response->content, $response);
                   1805:     } else {
                   1806: 	return $response->content;
1.942     foxr     1807:     }
1.324     www      1808: }
                   1809: 
                   1810: sub externalssi {
                   1811:     my ($url)=@_;
                   1812:     my $ua=new LWP::UserAgent;
                   1813:     my $request=new HTTP::Request('GET',$url);
                   1814:     my $response=$ua->request($request);
1.954     raeburn  1815:     if (wantarray) {
                   1816:         return ($response->content, $response);
                   1817:     } else {
                   1818:         return $response->content;
                   1819:     }
1.15      www      1820: }
1.254     www      1821: 
1.492     albertel 1822: # -------------------------------- Allow a /uploaded/ URI to be vouched for
                   1823: 
                   1824: sub allowuploaded {
                   1825:     my ($srcurl,$url)=@_;
                   1826:     $url=&clutter(&declutter($url));
                   1827:     my $dir=$url;
                   1828:     $dir=~s/\/[^\/]+$//;
                   1829:     my %httpref=();
                   1830:     my $httpurl=&hreflocation('',$url);
                   1831:     $httpref{'httpref.'.$httpurl}=$srcurl;
1.949     raeburn  1832:     &Apache::lonnet::appenv(\%httpref);
1.254     www      1833: }
1.477     raeburn  1834: 
1.478     albertel 1835: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1.638     albertel 1836: # input: action, courseID, current domain, intended
1.637     raeburn  1837: #        path to file, source of file, instruction to parse file for objects,
                   1838: #        ref to hash for embedded objects,
                   1839: #        ref to hash for codebase of java objects.
                   1840: #
1.485     raeburn  1841: # output: url to file (if action was uploaddoc), 
                   1842: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1.477     raeburn  1843: #
1.478     albertel 1844: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
                   1845: # course.
1.477     raeburn  1846: #
1.478     albertel 1847: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   1848: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
                   1849: #          course's home server.
1.477     raeburn  1850: #
1.478     albertel 1851: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
                   1852: #          be copied from $source (current location) to 
                   1853: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   1854: #         and will then be copied to
                   1855: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
                   1856: #         course's home server.
1.485     raeburn  1857: #
1.481     raeburn  1858: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1.620     albertel 1859: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
1.481     raeburn  1860: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   1861: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
                   1862: #         in course's home server.
1.637     raeburn  1863: #
1.477     raeburn  1864: 
                   1865: sub process_coursefile {
1.638     albertel 1866:     my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase)=@_;
1.477     raeburn  1867:     my $fetchresult;
1.638     albertel 1868:     my $home=&homeserver($docuname,$docudom);
1.477     raeburn  1869:     if ($action eq 'propagate') {
1.638     albertel 1870:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
                   1871: 			     $home);
1.481     raeburn  1872:     } else {
1.477     raeburn  1873:         my $fpath = '';
                   1874:         my $fname = $file;
1.478     albertel 1875:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1.477     raeburn  1876:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1.637     raeburn  1877:         my $filepath = &build_filepath($fpath);
1.481     raeburn  1878:         if ($action eq 'copy') {
                   1879:             if ($source eq '') {
                   1880:                 $fetchresult = 'no source file';
                   1881:                 return $fetchresult;
                   1882:             } else {
                   1883:                 my $destination = $filepath.'/'.$fname;
                   1884:                 rename($source,$destination);
                   1885:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 1886:                                  $home);
1.481     raeburn  1887:             }
                   1888:         } elsif ($action eq 'uploaddoc') {
                   1889:             open(my $fh,'>'.$filepath.'/'.$fname);
1.620     albertel 1890:             print $fh $env{'form.'.$source};
1.481     raeburn  1891:             close($fh);
1.637     raeburn  1892:             if ($parser eq 'parse') {
1.961     raeburn  1893:                 my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
1.637     raeburn  1894:                 unless ($parse_result eq 'ok') {
                   1895:                     &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
                   1896:                 }
                   1897:             }
1.477     raeburn  1898:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 1899:                                  $home);
1.481     raeburn  1900:             if ($fetchresult eq 'ok') {
                   1901:                 return '/uploaded/'.$fpath.'/'.$fname;
                   1902:             } else {
                   1903:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638     albertel 1904:                         ' to host '.$home.': '.$fetchresult);
1.481     raeburn  1905:                 return '/adm/notfound.html';
                   1906:             }
1.477     raeburn  1907:         }
                   1908:     }
1.485     raeburn  1909:     unless ( $fetchresult eq 'ok') {
1.477     raeburn  1910:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638     albertel 1911:              ' to host '.$home.': '.$fetchresult);
1.477     raeburn  1912:     }
                   1913:     return $fetchresult;
                   1914: }
                   1915: 
1.637     raeburn  1916: sub build_filepath {
                   1917:     my ($fpath) = @_;
                   1918:     my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
                   1919:     unless ($fpath eq '') {
                   1920:         my @parts=split('/',$fpath);
                   1921:         foreach my $part (@parts) {
                   1922:             $filepath.= '/'.$part;
                   1923:             if ((-e $filepath)!=1) {
                   1924:                 mkdir($filepath,0777);
                   1925:             }
                   1926:         }
                   1927:     }
                   1928:     return $filepath;
                   1929: }
                   1930: 
                   1931: sub store_edited_file {
1.638     albertel 1932:     my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
1.637     raeburn  1933:     my $file = $primary_url;
                   1934:     $file =~ s#^/uploaded/$docudom/$docuname/##;
                   1935:     my $fpath = '';
                   1936:     my $fname = $file;
                   1937:     ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
                   1938:     $fpath=$docudom.'/'.$docuname.'/'.$fpath;
                   1939:     my $filepath = &build_filepath($fpath);
                   1940:     open(my $fh,'>'.$filepath.'/'.$fname);
                   1941:     print $fh $content;
                   1942:     close($fh);
1.638     albertel 1943:     my $home=&homeserver($docuname,$docudom);
1.637     raeburn  1944:     $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 1945: 			  $home);
1.637     raeburn  1946:     if ($$fetchresult eq 'ok') {
                   1947:         return '/uploaded/'.$fpath.'/'.$fname;
                   1948:     } else {
1.638     albertel 1949:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
                   1950: 		 ' to host '.$home.': '.$$fetchresult);
1.637     raeburn  1951:         return '/adm/notfound.html';
                   1952:     }
                   1953: }
                   1954: 
1.531     albertel 1955: sub clean_filename {
1.831     albertel 1956:     my ($fname,$args)=@_;
1.315     www      1957: # Replace Windows backslashes by forward slashes
1.257     www      1958:     $fname=~s/\\/\//g;
1.831     albertel 1959:     if (!$args->{'keep_path'}) {
                   1960:         # Get rid of everything but the actual filename
                   1961: 	$fname=~s/^.*\/([^\/]+)$/$1/;
                   1962:     }
1.315     www      1963: # Replace spaces by underscores
                   1964:     $fname=~s/\s+/\_/g;
                   1965: # Replace all other weird characters by nothing
1.831     albertel 1966:     $fname=~s{[^/\w\.\-]}{}g;
1.540     albertel 1967: # Replace all .\d. sequences with _\d. so they no longer look like version
                   1968: # numbers
                   1969:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
1.531     albertel 1970:     return $fname;
                   1971: }
                   1972: 
1.608     albertel 1973: # --------------- Take an uploaded file and put it into the userfiles directory
1.686     albertel 1974: # input: $formname - the contents of the file are in $env{"form.$formname"}
1.719     banghart 1975: #                    the desired filenam is in $env{"form.$formname.filename"}
1.686     albertel 1976: #        $coursedoc - if true up to the current course
                   1977: #                     if false
                   1978: #        $subdir - directory in userfile to store the file into
1.858     raeburn  1979: #        $parser - instruction to parse file for objects ($parser = parse)    
                   1980: #        $allfiles - reference to hash for embedded objects
                   1981: #        $codebase - reference to hash for codebase of java objects
                   1982: #        $desuname - username for permanent storage of uploaded file
                   1983: #        $dsetudom - domain for permanaent storage of uploaded file
1.860     raeburn  1984: #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
                   1985: #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
1.858     raeburn  1986: # 
1.686     albertel 1987: # output: url of file in userspace, or error: <message> 
                   1988: #             or /adm/notfound.html if failure to upload occurse
1.608     albertel 1989: 
                   1990: 
1.531     albertel 1991: sub userfileupload {
1.860     raeburn  1992:     my ($formname,$coursedoc,$subdir,$parser,$allfiles,$codebase,$destuname,
                   1993:         $destudom,$thumbwidth,$thumbheight)=@_;
1.531     albertel 1994:     if (!defined($subdir)) { $subdir='unknown'; }
1.620     albertel 1995:     my $fname=$env{'form.'.$formname.'.filename'};
1.531     albertel 1996:     $fname=&clean_filename($fname);
1.315     www      1997: # See if there is anything left
1.257     www      1998:     unless ($fname) { return 'error: no uploaded file'; }
1.620     albertel 1999:     chop($env{'form.'.$formname});
1.523     raeburn  2000:     if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) { #files uploaded to help request form are handled differently
                   2001:         my $now = time;
                   2002:         my $filepath = 'tmp/helprequests/'.$now;
                   2003:         my @parts=split(/\//,$filepath);
                   2004:         my $fullpath = $perlvar{'lonDaemons'};
                   2005:         for (my $i=0;$i<@parts;$i++) {
                   2006:             $fullpath .= '/'.$parts[$i];
                   2007:             if ((-e $fullpath)!=1) {
                   2008:                 mkdir($fullpath,0777);
                   2009:             }
                   2010:         }
                   2011:         open(my $fh,'>'.$fullpath.'/'.$fname);
1.620     albertel 2012:         print $fh $env{'form.'.$formname};
1.523     raeburn  2013:         close($fh);
1.741     raeburn  2014:         return $fullpath.'/'.$fname;
                   2015:     } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) { #files uploaded to create course page are handled differently
                   2016:         my $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
                   2017:                        '_'.$env{'user.domain'}.'/pending';
                   2018:         my @parts=split(/\//,$filepath);
                   2019:         my $fullpath = $perlvar{'lonDaemons'};
                   2020:         for (my $i=0;$i<@parts;$i++) {
                   2021:             $fullpath .= '/'.$parts[$i];
                   2022:             if ((-e $fullpath)!=1) {
                   2023:                 mkdir($fullpath,0777);
                   2024:             }
                   2025:         }
                   2026:         open(my $fh,'>'.$fullpath.'/'.$fname);
                   2027:         print $fh $env{'form.'.$formname};
                   2028:         close($fh);
                   2029:         return $fullpath.'/'.$fname;
1.523     raeburn  2030:     }
1.719     banghart 2031:     
1.258     www      2032: # Create the directory if not present
1.493     albertel 2033:     $fname="$subdir/$fname";
1.259     www      2034:     if ($coursedoc) {
1.638     albertel 2035: 	my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   2036: 	my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.646     raeburn  2037:         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
1.638     albertel 2038:             return &finishuserfileupload($docuname,$docudom,
                   2039: 					 $formname,$fname,$parser,$allfiles,
1.860     raeburn  2040: 					 $codebase,$thumbwidth,$thumbheight);
1.481     raeburn  2041:         } else {
1.620     albertel 2042:             $fname=$env{'form.folder'}.'/'.$fname;
1.638     albertel 2043:             return &process_coursefile('uploaddoc',$docuname,$docudom,
                   2044: 				       $fname,$formname,$parser,
                   2045: 				       $allfiles,$codebase);
1.481     raeburn  2046:         }
1.719     banghart 2047:     } elsif (defined($destuname)) {
                   2048:         my $docuname=$destuname;
                   2049:         my $docudom=$destudom;
1.860     raeburn  2050: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
                   2051: 				     $parser,$allfiles,$codebase,
                   2052:                                      $thumbwidth,$thumbheight);
1.719     banghart 2053:         
1.259     www      2054:     } else {
1.638     albertel 2055:         my $docuname=$env{'user.name'};
                   2056:         my $docudom=$env{'user.domain'};
1.714     raeburn  2057:         if (exists($env{'form.group'})) {
                   2058:             $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   2059:             $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   2060:         }
1.860     raeburn  2061: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
                   2062: 				     $parser,$allfiles,$codebase,
                   2063:                                      $thumbwidth,$thumbheight);
1.259     www      2064:     }
1.271     www      2065: }
                   2066: 
                   2067: sub finishuserfileupload {
1.860     raeburn  2068:     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
                   2069:         $thumbwidth,$thumbheight) = @_;
1.477     raeburn  2070:     my $path=$docudom.'/'.$docuname.'/';
1.258     www      2071:     my $filepath=$perlvar{'lonDocRoot'};
1.860     raeburn  2072:     my ($fnamepath,$file,$fetchthumb);
1.494     albertel 2073:     $file=$fname;
                   2074:     if ($fname=~m|/|) {
                   2075:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
                   2076: 	$path.=$fnamepath.'/';
                   2077:     }
1.259     www      2078:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258     www      2079:     my $count;
                   2080:     for ($count=4;$count<=$#parts;$count++) {
                   2081:         $filepath.="/$parts[$count]";
                   2082:         if ((-e $filepath)!=1) {
                   2083: 	    mkdir($filepath,0777);
                   2084:         }
                   2085:     }
                   2086: # Save the file
                   2087:     {
1.701     albertel 2088: 	if (!open(FH,'>'.$filepath.'/'.$file)) {
                   2089: 	    &logthis('Failed to create '.$filepath.'/'.$file);
                   2090: 	    print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
                   2091: 	    return '/adm/notfound.html';
                   2092: 	}
                   2093: 	if (!print FH ($env{'form.'.$formname})) {
                   2094: 	    &logthis('Failed to write to '.$filepath.'/'.$file);
                   2095: 	    print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
                   2096: 	    return '/adm/notfound.html';
                   2097: 	}
1.570     albertel 2098: 	close(FH);
1.258     www      2099:     }
1.637     raeburn  2100:     if ($parser eq 'parse') {
1.961     raeburn  2101:         my $parse_result = &extract_embedded_items($filepath.'/'.$file,$allfiles,
1.638     albertel 2102: 						   $codebase);
1.637     raeburn  2103:         unless ($parse_result eq 'ok') {
1.638     albertel 2104:             &logthis('Failed to parse '.$filepath.$file.
                   2105: 		     ' for embedded media: '.$parse_result); 
1.637     raeburn  2106:         }
                   2107:     }
1.860     raeburn  2108:     if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
                   2109:         my $input = $filepath.'/'.$file;
                   2110:         my $output = $filepath.'/'.'tn-'.$file;
                   2111:         my $thumbsize = $thumbwidth.'x'.$thumbheight;
                   2112:         system("convert -sample $thumbsize $input $output");
                   2113:         if (-e $filepath.'/'.'tn-'.$file) {
                   2114:             $fetchthumb  = 1; 
                   2115:         }
                   2116:     }
1.858     raeburn  2117:  
1.259     www      2118: # Notify homeserver to grep it
                   2119: #
1.638     albertel 2120:     my $docuhome=&homeserver($docuname,$docudom);
1.494     albertel 2121:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1.295     www      2122:     if ($fetchresult eq 'ok') {
1.860     raeburn  2123:         if ($fetchthumb) {
                   2124:             my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
                   2125:             if ($thumbresult ne 'ok') {
                   2126:                 &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
                   2127:                          $docuhome.': '.$thumbresult);
                   2128:             }
                   2129:         }
1.259     www      2130: #
1.258     www      2131: # Return the URL to it
1.494     albertel 2132:         return '/uploaded/'.$path.$file;
1.263     www      2133:     } else {
1.494     albertel 2134:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
                   2135: 		 ': '.$fetchresult);
1.263     www      2136:         return '/adm/notfound.html';
1.858     raeburn  2137:     }
1.493     albertel 2138: }
                   2139: 
1.637     raeburn  2140: sub extract_embedded_items {
1.961     raeburn  2141:     my ($fullpath,$allfiles,$codebase,$content) = @_;
1.637     raeburn  2142:     my @state = ();
                   2143:     my %javafiles = (
                   2144:                       codebase => '',
                   2145:                       code => '',
                   2146:                       archive => ''
                   2147:                     );
                   2148:     my %mediafiles = (
                   2149:                       src => '',
                   2150:                       movie => '',
                   2151:                      );
1.648     raeburn  2152:     my $p;
                   2153:     if ($content) {
                   2154:         $p = HTML::LCParser->new($content);
                   2155:     } else {
1.961     raeburn  2156:         $p = HTML::LCParser->new($fullpath);
1.648     raeburn  2157:     }
1.641     albertel 2158:     while (my $t=$p->get_token()) {
1.640     albertel 2159: 	if ($t->[0] eq 'S') {
                   2160: 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
1.886     albertel 2161: 	    push(@state, $tagname);
1.648     raeburn  2162:             if (lc($tagname) eq 'allow') {
                   2163:                 &add_filetype($allfiles,$attr->{'src'},'src');
                   2164:             }
1.640     albertel 2165: 	    if (lc($tagname) eq 'img') {
                   2166: 		&add_filetype($allfiles,$attr->{'src'},'src');
                   2167: 	    }
1.886     albertel 2168: 	    if (lc($tagname) eq 'a') {
                   2169: 		&add_filetype($allfiles,$attr->{'href'},'href');
                   2170: 	    }
1.645     raeburn  2171:             if (lc($tagname) eq 'script') {
                   2172:                 if ($attr->{'archive'} =~ /\.jar$/i) {
                   2173:                     &add_filetype($allfiles,$attr->{'archive'},'archive');
                   2174:                 } else {
                   2175:                     &add_filetype($allfiles,$attr->{'src'},'src');
                   2176:                 }
                   2177:             }
                   2178:             if (lc($tagname) eq 'link') {
                   2179:                 if (lc($attr->{'rel'}) eq 'stylesheet') { 
                   2180:                     &add_filetype($allfiles,$attr->{'href'},'href');
                   2181:                 }
                   2182:             }
1.640     albertel 2183: 	    if (lc($tagname) eq 'object' ||
                   2184: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
                   2185: 		foreach my $item (keys(%javafiles)) {
                   2186: 		    $javafiles{$item} = '';
                   2187: 		}
                   2188: 	    }
                   2189: 	    if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
                   2190: 		my $name = lc($attr->{'name'});
                   2191: 		foreach my $item (keys(%javafiles)) {
                   2192: 		    if ($name eq $item) {
                   2193: 			$javafiles{$item} = $attr->{'value'};
                   2194: 			last;
                   2195: 		    }
                   2196: 		}
                   2197: 		foreach my $item (keys(%mediafiles)) {
                   2198: 		    if ($name eq $item) {
                   2199: 			&add_filetype($allfiles, $attr->{'value'}, 'value');
                   2200: 			last;
                   2201: 		    }
                   2202: 		}
                   2203: 	    }
                   2204: 	    if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
                   2205: 		foreach my $item (keys(%javafiles)) {
                   2206: 		    if ($attr->{$item}) {
                   2207: 			$javafiles{$item} = $attr->{$item};
                   2208: 			last;
                   2209: 		    }
                   2210: 		}
                   2211: 		foreach my $item (keys(%mediafiles)) {
                   2212: 		    if ($attr->{$item}) {
                   2213: 			&add_filetype($allfiles,$attr->{$item},$item);
                   2214: 			last;
                   2215: 		    }
                   2216: 		}
                   2217: 	    }
                   2218: 	} elsif ($t->[0] eq 'E') {
                   2219: 	    my ($tagname) = ($t->[1]);
                   2220: 	    if ($javafiles{'codebase'} ne '') {
                   2221: 		$javafiles{'codebase'} .= '/';
                   2222: 	    }  
                   2223: 	    if (lc($tagname) eq 'applet' ||
                   2224: 		lc($tagname) eq 'object' ||
                   2225: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
                   2226: 		) {
                   2227: 		foreach my $item (keys(%javafiles)) {
                   2228: 		    if ($item ne 'codebase' && $javafiles{$item} ne '') {
                   2229: 			my $file=$javafiles{'codebase'}.$javafiles{$item};
                   2230: 			&add_filetype($allfiles,$file,$item);
                   2231: 		    }
                   2232: 		}
                   2233: 	    } 
                   2234: 	    pop @state;
                   2235: 	}
                   2236:     }
1.637     raeburn  2237:     return 'ok';
                   2238: }
                   2239: 
1.639     albertel 2240: sub add_filetype {
                   2241:     my ($allfiles,$file,$type)=@_;
                   2242:     if (exists($allfiles->{$file})) {
                   2243: 	unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
                   2244: 	    push(@{$allfiles->{$file}}, &escape($type));
                   2245: 	}
                   2246:     } else {
                   2247: 	@{$allfiles->{$file}} = (&escape($type));
1.637     raeburn  2248:     }
                   2249: }
                   2250: 
1.493     albertel 2251: sub removeuploadedurl {
                   2252:     my ($url)=@_;
                   2253:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);
1.613     albertel 2254:     return &removeuserfile($uname,$udom,$fname);
1.490     albertel 2255: }
                   2256: 
                   2257: sub removeuserfile {
                   2258:     my ($docuname,$docudom,$fname)=@_;
                   2259:     my $home=&homeserver($docuname,$docudom);
1.798     raeburn  2260:     my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
                   2261:     if ($result eq 'ok') {
                   2262:         if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
                   2263:             my $metafile = $fname.'.meta';
                   2264:             my $metaresult = &removeuserfile($docuname,$docudom,$metafile); 
1.823     albertel 2265: 	    my $url = "/uploaded/$docudom/$docuname/$fname";
                   2266:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821     raeburn  2267:             my $sqlresult = 
1.823     albertel 2268:                 &update_portfolio_table($docuname,$docudom,$file,
1.821     raeburn  2269:                                         'portfolio_metadata',$group,
                   2270:                                         'delete');
1.798     raeburn  2271:         }
                   2272:     }
                   2273:     return $result;
1.257     www      2274: }
1.15      www      2275: 
1.530     albertel 2276: sub mkdiruserfile {
                   2277:     my ($docuname,$docudom,$dir)=@_;
                   2278:     my $home=&homeserver($docuname,$docudom);
                   2279:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
                   2280: }
                   2281: 
1.531     albertel 2282: sub renameuserfile {
                   2283:     my ($docuname,$docudom,$old,$new)=@_;
                   2284:     my $home=&homeserver($docuname,$docudom);
1.798     raeburn  2285:     my $result = &reply("renameuserfile:$docudom:$docuname:".
                   2286:                         &escape("$old").':'.&escape("$new"),$home);
                   2287:     if ($result eq 'ok') {
                   2288:         if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
                   2289:             my $oldmeta = $old.'.meta';
                   2290:             my $newmeta = $new.'.meta';
                   2291:             my $metaresult = 
                   2292:                 &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
1.823     albertel 2293: 	    my $url = "/uploaded/$docudom/$docuname/$old";
                   2294:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821     raeburn  2295:             my $sqlresult = 
1.823     albertel 2296:                 &update_portfolio_table($docuname,$docudom,$file,
1.821     raeburn  2297:                                         'portfolio_metadata',$group,
                   2298:                                         'delete');
1.798     raeburn  2299:         }
                   2300:     }
                   2301:     return $result;
1.531     albertel 2302: }
                   2303: 
1.14      www      2304: # ------------------------------------------------------------------------- Log
                   2305: 
                   2306: sub log {
                   2307:     my ($dom,$nam,$hom,$what)=@_;
1.47      www      2308:     return critical("log:$dom:$nam:$what",$hom);
1.157     www      2309: }
                   2310: 
                   2311: # ------------------------------------------------------------------ Course Log
1.352     www      2312: #
                   2313: # This routine flushes several buffers of non-mission-critical nature
                   2314: #
1.157     www      2315: 
                   2316: sub flushcourselogs {
1.352     www      2317:     &logthis('Flushing log buffers');
                   2318: #
                   2319: # course logs
                   2320: # This is a log of all transactions in a course, which can be used
                   2321: # for data mining purposes
                   2322: #
                   2323: # It also collects the courseid database, which lists last transaction
                   2324: # times and course titles for all courseids
                   2325: #
                   2326:     my %courseidbuffer=();
1.921     raeburn  2327:     foreach my $crsid (keys(%courselogs)) {
1.352     www      2328:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1.188     www      2329: 		          &escape($courselogs{$crsid}),
                   2330: 		          $coursehombuf{$crsid}) eq 'ok') {
1.157     www      2331: 	    delete $courselogs{$crsid};
                   2332:         } else {
                   2333:             &logthis('Failed to flush log buffer for '.$crsid);
                   2334:             if (length($courselogs{$crsid})>40000) {
1.672     albertel 2335:                &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
1.157     www      2336:                         " exceeded maximum size, deleting.</font>");
                   2337:                delete $courselogs{$crsid};
                   2338:             }
1.352     www      2339:         }
1.920     raeburn  2340:         $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
1.936     raeburn  2341:             'description' => $coursedescrbuf{$crsid},
                   2342:             'inst_code'    => $courseinstcodebuf{$crsid},
                   2343:             'type'        => $coursetypebuf{$crsid},
                   2344:             'owner'       => $courseownerbuf{$crsid},
1.920     raeburn  2345:         };
1.191     harris41 2346:     }
1.352     www      2347: #
                   2348: # Write course id database (reverse lookup) to homeserver of courses 
                   2349: # Is used in pickcourse
                   2350: #
1.840     albertel 2351:     foreach my $crs_home (keys(%courseidbuffer)) {
1.918     raeburn  2352:         my $response = &courseidput(&host_domain($crs_home),
1.921     raeburn  2353:                                     $courseidbuffer{$crs_home},
                   2354:                                     $crs_home,'timeonly');
1.352     www      2355:     }
                   2356: #
                   2357: # File accesses
                   2358: # Writes to the dynamic metadata of resources to get hit counts, etc.
                   2359: #
1.449     matthew  2360:     foreach my $entry (keys(%accesshash)) {
1.458     matthew  2361:         if ($entry =~ /___count$/) {
                   2362:             my ($dom,$name);
1.807     albertel 2363:             ($dom,$name,undef)=
1.811     albertel 2364: 		($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
1.458     matthew  2365:             if (! defined($dom) || $dom eq '' || 
                   2366:                 ! defined($name) || $name eq '') {
1.620     albertel 2367:                 my $cid = $env{'request.course.id'};
                   2368:                 $dom  = $env{'request.'.$cid.'.domain'};
                   2369:                 $name = $env{'request.'.$cid.'.num'};
1.458     matthew  2370:             }
1.450     matthew  2371:             my $value = $accesshash{$entry};
                   2372:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
                   2373:             my %temphash=($url => $value);
1.449     matthew  2374:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
                   2375:             if ($result eq 'ok') {
                   2376:                 delete $accesshash{$entry};
                   2377:             } elsif ($result eq 'unknown_cmd') {
                   2378:                 # Target server has old code running on it.
1.450     matthew  2379:                 my %temphash=($entry => $value);
1.449     matthew  2380:                 if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
                   2381:                     delete $accesshash{$entry};
                   2382:                 }
                   2383:             }
                   2384:         } else {
1.811     albertel 2385:             my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
1.450     matthew  2386:             my %temphash=($entry => $accesshash{$entry});
1.449     matthew  2387:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
                   2388:                 delete $accesshash{$entry};
                   2389:             }
1.185     www      2390:         }
1.191     harris41 2391:     }
1.352     www      2392: #
                   2393: # Roles
                   2394: # Reverse lookup of user roles for course faculty/staff and co-authorship
                   2395: #
1.800     albertel 2396:     foreach my $entry (keys(%userrolehash)) {
1.351     www      2397:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1.349     www      2398: 	    split(/\:/,$entry);
                   2399:         if (&Apache::lonnet::put('nohist_userroles',
1.351     www      2400:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1.349     www      2401:                 $rudom,$runame) eq 'ok') {
                   2402: 	    delete $userrolehash{$entry};
                   2403:         }
                   2404:     }
1.662     raeburn  2405: #
                   2406: # Reverse lookup of domain roles (dc, ad, li, sc, au)
                   2407: #
                   2408:     my %domrolebuffer = ();
                   2409:     foreach my $entry (keys %domainrolehash) {
1.901     albertel 2410:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
1.662     raeburn  2411:         if ($domrolebuffer{$rudom}) {
                   2412:             $domrolebuffer{$rudom}.='&'.&escape($entry).
                   2413:                       '='.&escape($domainrolehash{$entry});
                   2414:         } else {
                   2415:             $domrolebuffer{$rudom}.=&escape($entry).
                   2416:                       '='.&escape($domainrolehash{$entry});
                   2417:         }
                   2418:         delete $domainrolehash{$entry};
                   2419:     }
                   2420:     foreach my $dom (keys(%domrolebuffer)) {
1.841     albertel 2421: 	my %servers = &get_servers($dom,'library');
                   2422: 	foreach my $tryserver (keys(%servers)) {
                   2423: 	    unless (&reply('domroleput:'.$dom.':'.
                   2424: 			   $domrolebuffer{$dom},$tryserver) eq 'ok') {
                   2425: 		&logthis('Put of domain roles failed for '.$dom.' and  '.$tryserver);
                   2426: 	    }
1.662     raeburn  2427:         }
                   2428:     }
1.186     www      2429:     $dumpcount++;
1.157     www      2430: }
                   2431: 
                   2432: sub courselog {
                   2433:     my $what=shift;
1.158     www      2434:     $what=time.':'.$what;
1.620     albertel 2435:     unless ($env{'request.course.id'}) { return ''; }
                   2436:     $coursedombuf{$env{'request.course.id'}}=
                   2437:        $env{'course.'.$env{'request.course.id'}.'.domain'};
                   2438:     $coursenumbuf{$env{'request.course.id'}}=
                   2439:        $env{'course.'.$env{'request.course.id'}.'.num'};
                   2440:     $coursehombuf{$env{'request.course.id'}}=
                   2441:        $env{'course.'.$env{'request.course.id'}.'.home'};
                   2442:     $coursedescrbuf{$env{'request.course.id'}}=
                   2443:        $env{'course.'.$env{'request.course.id'}.'.description'};
                   2444:     $courseinstcodebuf{$env{'request.course.id'}}=
                   2445:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
                   2446:     $courseownerbuf{$env{'request.course.id'}}=
                   2447:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
1.741     raeburn  2448:     $coursetypebuf{$env{'request.course.id'}}=
                   2449:        $env{'course.'.$env{'request.course.id'}.'.type'};
1.620     albertel 2450:     if (defined $courselogs{$env{'request.course.id'}}) {
                   2451: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
1.157     www      2452:     } else {
1.620     albertel 2453: 	$courselogs{$env{'request.course.id'}}.=$what;
1.157     www      2454:     }
1.620     albertel 2455:     if (length($courselogs{$env{'request.course.id'}})>4048) {
1.157     www      2456: 	&flushcourselogs();
                   2457:     }
1.158     www      2458: }
                   2459: 
                   2460: sub courseacclog {
                   2461:     my $fnsymb=shift;
1.620     albertel 2462:     unless ($env{'request.course.id'}) { return ''; }
                   2463:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
1.657     albertel 2464:     if ($fnsymb=~/(problem|exam|quiz|assess|survey|form|task|page)$/) {
1.187     www      2465:         $what.=':POST';
1.583     matthew  2466:         # FIXME: Probably ought to escape things....
1.800     albertel 2467: 	foreach my $key (keys(%env)) {
                   2468:             if ($key=~/^form\.(.*)/) {
                   2469: 		$what.=':'.$1.'='.$env{$key};
1.158     www      2470:             }
1.191     harris41 2471:         }
1.583     matthew  2472:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
                   2473:         # FIXME: We should not be depending on a form parameter that someone
                   2474:         # editing lonsearchcat.pm might change in the future.
1.620     albertel 2475:         if ($env{'form.phase'} eq 'course_search') {
1.583     matthew  2476:             $what.= ':POST';
                   2477:             # FIXME: Probably ought to escape things....
                   2478:             foreach my $element ('courseexp','crsfulltext','crsrelated',
                   2479:                                  'crsdiscuss') {
1.620     albertel 2480:                 $what.=':'.$element.'='.$env{'form.'.$element};
1.583     matthew  2481:             }
                   2482:         }
1.158     www      2483:     }
                   2484:     &courselog($what);
1.149     www      2485: }
                   2486: 
1.185     www      2487: sub countacc {
                   2488:     my $url=&declutter(shift);
1.458     matthew  2489:     return if (! defined($url) || $url eq '');
1.620     albertel 2490:     unless ($env{'request.course.id'}) { return ''; }
                   2491:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
1.281     www      2492:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.450     matthew  2493:     $accesshash{$key}++;
1.185     www      2494: }
1.349     www      2495: 
1.361     www      2496: sub linklog {
                   2497:     my ($from,$to)=@_;
                   2498:     $from=&declutter($from);
                   2499:     $to=&declutter($to);
                   2500:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
                   2501:     $accesshash{$to.'___'.$from.'___goto'}=1;
                   2502: }
                   2503:   
1.349     www      2504: sub userrolelog {
                   2505:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
1.661     raeburn  2506:     if (($trole=~/^ca/) || ($trole=~/^aa/) ||
1.662     raeburn  2507:         ($trole=~/^in/) || ($trole=~/^cc/) ||
1.661     raeburn  2508:         ($trole=~/^ep/) || ($trole=~/^cr/) ||
                   2509:         ($trole=~/^ta/)) {
1.350     www      2510:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   2511:        $userrolehash
                   2512:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1.349     www      2513:                     =$tend.':'.$tstart;
1.662     raeburn  2514:     }
1.898     albertel 2515:     if (($env{'request.role'} =~ /dc\./) &&
                   2516: 	(($trole=~/^au/) || ($trole=~/^in/) ||
                   2517: 	 ($trole=~/^cc/) || ($trole=~/^ep/) ||
                   2518: 	 ($trole=~/^cr/) || ($trole=~/^ta/))) {
                   2519:        $userrolehash
                   2520:          {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
                   2521:                     =$tend.':'.$tstart;
                   2522:     }
1.662     raeburn  2523:     if (($trole=~/^dc/) || ($trole=~/^ad/) ||
                   2524:         ($trole=~/^li/) || ($trole=~/^li/) ||
                   2525:         ($trole=~/^au/) || ($trole=~/^dg/) ||
                   2526:         ($trole=~/^sc/)) {
                   2527:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   2528:        $domainrolehash
                   2529:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
                   2530:                     = $tend.':'.$tstart;
                   2531:     }
1.351     www      2532: }
                   2533: 
1.957     raeburn  2534: sub courserolelog {
                   2535:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
                   2536:     if (($trole eq 'cc') || ($trole eq 'in') ||
                   2537:         ($trole eq 'ep') || ($trole eq 'ad') ||
                   2538:         ($trole eq 'ta') || ($trole eq 'st') ||
                   2539:         ($trole=~/^cr/) || ($trole eq 'gr')) {
                   2540:         if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
                   2541:             my $cdom = $1;
                   2542:             my $cnum = $2;
                   2543:             my $sec = $3;
                   2544:             my $namespace = 'rolelog';
                   2545:             my %storehash = (
                   2546:                                role    => $trole,
                   2547:                                start   => $tstart,
                   2548:                                end     => $tend,
                   2549:                                selfenroll => $selfenroll,
                   2550:                                context    => $context,
                   2551:                             );
                   2552:             if ($trole eq 'gr') {
                   2553:                 $namespace = 'groupslog';
                   2554:                 $storehash{'group'} = $sec;
                   2555:             } else {
                   2556:                 $storehash{'section'} = $sec;
                   2557:             }
                   2558:             &instructor_log($namespace,\%storehash,$delflag,$username,$domain,$cnum,$cdom);
                   2559:         }
                   2560:     }
                   2561:     return;
                   2562: }
                   2563: 
1.351     www      2564: sub get_course_adv_roles {
1.948     raeburn  2565:     my ($cid,$codes) = @_;
1.620     albertel 2566:     $cid=$env{'request.course.id'} unless (defined($cid));
1.351     www      2567:     my %coursehash=&coursedescription($cid);
1.470     www      2568:     my %nothide=();
1.800     albertel 2569:     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
1.937     raeburn  2570:         if ($user !~ /:/) {
                   2571: 	    $nothide{join(':',split(/[\@]/,$user))}=1;
                   2572:         } else {
                   2573:             $nothide{$user}=1;
                   2574:         }
1.470     www      2575:     }
1.351     www      2576:     my %returnhash=();
                   2577:     my %dumphash=
                   2578:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
                   2579:     my $now=time;
1.800     albertel 2580:     foreach my $entry (keys %dumphash) {
                   2581: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
1.351     www      2582:         if (($tstart) && ($tstart<0)) { next; }
                   2583:         if (($tend) && ($tend<$now)) { next; }
                   2584:         if (($tstart) && ($now<$tstart)) { next; }
1.800     albertel 2585:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
1.576     albertel 2586: 	if ($username eq '' || $domain eq '') { next; }
1.470     www      2587: 	if ((&privileged($username,$domain)) && 
                   2588: 	    (!$nothide{$username.':'.$domain})) { next; }
1.656     albertel 2589: 	if ($role eq 'cr') { next; }
1.948     raeburn  2590:         if ($codes) {
                   2591:             if ($section) { $role .= ':'.$section; }
                   2592:             if ($returnhash{$role}) {
                   2593:                 $returnhash{$role}.=','.$username.':'.$domain;
                   2594:             } else {
                   2595:                 $returnhash{$role}=$username.':'.$domain;
                   2596:             }
1.351     www      2597:         } else {
1.948     raeburn  2598:             my $key=&plaintext($role);
                   2599:             if ($section) { $key.=' (Section '.$section.')'; }
                   2600:             if ($returnhash{$key}) {
                   2601: 	        $returnhash{$key}.=','.$username.':'.$domain;
                   2602:             } else {
                   2603:                 $returnhash{$key}=$username.':'.$domain;
                   2604:             }
1.351     www      2605:         }
1.948     raeburn  2606:     }
1.400     www      2607:     return %returnhash;
                   2608: }
                   2609: 
                   2610: sub get_my_roles {
1.937     raeburn  2611:     my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
1.620     albertel 2612:     unless (defined($uname)) { $uname=$env{'user.name'}; }
                   2613:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
1.937     raeburn  2614:     my (%dumphash,%nothide);
1.858     raeburn  2615:     if ($context eq 'userroles') { 
                   2616:         %dumphash = &dump('roles',$udom,$uname);
                   2617:     } else {
                   2618:         %dumphash=
1.400     www      2619:             &dump('nohist_userroles',$udom,$uname);
1.937     raeburn  2620:         if ($hidepriv) {
                   2621:             my %coursehash=&coursedescription($udom.'_'.$uname);
                   2622:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
                   2623:                 if ($user !~ /:/) {
                   2624:                     $nothide{join(':',split(/[\@]/,$user))} = 1;
                   2625:                 } else {
                   2626:                     $nothide{$user} = 1;
                   2627:                 }
                   2628:             }
                   2629:         }
1.858     raeburn  2630:     }
1.400     www      2631:     my %returnhash=();
                   2632:     my $now=time;
1.800     albertel 2633:     foreach my $entry (keys(%dumphash)) {
1.867     raeburn  2634:         my ($role,$tend,$tstart);
                   2635:         if ($context eq 'userroles') {
                   2636: 	    ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
                   2637:         } else {
                   2638:             ($tend,$tstart)=split(/\:/,$dumphash{$entry});
                   2639:         }
1.400     www      2640:         if (($tstart) && ($tstart<0)) { next; }
1.832     raeburn  2641:         my $status = 'active';
1.939     raeburn  2642:         if (($tend) && ($tend<=$now)) {
1.832     raeburn  2643:             $status = 'previous';
                   2644:         } 
                   2645:         if (($tstart) && ($now<$tstart)) {
                   2646:             $status = 'future';
                   2647:         }
                   2648:         if (ref($types) eq 'ARRAY') {
                   2649:             if (!grep(/^\Q$status\E$/,@{$types})) {
                   2650:                 next;
                   2651:             } 
                   2652:         } else {
                   2653:             if ($status ne 'active') {
                   2654:                 next;
                   2655:             }
                   2656:         }
1.867     raeburn  2657:         my ($rolecode,$username,$domain,$section,$area);
                   2658:         if ($context eq 'userroles') {
                   2659:             ($area,$rolecode) = split(/_/,$entry);
                   2660:             (undef,$domain,$username,$section) = split(/\//,$area);
                   2661:         } else {
                   2662:             ($role,$username,$domain,$section) = split(/\:/,$entry);
                   2663:         }
1.832     raeburn  2664:         if (ref($roledoms) eq 'ARRAY') {
                   2665:             if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
                   2666:                 next;
                   2667:             }
                   2668:         }
                   2669:         if (ref($roles) eq 'ARRAY') {
                   2670:             if (!grep(/^\Q$role\E$/,@{$roles})) {
1.922     raeburn  2671:                 if ($role =~ /^cr\//) {
                   2672:                     if (!grep(/^cr$/,@{$roles})) {
                   2673:                         next;
                   2674:                     }
                   2675:                 } else {
                   2676:                     next;
                   2677:                 }
1.832     raeburn  2678:             }
1.867     raeburn  2679:         }
1.937     raeburn  2680:         if ($hidepriv) {
                   2681:             if ((&privileged($username,$domain)) &&
                   2682:                 (!$nothide{$username.':'.$domain})) { 
                   2683:                 next;
                   2684:             }
                   2685:         }
1.933     raeburn  2686:         if ($withsec) {
                   2687:             $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
                   2688:                 $tstart.':'.$tend;
                   2689:         } else {
                   2690:             $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
                   2691:         }
1.832     raeburn  2692:     }
1.373     www      2693:     return %returnhash;
1.399     www      2694: }
                   2695: 
                   2696: # ----------------------------------------------------- Frontpage Announcements
                   2697: #
                   2698: #
                   2699: 
                   2700: sub postannounce {
                   2701:     my ($server,$text)=@_;
1.844     albertel 2702:     unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
1.399     www      2703:     unless ($text=~/\w/) { $text=''; }
                   2704:     return &reply('setannounce:'.&escape($text),$server);
                   2705: }
                   2706: 
                   2707: sub getannounce {
1.448     albertel 2708: 
                   2709:     if (open(my $fh,$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1.399     www      2710: 	my $announcement='';
1.800     albertel 2711: 	while (my $line = <$fh>) { $announcement .= $line; }
1.448     albertel 2712: 	close($fh);
1.399     www      2713: 	if ($announcement=~/\w/) { 
                   2714: 	    return 
                   2715:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1.518     albertel 2716:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
1.399     www      2717: 	} else {
                   2718: 	    return '';
                   2719: 	}
                   2720:     } else {
                   2721: 	return '';
                   2722:     }
1.351     www      2723: }
1.353     www      2724: 
                   2725: # ---------------------------------------------------------- Course ID routines
                   2726: # Deal with domain's nohist_courseid.db files
                   2727: #
                   2728: 
                   2729: sub courseidput {
1.921     raeburn  2730:     my ($domain,$storehash,$coursehome,$caller) = @_;
                   2731:     my $outcome;
                   2732:     if ($caller eq 'timeonly') {
                   2733:         my $cids = '';
                   2734:         foreach my $item (keys(%$storehash)) {
                   2735:             $cids.=&escape($item).'&';
                   2736:         }
                   2737:         $cids=~s/\&$//;
                   2738:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
                   2739:                           $coursehome);       
                   2740:     } else {
                   2741:         my $items = '';
                   2742:         foreach my $item (keys(%$storehash)) {
                   2743:             $items.= &escape($item).'='.
                   2744:                      &freeze_escape($$storehash{$item}).'&';
                   2745:         }
                   2746:         $items=~s/\&$//;
                   2747:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
                   2748:                           $coursehome);
1.918     raeburn  2749:     }
                   2750:     if ($outcome eq 'unknown_cmd') {
                   2751:         my $what;
                   2752:         foreach my $cid (keys(%$storehash)) {
                   2753:             $what .= &escape($cid).'=';
1.921     raeburn  2754:             foreach my $item ('description','inst_code','owner','type') {
1.936     raeburn  2755:                 $what .= &escape($storehash->{$cid}{$item}).':';
1.918     raeburn  2756:             }
                   2757:             $what =~ s/\:$/&/;
                   2758:         }
                   2759:         $what =~ s/\&$//;  
                   2760:         return &reply('courseidput:'.$domain.':'.$what,$coursehome);
                   2761:     } else {
                   2762:         return $outcome;
                   2763:     }
1.353     www      2764: }
                   2765: 
                   2766: sub courseiddump {
1.921     raeburn  2767:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
1.947     raeburn  2768:         $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
1.962     raeburn  2769:         $selfenrollonly,$catfilter,$showhidden,$caller)=@_;
1.918     raeburn  2770:     my $as_hash = 1;
                   2771:     my %returnhash;
                   2772:     if (!$domfilter) { $domfilter=''; }
1.845     albertel 2773:     my %libserv = &all_library();
                   2774:     foreach my $tryserver (keys(%libserv)) {
                   2775:         if ( (  $hostidflag == 1 
                   2776: 	        && grep(/^\Q$tryserver\E$/,@{$hostidref}) ) 
                   2777: 	     || (!defined($hostidflag)) ) {
                   2778: 
1.918     raeburn  2779: 	    if (($domfilter eq '') ||
                   2780: 		(&host_domain($tryserver) eq $domfilter)) {
                   2781:                 my $rep = 
                   2782:                   &reply('courseiddump:'.&host_domain($tryserver).':'.
                   2783:                          $sincefilter.':'.&escape($descfilter).':'.
                   2784:                          &escape($instcodefilter).':'.&escape($ownerfilter).
                   2785:                          ':'.&escape($coursefilter).':'.&escape($typefilter).
1.947     raeburn  2786:                          ':'.&escape($regexp_ok).':'.$as_hash.':'.
1.962     raeburn  2787:                          &escape($selfenrollonly).':'.&escape($catfilter).':'.
                   2788:                          $showhidden.':'.$caller,$tryserver);
1.918     raeburn  2789:                 my @pairs=split(/\&/,$rep);
                   2790:                 foreach my $item (@pairs) {
                   2791:                     my ($key,$value)=split(/\=/,$item,2);
                   2792:                     $key = &unescape($key);
                   2793:                     next if ($key =~ /^error: 2 /);
                   2794:                     my $result = &thaw_unescape($value);
                   2795:                     if (ref($result) eq 'HASH') {
                   2796:                         $returnhash{$key}=$result;
                   2797:                     } else {
1.921     raeburn  2798:                         my @responses = split(/:/,$value);
                   2799:                         my @items = ('description','inst_code','owner','type');
1.918     raeburn  2800:                         for (my $i=0; $i<@responses; $i++) {
1.921     raeburn  2801:                             $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
1.918     raeburn  2802:                         }
                   2803:                     } 
1.353     www      2804:                 }
                   2805:             }
                   2806:         }
                   2807:     }
                   2808:     return %returnhash;
                   2809: }
                   2810: 
1.658     raeburn  2811: # ---------------------------------------------------------- DC e-mail
1.662     raeburn  2812: 
                   2813: sub dcmailput {
1.685     raeburn  2814:     my ($domain,$msgid,$message,$server)=@_;
1.662     raeburn  2815:     my $status = &Apache::lonnet::critical(
1.740     www      2816:        'dcmailput:'.$domain.':'.&escape($msgid).'='.
                   2817:        &escape($message),$server);
1.662     raeburn  2818:     return $status;
                   2819: }
                   2820: 
1.658     raeburn  2821: sub dcmaildump {
                   2822:     my ($dom,$startdate,$enddate,$senders) = @_;
1.685     raeburn  2823:     my %returnhash=();
1.846     albertel 2824: 
                   2825:     if (defined(&domain($dom,'primary'))) {
1.685     raeburn  2826:         my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
                   2827:                                                          &escape($enddate).':';
                   2828: 	my @esc_senders=map { &escape($_)} @$senders;
                   2829: 	$cmd.=&escape(join('&',@esc_senders));
1.846     albertel 2830: 	foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
1.800     albertel 2831:             my ($key,$value) = split(/\=/,$line,2);
1.685     raeburn  2832:             if (($key) && ($value)) {
                   2833:                 $returnhash{&unescape($key)} = &unescape($value);
1.658     raeburn  2834:             }
                   2835:         }
                   2836:     }
                   2837:     return %returnhash;
                   2838: }
1.662     raeburn  2839: # ---------------------------------------------------------- Domain roles
                   2840: 
                   2841: sub get_domain_roles {
                   2842:     my ($dom,$roles,$startdate,$enddate)=@_;
                   2843:     if (undef($startdate) || $startdate eq '') {
                   2844:         $startdate = '.';
                   2845:     }
                   2846:     if (undef($enddate) || $enddate eq '') {
                   2847:         $enddate = '.';
                   2848:     }
1.922     raeburn  2849:     my $rolelist;
                   2850:     if (ref($roles) eq 'ARRAY') {
                   2851:         $rolelist = join(':',@{$roles});
                   2852:     }
1.662     raeburn  2853:     my %personnel = ();
1.841     albertel 2854: 
                   2855:     my %servers = &get_servers($dom,'library');
                   2856:     foreach my $tryserver (keys(%servers)) {
                   2857: 	%{$personnel{$tryserver}}=();
                   2858: 	foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
                   2859: 					    &escape($startdate).':'.
                   2860: 					    &escape($enddate).':'.
                   2861: 					    &escape($rolelist), $tryserver))) {
                   2862: 	    my ($key,$value) = split(/\=/,$line,2);
                   2863: 	    if (($key) && ($value)) {
                   2864: 		$personnel{$tryserver}{&unescape($key)} = &unescape($value);
                   2865: 	    }
                   2866: 	}
1.662     raeburn  2867:     }
                   2868:     return %personnel;
                   2869: }
1.658     raeburn  2870: 
1.149     www      2871: # ----------------------------------------------------------- Check out an item
                   2872: 
1.504     albertel 2873: sub get_first_access {
                   2874:     my ($type,$argsymb)=@_;
1.790     albertel 2875:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 2876:     if ($argsymb) { $symb=$argsymb; }
                   2877:     my ($map,$id,$res)=&decode_symb($symb);
1.926     albertel 2878:     if ($type eq 'course') {
                   2879: 	$res='course';
                   2880:     } elsif ($type eq 'map') {
1.588     albertel 2881: 	$res=&symbread($map);
                   2882:     } else {
                   2883: 	$res=$symb;
                   2884:     }
                   2885:     my %times=&get('firstaccesstimes',["$courseid\0$res"],$udom,$uname);
                   2886:     return $times{"$courseid\0$res"};
1.504     albertel 2887: }
                   2888: 
                   2889: sub set_first_access {
                   2890:     my ($type)=@_;
1.790     albertel 2891:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 2892:     my ($map,$id,$res)=&decode_symb($symb);
1.928     albertel 2893:     if ($type eq 'course') {
                   2894: 	$res='course';
                   2895:     } elsif ($type eq 'map') {
1.588     albertel 2896: 	$res=&symbread($map);
                   2897:     } else {
                   2898: 	$res=$symb;
                   2899:     }
                   2900:     my $firstaccess=&get_first_access($type,$symb);
1.505     albertel 2901:     if (!$firstaccess) {
1.588     albertel 2902: 	return &put('firstaccesstimes',{"$courseid\0$res"=>time},$udom,$uname);
1.505     albertel 2903:     }
                   2904:     return 'already_set';
1.504     albertel 2905: }
                   2906: 
1.149     www      2907: sub checkout {
                   2908:     my ($symb,$tuname,$tudom,$tcrsid)=@_;
                   2909:     my $now=time;
                   2910:     my $lonhost=$perlvar{'lonHostID'};
                   2911:     my $infostr=&escape(
1.234     www      2912:                  'CHECKOUTTOKEN&'.
1.149     www      2913:                  $tuname.'&'.
                   2914:                  $tudom.'&'.
                   2915:                  $tcrsid.'&'.
                   2916:                  $symb.'&'.
                   2917: 		 $now.'&'.$ENV{'REMOTE_ADDR'});
                   2918:     my $token=&reply('tmpput:'.$infostr,$lonhost);
1.151     www      2919:     if ($token=~/^error\:/) { 
1.672     albertel 2920:         &logthis("<font color=\"blue\">WARNING: ".
1.151     www      2921:                 "Checkout tmpput failed ".$tudom.' - '.$tuname.' - '.$symb.
                   2922:                  "</font>");
                   2923:         return ''; 
                   2924:     }
                   2925: 
1.149     www      2926:     $token=~s/^(\d+)\_.*\_(\d+)$/$1\*$2\*$lonhost/;
                   2927:     $token=~tr/a-z/A-Z/;
                   2928: 
1.153     www      2929:     my %infohash=('resource.0.outtoken' => $token,
                   2930:                   'resource.0.checkouttime' => $now,
                   2931:                   'resource.0.outremote' => $ENV{'REMOTE_ADDR'});
1.149     www      2932: 
                   2933:     unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
                   2934:        return '';
1.151     www      2935:     } else {
1.672     albertel 2936:         &logthis("<font color=\"blue\">WARNING: ".
1.151     www      2937:                 "Checkout cstore failed ".$tudom.' - '.$tuname.' - '.$symb.
                   2938:                  "</font>");
1.149     www      2939:     }    
                   2940: 
                   2941:     if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
                   2942:                          &escape('Checkout '.$infostr.' - '.
                   2943:                                                  $token)) ne 'ok') {
                   2944: 	return '';
1.151     www      2945:     } else {
1.672     albertel 2946:         &logthis("<font color=\"blue\">WARNING: ".
1.151     www      2947:                 "Checkout log failed ".$tudom.' - '.$tuname.' - '.$symb.
                   2948:                  "</font>");
1.149     www      2949:     }
1.151     www      2950:     return $token;
1.149     www      2951: }
                   2952: 
                   2953: # ------------------------------------------------------------ Check in an item
                   2954: 
                   2955: sub checkin {
                   2956:     my $token=shift;
1.150     www      2957:     my $now=time;
                   2958:     my ($ta,$tb,$lonhost)=split(/\*/,$token);
                   2959:     $lonhost=~tr/A-Z/a-z/;
1.838     albertel 2960:     my $dtoken=$ta.'_'.&hostname($lonhost).'_'.$tb;
1.150     www      2961:     $dtoken=~s/\W/\_/g;
1.234     www      2962:     my ($dummy,$tuname,$tudom,$tcrsid,$symb,$chtim,$rmaddr)=
1.150     www      2963:                  split(/\&/,&unescape(&reply('tmpget:'.$dtoken,$lonhost)));
                   2964: 
1.154     www      2965:     unless (($tuname) && ($tudom)) {
                   2966:         &logthis('Check in '.$token.' ('.$dtoken.') failed');
                   2967:         return '';
                   2968:     }
                   2969:     
                   2970:     unless (&allowed('mgr',$tcrsid)) {
                   2971:         &logthis('Check in '.$token.' ('.$dtoken.') unauthorized: '.
1.620     albertel 2972:                  $env{'user.name'}.' - '.$env{'user.domain'});
1.154     www      2973:         return '';
                   2974:     }
                   2975: 
1.153     www      2976:     my %infohash=('resource.0.intoken' => $token,
                   2977:                   'resource.0.checkintime' => $now,
                   2978:                   'resource.0.inremote' => $ENV{'REMOTE_ADDR'});
1.150     www      2979: 
                   2980:     unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
                   2981:        return '';
                   2982:     }    
                   2983: 
                   2984:     if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
                   2985:                          &escape('Checkin - '.$token)) ne 'ok') {
                   2986: 	return '';
                   2987:     }
                   2988: 
                   2989:     return ($symb,$tuname,$tudom,$tcrsid);    
1.110     www      2990: }
                   2991: 
                   2992: # --------------------------------------------- Set Expire Date for Spreadsheet
                   2993: 
                   2994: sub expirespread {
                   2995:     my ($uname,$udom,$stype,$usymb)=@_;
1.620     albertel 2996:     my $cid=$env{'request.course.id'}; 
1.110     www      2997:     if ($cid) {
                   2998:        my $now=time;
                   2999:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1.620     albertel 3000:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
                   3001:                             $env{'course.'.$cid.'.num'}.
1.110     www      3002: 	        	    ':nohist_expirationdates:'.
                   3003:                             &escape($key).'='.$now,
1.620     albertel 3004:                             $env{'course.'.$cid.'.home'})
1.110     www      3005:     }
                   3006:     return 'ok';
1.14      www      3007: }
                   3008: 
1.109     www      3009: # ----------------------------------------------------- Devalidate Spreadsheets
                   3010: 
                   3011: sub devalidate {
1.325     www      3012:     my ($symb,$uname,$udom)=@_;
1.620     albertel 3013:     my $cid=$env{'request.course.id'}; 
1.109     www      3014:     if ($cid) {
1.391     matthew  3015:         # delete the stored spreadsheets for
                   3016:         # - the student level sheet of this user in course's homespace
                   3017:         # - the assessment level sheet for this resource 
                   3018:         #   for this user in user's homespace
1.553     albertel 3019: 	# - current conditional state info
1.325     www      3020: 	my $key=$uname.':'.$udom.':';
1.109     www      3021:         my $status=
1.299     matthew  3022: 	    &del('nohist_calculatedsheets',
1.391     matthew  3023: 		 [$key.'studentcalc:'],
1.620     albertel 3024: 		 $env{'course.'.$cid.'.domain'},
                   3025: 		 $env{'course.'.$cid.'.num'})
1.133     albertel 3026: 		.' '.
                   3027: 	    &del('nohist_calculatedsheets_'.$cid,
1.391     matthew  3028: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
1.109     www      3029:         unless ($status eq 'ok ok') {
                   3030:            &logthis('Could not devalidate spreadsheet '.
1.325     www      3031:                     $uname.' at '.$udom.' for '.
1.109     www      3032: 		    $symb.': '.$status);
1.133     albertel 3033:         }
1.553     albertel 3034: 	&delenv('user.state.'.$cid);
1.109     www      3035:     }
                   3036: }
                   3037: 
1.265     albertel 3038: sub get_scalar {
                   3039:     my ($string,$end) = @_;
                   3040:     my $value;
                   3041:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
                   3042: 	$value = $1;
                   3043:     } elsif ($$string =~ s/^([^&]*?)&//) {
                   3044: 	$value = $1;
                   3045:     }
                   3046:     return &unescape($value);
                   3047: }
                   3048: 
                   3049: sub array2str {
                   3050:   my (@array) = @_;
                   3051:   my $result=&arrayref2str(\@array);
                   3052:   $result=~s/^__ARRAY_REF__//;
                   3053:   $result=~s/__END_ARRAY_REF__$//;
                   3054:   return $result;
                   3055: }
                   3056: 
1.204     albertel 3057: sub arrayref2str {
                   3058:   my ($arrayref) = @_;
1.265     albertel 3059:   my $result='__ARRAY_REF__';
1.204     albertel 3060:   foreach my $elem (@$arrayref) {
1.265     albertel 3061:     if(ref($elem) eq 'ARRAY') {
                   3062:       $result.=&arrayref2str($elem).'&';
                   3063:     } elsif(ref($elem) eq 'HASH') {
                   3064:       $result.=&hashref2str($elem).'&';
                   3065:     } elsif(ref($elem)) {
                   3066:       #print("Got a ref of ".(ref($elem))." skipping.");
1.204     albertel 3067:     } else {
                   3068:       $result.=&escape($elem).'&';
                   3069:     }
                   3070:   }
                   3071:   $result=~s/\&$//;
1.265     albertel 3072:   $result .= '__END_ARRAY_REF__';
1.204     albertel 3073:   return $result;
                   3074: }
                   3075: 
1.168     albertel 3076: sub hash2str {
1.204     albertel 3077:   my (%hash) = @_;
                   3078:   my $result=&hashref2str(\%hash);
1.265     albertel 3079:   $result=~s/^__HASH_REF__//;
                   3080:   $result=~s/__END_HASH_REF__$//;
1.204     albertel 3081:   return $result;
                   3082: }
                   3083: 
                   3084: sub hashref2str {
                   3085:   my ($hashref)=@_;
1.265     albertel 3086:   my $result='__HASH_REF__';
1.800     albertel 3087:   foreach my $key (sort(keys(%$hashref))) {
                   3088:     if (ref($key) eq 'ARRAY') {
                   3089:       $result.=&arrayref2str($key).'=';
                   3090:     } elsif (ref($key) eq 'HASH') {
                   3091:       $result.=&hashref2str($key).'=';
                   3092:     } elsif (ref($key)) {
1.265     albertel 3093:       $result.='=';
1.800     albertel 3094:       #print("Got a ref of ".(ref($key))." skipping.");
1.204     albertel 3095:     } else {
1.800     albertel 3096: 	if ($key) {$result.=&escape($key).'=';} else { last; }
1.204     albertel 3097:     }
                   3098: 
1.800     albertel 3099:     if(ref($hashref->{$key}) eq 'ARRAY') {
                   3100:       $result.=&arrayref2str($hashref->{$key}).'&';
                   3101:     } elsif(ref($hashref->{$key}) eq 'HASH') {
                   3102:       $result.=&hashref2str($hashref->{$key}).'&';
                   3103:     } elsif(ref($hashref->{$key})) {
1.265     albertel 3104:        $result.='&';
1.800     albertel 3105:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
1.204     albertel 3106:     } else {
1.800     albertel 3107:       $result.=&escape($hashref->{$key}).'&';
1.204     albertel 3108:     }
                   3109:   }
1.168     albertel 3110:   $result=~s/\&$//;
1.265     albertel 3111:   $result .= '__END_HASH_REF__';
1.168     albertel 3112:   return $result;
                   3113: }
                   3114: 
                   3115: sub str2hash {
1.265     albertel 3116:     my ($string)=@_;
                   3117:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
                   3118:     return %$hash;
                   3119: }
                   3120: 
                   3121: sub str2hashref {
1.168     albertel 3122:   my ($string) = @_;
1.265     albertel 3123: 
                   3124:   my %hash;
                   3125: 
                   3126:   if($string !~ /^__HASH_REF__/) {
                   3127:       if (! ($string eq '' || !defined($string))) {
                   3128: 	  $hash{'error'}='Not hash reference';
                   3129:       }
                   3130:       return (\%hash, $string);
                   3131:   }
                   3132: 
                   3133:   $string =~ s/^__HASH_REF__//;
                   3134: 
                   3135:   while($string !~ /^__END_HASH_REF__/) {
                   3136:       #key
                   3137:       my $key='';
                   3138:       if($string =~ /^__HASH_REF__/) {
                   3139:           ($key, $string)=&str2hashref($string);
                   3140:           if(defined($key->{'error'})) {
                   3141:               $hash{'error'}='Bad data';
                   3142:               return (\%hash, $string);
                   3143:           }
                   3144:       } elsif($string =~ /^__ARRAY_REF__/) {
                   3145:           ($key, $string)=&str2arrayref($string);
                   3146:           if($key->[0] eq 'Array reference error') {
                   3147:               $hash{'error'}='Bad data';
                   3148:               return (\%hash, $string);
                   3149:           }
                   3150:       } else {
                   3151:           $string =~ s/^(.*?)=//;
1.267     albertel 3152: 	  $key=&unescape($1);
1.265     albertel 3153:       }
                   3154:       $string =~ s/^=//;
                   3155: 
                   3156:       #value
                   3157:       my $value='';
                   3158:       if($string =~ /^__HASH_REF__/) {
                   3159:           ($value, $string)=&str2hashref($string);
                   3160:           if(defined($value->{'error'})) {
                   3161:               $hash{'error'}='Bad data';
                   3162:               return (\%hash, $string);
                   3163:           }
                   3164:       } elsif($string =~ /^__ARRAY_REF__/) {
                   3165:           ($value, $string)=&str2arrayref($string);
                   3166:           if($value->[0] eq 'Array reference error') {
                   3167:               $hash{'error'}='Bad data';
                   3168:               return (\%hash, $string);
                   3169:           }
                   3170:       } else {
                   3171: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
                   3172:       }
                   3173:       $string =~ s/^&//;
                   3174: 
                   3175:       $hash{$key}=$value;
1.204     albertel 3176:   }
1.265     albertel 3177: 
                   3178:   $string =~ s/^__END_HASH_REF__//;
                   3179: 
                   3180:   return (\%hash, $string);
1.204     albertel 3181: }
                   3182: 
                   3183: sub str2array {
1.265     albertel 3184:     my ($string)=@_;
                   3185:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
                   3186:     return @$array;
                   3187: }
                   3188: 
                   3189: sub str2arrayref {
1.204     albertel 3190:   my ($string) = @_;
1.265     albertel 3191:   my @array;
                   3192: 
                   3193:   if($string !~ /^__ARRAY_REF__/) {
                   3194:       if (! ($string eq '' || !defined($string))) {
                   3195: 	  $array[0]='Array reference error';
                   3196:       }
                   3197:       return (\@array, $string);
                   3198:   }
                   3199: 
                   3200:   $string =~ s/^__ARRAY_REF__//;
                   3201: 
                   3202:   while($string !~ /^__END_ARRAY_REF__/) {
                   3203:       my $value='';
                   3204:       if($string =~ /^__HASH_REF__/) {
                   3205:           ($value, $string)=&str2hashref($string);
                   3206:           if(defined($value->{'error'})) {
                   3207:               $array[0] ='Array reference error';
                   3208:               return (\@array, $string);
                   3209:           }
                   3210:       } elsif($string =~ /^__ARRAY_REF__/) {
                   3211:           ($value, $string)=&str2arrayref($string);
                   3212:           if($value->[0] eq 'Array reference error') {
                   3213:               $array[0] ='Array reference error';
                   3214:               return (\@array, $string);
                   3215:           }
                   3216:       } else {
                   3217: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
                   3218:       }
                   3219:       $string =~ s/^&//;
                   3220: 
                   3221:       push(@array, $value);
1.191     harris41 3222:   }
1.265     albertel 3223: 
                   3224:   $string =~ s/^__END_ARRAY_REF__//;
                   3225: 
                   3226:   return (\@array, $string);
1.168     albertel 3227: }
                   3228: 
1.167     albertel 3229: # -------------------------------------------------------------------Temp Store
                   3230: 
1.168     albertel 3231: sub tmpreset {
                   3232:   my ($symb,$namespace,$domain,$stuname) = @_;
                   3233:   if (!$symb) {
                   3234:     $symb=&symbread();
1.620     albertel 3235:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 3236:   }
                   3237:   $symb=escape($symb);
                   3238: 
1.620     albertel 3239:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.168     albertel 3240:   $namespace=~s/\//\_/g;
                   3241:   $namespace=~s/\W//g;
                   3242: 
1.620     albertel 3243:   if (!$domain) { $domain=$env{'user.domain'}; }
                   3244:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 3245:   if ($domain eq 'public' && $stuname eq 'public') {
                   3246:       $stuname=$ENV{'REMOTE_ADDR'};
                   3247:   }
1.168     albertel 3248:   my $path=$perlvar{'lonDaemons'}.'/tmp';
                   3249:   my %hash;
                   3250:   if (tie(%hash,'GDBM_File',
                   3251: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 3252: 	  &GDBM_WRCREAT(),0640)) {
1.168     albertel 3253:     foreach my $key (keys %hash) {
1.180     albertel 3254:       if ($key=~ /:$symb/) {
1.168     albertel 3255: 	delete($hash{$key});
                   3256:       }
                   3257:     }
                   3258:   }
                   3259: }
                   3260: 
1.167     albertel 3261: sub tmpstore {
1.168     albertel 3262:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   3263: 
                   3264:   if (!$symb) {
                   3265:     $symb=&symbread();
1.620     albertel 3266:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 3267:   }
                   3268:   $symb=escape($symb);
                   3269: 
                   3270:   if (!$namespace) {
                   3271:     # I don't think we would ever want to store this for a course.
                   3272:     # it seems this will only be used if we don't have a course.
1.620     albertel 3273:     #$namespace=$env{'request.course.id'};
1.168     albertel 3274:     #if (!$namespace) {
1.620     albertel 3275:       $namespace=$env{'request.state'};
1.168     albertel 3276:     #}
                   3277:   }
                   3278:   $namespace=~s/\//\_/g;
                   3279:   $namespace=~s/\W//g;
1.620     albertel 3280:   if (!$domain) { $domain=$env{'user.domain'}; }
                   3281:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 3282:   if ($domain eq 'public' && $stuname eq 'public') {
                   3283:       $stuname=$ENV{'REMOTE_ADDR'};
                   3284:   }
1.168     albertel 3285:   my $now=time;
                   3286:   my %hash;
                   3287:   my $path=$perlvar{'lonDaemons'}.'/tmp';
                   3288:   if (tie(%hash,'GDBM_File',
                   3289: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 3290: 	  &GDBM_WRCREAT(),0640)) {
1.168     albertel 3291:     $hash{"version:$symb"}++;
                   3292:     my $version=$hash{"version:$symb"};
                   3293:     my $allkeys=''; 
                   3294:     foreach my $key (keys(%$storehash)) {
                   3295:       $allkeys.=$key.':';
1.591     albertel 3296:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
1.168     albertel 3297:     }
                   3298:     $hash{"$version:$symb:timestamp"}=$now;
                   3299:     $allkeys.='timestamp';
                   3300:     $hash{"$version:keys:$symb"}=$allkeys;
                   3301:     if (untie(%hash)) {
                   3302:       return 'ok';
                   3303:     } else {
                   3304:       return "error:$!";
                   3305:     }
                   3306:   } else {
                   3307:     return "error:$!";
                   3308:   }
                   3309: }
1.167     albertel 3310: 
1.168     albertel 3311: # -----------------------------------------------------------------Temp Restore
1.167     albertel 3312: 
1.168     albertel 3313: sub tmprestore {
                   3314:   my ($symb,$namespace,$domain,$stuname) = @_;
1.167     albertel 3315: 
1.168     albertel 3316:   if (!$symb) {
                   3317:     $symb=&symbread();
1.620     albertel 3318:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 3319:   }
                   3320:   $symb=escape($symb);
                   3321: 
1.620     albertel 3322:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.591     albertel 3323: 
1.620     albertel 3324:   if (!$domain) { $domain=$env{'user.domain'}; }
                   3325:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 3326:   if ($domain eq 'public' && $stuname eq 'public') {
                   3327:       $stuname=$ENV{'REMOTE_ADDR'};
                   3328:   }
1.168     albertel 3329:   my %returnhash;
                   3330:   $namespace=~s/\//\_/g;
                   3331:   $namespace=~s/\W//g;
                   3332:   my %hash;
                   3333:   my $path=$perlvar{'lonDaemons'}.'/tmp';
                   3334:   if (tie(%hash,'GDBM_File',
                   3335: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 3336: 	  &GDBM_READER(),0640)) {
1.168     albertel 3337:     my $version=$hash{"version:$symb"};
                   3338:     $returnhash{'version'}=$version;
                   3339:     my $scope;
                   3340:     for ($scope=1;$scope<=$version;$scope++) {
                   3341:       my $vkeys=$hash{"$scope:keys:$symb"};
                   3342:       my @keys=split(/:/,$vkeys);
                   3343:       my $key;
                   3344:       $returnhash{"$scope:keys"}=$vkeys;
                   3345:       foreach $key (@keys) {
1.591     albertel 3346: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
                   3347: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
1.167     albertel 3348:       }
                   3349:     }
1.168     albertel 3350:     if (!(untie(%hash))) {
                   3351:       return "error:$!";
                   3352:     }
                   3353:   } else {
                   3354:     return "error:$!";
                   3355:   }
                   3356:   return %returnhash;
1.167     albertel 3357: }
                   3358: 
1.9       www      3359: # ----------------------------------------------------------------------- Store
                   3360: 
                   3361: sub store {
1.124     www      3362:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   3363:     my $home='';
                   3364: 
1.168     albertel 3365:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      3366: 
1.213     www      3367:     $symb=&symbclean($symb);
1.122     albertel 3368:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      3369: 
1.620     albertel 3370:     if (!$domain) { $domain=$env{'user.domain'}; }
                   3371:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      3372: 
                   3373:     &devalidate($symb,$stuname,$domain);
1.109     www      3374: 
                   3375:     $symb=escape($symb);
1.187     www      3376:     if (!$namespace) { 
1.620     albertel 3377:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      3378:           return ''; 
                   3379:        } 
                   3380:     }
1.620     albertel 3381:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      3382: 
                   3383:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
                   3384:     $$storehash{'host'}=$perlvar{'lonHostID'};
                   3385: 
1.12      www      3386:     my $namevalue='';
1.800     albertel 3387:     foreach my $key (keys(%$storehash)) {
                   3388:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 3389:     }
1.12      www      3390:     $namevalue=~s/\&$//;
1.187     www      3391:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.124     www      3392:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.9       www      3393: }
                   3394: 
1.47      www      3395: # -------------------------------------------------------------- Critical Store
                   3396: 
                   3397: sub cstore {
1.124     www      3398:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   3399:     my $home='';
                   3400: 
1.168     albertel 3401:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      3402: 
1.213     www      3403:     $symb=&symbclean($symb);
1.122     albertel 3404:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      3405: 
1.620     albertel 3406:     if (!$domain) { $domain=$env{'user.domain'}; }
                   3407:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      3408: 
                   3409:     &devalidate($symb,$stuname,$domain);
1.109     www      3410: 
                   3411:     $symb=escape($symb);
1.187     www      3412:     if (!$namespace) { 
1.620     albertel 3413:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      3414:           return ''; 
                   3415:        } 
                   3416:     }
1.620     albertel 3417:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      3418: 
                   3419:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
                   3420:     $$storehash{'host'}=$perlvar{'lonHostID'};
1.122     albertel 3421: 
1.47      www      3422:     my $namevalue='';
1.800     albertel 3423:     foreach my $key (keys(%$storehash)) {
                   3424:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 3425:     }
1.47      www      3426:     $namevalue=~s/\&$//;
1.187     www      3427:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188     www      3428:     return critical
                   3429:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.47      www      3430: }
                   3431: 
1.9       www      3432: # --------------------------------------------------------------------- Restore
                   3433: 
                   3434: sub restore {
1.124     www      3435:     my ($symb,$namespace,$domain,$stuname) = @_;
                   3436:     my $home='';
                   3437: 
1.168     albertel 3438:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      3439: 
1.122     albertel 3440:     if (!$symb) {
                   3441:       unless ($symb=escape(&symbread())) { return ''; }
                   3442:     } else {
1.213     www      3443:       $symb=&escape(&symbclean($symb));
1.122     albertel 3444:     }
1.188     www      3445:     if (!$namespace) { 
1.620     albertel 3446:        unless ($namespace=$env{'request.course.id'}) { 
1.188     www      3447:           return ''; 
                   3448:        } 
                   3449:     }
1.620     albertel 3450:     if (!$domain) { $domain=$env{'user.domain'}; }
                   3451:     if (!$stuname) { $stuname=$env{'user.name'}; }
                   3452:     if (!$home) { $home=$env{'user.home'}; }
1.122     albertel 3453:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
                   3454: 
1.12      www      3455:     my %returnhash=();
1.800     albertel 3456:     foreach my $line (split(/\&/,$answer)) {
                   3457: 	my ($name,$value)=split(/\=/,$line);
1.591     albertel 3458:         $returnhash{&unescape($name)}=&thaw_unescape($value);
1.191     harris41 3459:     }
1.75      www      3460:     my $version;
                   3461:     for ($version=1;$version<=$returnhash{'version'};$version++) {
1.800     albertel 3462:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
                   3463:           $returnhash{$item}=$returnhash{$version.':'.$item};
1.191     harris41 3464:        }
1.75      www      3465:     }
1.13      www      3466:     return %returnhash;
1.34      www      3467: }
                   3468: 
                   3469: # ---------------------------------------------------------- Course Description
                   3470: 
                   3471: sub coursedescription {
1.731     albertel 3472:     my ($courseid,$args)=@_;
1.34      www      3473:     $courseid=~s/^\///;
1.49      www      3474:     $courseid=~s/\_/\//g;
1.34      www      3475:     my ($cdomain,$cnum)=split(/\//,$courseid);
1.129     albertel 3476:     my $chome=&homeserver($cnum,$cdomain);
1.302     albertel 3477:     my $normalid=$cdomain.'_'.$cnum;
                   3478:     # need to always cache even if we get errors otherwise we keep 
                   3479:     # trying and trying and trying to get the course description.
                   3480:     my %envhash=();
                   3481:     my %returnhash=();
1.731     albertel 3482:     
                   3483:     my $expiretime=600;
                   3484:     if ($env{'request.course.id'} eq $normalid) {
                   3485: 	$expiretime=120;
                   3486:     }
                   3487: 
                   3488:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
                   3489:     if (!$args->{'freshen_cache'}
                   3490: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
                   3491: 	foreach my $key (keys(%env)) {
                   3492: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
                   3493: 	    my ($setting) = $1;
                   3494: 	    $returnhash{$setting} = $env{$key};
                   3495: 	}
                   3496: 	return %returnhash;
                   3497:     }
                   3498: 
                   3499:     # get the data agin
                   3500:     if (!$args->{'one_time'}) {
                   3501: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
                   3502:     }
1.811     albertel 3503: 
1.34      www      3504:     if ($chome ne 'no_host') {
1.302     albertel 3505:        %returnhash=&dump('environment',$cdomain,$cnum);
1.129     albertel 3506:        if (!exists($returnhash{'con_lost'})) {
                   3507:            $returnhash{'home'}= $chome;
                   3508: 	   $returnhash{'domain'} = $cdomain;
                   3509: 	   $returnhash{'num'} = $cnum;
1.741     raeburn  3510:            if (!defined($returnhash{'type'})) {
                   3511:                $returnhash{'type'} = 'Course';
                   3512:            }
1.130     albertel 3513:            while (my ($name,$value) = each %returnhash) {
1.53      www      3514:                $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129     albertel 3515:            }
1.270     www      3516:            $returnhash{'url'}=&clutter($returnhash{'url'});
1.34      www      3517:            $returnhash{'fn'}=$perlvar{'lonDaemons'}.'/tmp/'.
1.620     albertel 3518: 	       $env{'user.name'}.'_'.$cdomain.'_'.$cnum;
1.60      www      3519:            $envhash{'course.'.$normalid.'.home'}=$chome;
                   3520:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
                   3521:            $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34      www      3522:        }
                   3523:     }
1.731     albertel 3524:     if (!$args->{'one_time'}) {
1.949     raeburn  3525: 	&appenv(\%envhash);
1.731     albertel 3526:     }
1.302     albertel 3527:     return %returnhash;
1.461     www      3528: }
                   3529: 
                   3530: # -------------------------------------------------See if a user is privileged
                   3531: 
                   3532: sub privileged {
                   3533:     my ($username,$domain)=@_;
                   3534:     my $rolesdump=&reply("dump:$domain:$username:roles",
                   3535: 			&homeserver($username,$domain));
                   3536:     if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return 0; }
                   3537:     my $now=time;
                   3538:     if ($rolesdump ne '') {
1.800     albertel 3539:         foreach my $entry (split(/&/,$rolesdump)) {
                   3540: 	    if ($entry!~/^rolesdef_/) {
                   3541: 		my ($area,$role)=split(/=/,$entry);
1.461     www      3542: 		$area=~s/\_\w\w$//;
                   3543: 		my ($trole,$tend,$tstart)=split(/_/,$role);
                   3544: 		if (($trole eq 'dc') || ($trole eq 'su')) {
                   3545: 		    my $active=1;
                   3546: 		    if ($tend) {
                   3547: 			if ($tend<$now) { $active=0; }
                   3548: 		    }
                   3549: 		    if ($tstart) {
                   3550: 			if ($tstart>$now) { $active=0; }
                   3551: 		    }
                   3552: 		    if ($active) { return 1; }
                   3553: 		}
                   3554: 	    }
                   3555: 	}
                   3556:     }
                   3557:     return 0;
1.9       www      3558: }
1.1       albertel 3559: 
1.103     harris41 3560: # -------------------------------------------------------- Get user privileges
1.11      www      3561: 
                   3562: sub rolesinit {
                   3563:     my ($domain,$username,$authhost)=@_;
1.966   ! raeburn  3564:     my %userroles;
1.11      www      3565:     my $rolesdump=reply("dump:$domain:$username:roles",$authhost);
1.966   ! raeburn  3566:     if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return \%userroles; }
1.11      www      3567:     my %allroles=();
1.678     raeburn  3568:     my %allgroups=();   
1.11      www      3569:     my $now=time;
1.966   ! raeburn  3570:     %userroles = ('user.login.time' => $now);
1.678     raeburn  3571:     my $group_privs;
1.11      www      3572: 
                   3573:     if ($rolesdump ne '') {
1.800     albertel 3574:         foreach my $entry (split(/&/,$rolesdump)) {
                   3575: 	  if ($entry!~/^rolesdef_/) {
                   3576:             my ($area,$role)=split(/=/,$entry);
1.587     albertel 3577: 	    $area=~s/\_\w\w$//;
1.678     raeburn  3578:             my ($trole,$tend,$tstart,$group_privs);
1.587     albertel 3579: 	    if ($role=~/^cr/) { 
1.807     albertel 3580: 		if ($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
                   3581: 		    ($trole,my $trest)=($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|);
1.655     albertel 3582: 		    ($tend,$tstart)=split('_',$trest);
                   3583: 		} else {
                   3584: 		    $trole=$role;
                   3585: 		}
1.678     raeburn  3586:             } elsif ($role =~ m|^gr/|) {
                   3587:                 ($trole,$tend,$tstart) = split(/_/,$role);
                   3588:                 ($trole,$group_privs) = split(/\//,$trole);
                   3589:                 $group_privs = &unescape($group_privs);
1.587     albertel 3590: 	    } else {
                   3591: 		($trole,$tend,$tstart)=split(/_/,$role);
                   3592: 	    }
1.743     albertel 3593: 	    my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
                   3594: 					 $username);
                   3595: 	    @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
1.567     raeburn  3596:             if (($tend!=0) && ($tend<$now)) { $trole=''; }
                   3597:             if (($tstart!=0) && ($tstart>$now)) { $trole=''; }
1.11      www      3598:             if (($area ne '') && ($trole ne '')) {
1.347     albertel 3599: 		my $spec=$trole.'.'.$area;
                   3600: 		my ($tdummy,$tdomain,$trest)=split(/\//,$area);
                   3601: 		if ($trole =~ /^cr\//) {
1.567     raeburn  3602:                     &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
1.678     raeburn  3603:                 } elsif ($trole eq 'gr') {
                   3604:                     &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
1.347     albertel 3605: 		} else {
1.567     raeburn  3606:                     &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
1.347     albertel 3607: 		}
1.12      www      3608:             }
1.662     raeburn  3609:           }
1.191     harris41 3610:         }
1.743     albertel 3611:         my ($author,$adv) = &set_userprivs(\%userroles,\%allroles,\%allgroups);
                   3612:         $userroles{'user.adv'}    = $adv;
                   3613: 	$userroles{'user.author'} = $author;
1.620     albertel 3614:         $env{'user.adv'}=$adv;
1.11      www      3615:     }
1.743     albertel 3616:     return \%userroles;  
1.11      www      3617: }
                   3618: 
1.567     raeburn  3619: sub set_arearole {
                   3620:     my ($trole,$area,$tstart,$tend,$domain,$username) = @_;
                   3621: # log the associated role with the area
                   3622:     &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
1.743     albertel 3623:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
1.567     raeburn  3624: }
                   3625: 
                   3626: sub custom_roleprivs {
                   3627:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
                   3628:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
                   3629:     my $homsvr=homeserver($rauthor,$rdomain);
1.838     albertel 3630:     if (&hostname($homsvr) ne '') {
1.567     raeburn  3631:         my ($rdummy,$roledef)=
                   3632:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
                   3633:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
                   3634:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
                   3635:             if (defined($syspriv)) {
                   3636:                 $$allroles{'cm./'}.=':'.$syspriv;
                   3637:                 $$allroles{$spec.'./'}.=':'.$syspriv;
                   3638:             }
                   3639:             if ($tdomain ne '') {
                   3640:                 if (defined($dompriv)) {
                   3641:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
                   3642:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
                   3643:                 }
                   3644:                 if (($trest ne '') && (defined($coursepriv))) {
                   3645:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
                   3646:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
                   3647:                 }
                   3648:             }
                   3649:         }
                   3650:     }
                   3651: }
                   3652: 
1.678     raeburn  3653: sub group_roleprivs {
                   3654:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
                   3655:     my $access = 1;
                   3656:     my $now = time;
                   3657:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
                   3658:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
                   3659:     if ($access) {
1.811     albertel 3660:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
1.678     raeburn  3661:         $$allgroups{$course}{$group} .=':'.$group_privs;
                   3662:     }
                   3663: }
1.567     raeburn  3664: 
                   3665: sub standard_roleprivs {
                   3666:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
                   3667:     if (defined($pr{$trole.':s'})) {
                   3668:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
                   3669:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
                   3670:     }
                   3671:     if ($tdomain ne '') {
                   3672:         if (defined($pr{$trole.':d'})) {
                   3673:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   3674:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   3675:         }
                   3676:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
                   3677:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
                   3678:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
                   3679:         }
                   3680:     }
                   3681: }
                   3682: 
                   3683: sub set_userprivs {
1.678     raeburn  3684:     my ($userroles,$allroles,$allgroups) = @_; 
1.567     raeburn  3685:     my $author=0;
                   3686:     my $adv=0;
1.678     raeburn  3687:     my %grouproles = ();
                   3688:     if (keys(%{$allgroups}) > 0) {
                   3689:         foreach my $role (keys %{$allroles}) {
1.681     raeburn  3690:             my ($trole,$area,$sec,$extendedarea);
1.881     raeburn  3691:             if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
1.678     raeburn  3692:                 $trole = $1;
                   3693:                 $area = $2;
1.681     raeburn  3694:                 $sec = $3;
                   3695:                 $extendedarea = $area.$sec;
                   3696:                 if (exists($$allgroups{$area})) {
                   3697:                     foreach my $group (keys(%{$$allgroups{$area}})) {
                   3698:                         my $spec = $trole.'.'.$extendedarea;
                   3699:                         $grouproles{$spec.'.'.$area.'/'.$group} = 
                   3700:                                                 $$allgroups{$area}{$group};
1.678     raeburn  3701:                     }
                   3702:                 }
                   3703:             }
                   3704:         }
                   3705:     }
1.800     albertel 3706:     foreach my $group (keys(%grouproles)) {
                   3707:         $$allroles{$group} = $grouproles{$group};
1.678     raeburn  3708:     }
1.800     albertel 3709:     foreach my $role (keys(%{$allroles})) {
                   3710:         my %thesepriv;
1.941     raeburn  3711:         if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
1.800     albertel 3712:         foreach my $item (split(/:/,$$allroles{$role})) {
                   3713:             if ($item ne '') {
                   3714:                 my ($privilege,$restrictions)=split(/&/,$item);
1.567     raeburn  3715:                 if ($restrictions eq '') {
                   3716:                     $thesepriv{$privilege}='F';
                   3717:                 } elsif ($thesepriv{$privilege} ne 'F') {
                   3718:                     $thesepriv{$privilege}.=$restrictions;
                   3719:                 }
                   3720:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
                   3721:             }
                   3722:         }
                   3723:         my $thesestr='';
1.800     albertel 3724:         foreach my $priv (keys(%thesepriv)) {
                   3725: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
                   3726: 	}
                   3727:         $userroles->{'user.priv.'.$role} = $thesestr;
1.567     raeburn  3728:     }
                   3729:     return ($author,$adv);
                   3730: }
                   3731: 
1.12      www      3732: # --------------------------------------------------------------- get interface
                   3733: 
                   3734: sub get {
1.131     albertel 3735:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      3736:    my $items='';
1.800     albertel 3737:    foreach my $item (@$storearr) {
                   3738:        $items.=&escape($item).'&';
1.191     harris41 3739:    }
1.12      www      3740:    $items=~s/\&$//;
1.620     albertel 3741:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3742:    if (!$uname) { $uname=$env{'user.name'}; }
1.131     albertel 3743:    my $uhome=&homeserver($uname,$udomain);
                   3744: 
1.133     albertel 3745:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      3746:    my @pairs=split(/\&/,$rep);
1.273     albertel 3747:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                   3748:      return @pairs;
                   3749:    }
1.15      www      3750:    my %returnhash=();
1.42      www      3751:    my $i=0;
1.800     albertel 3752:    foreach my $item (@$storearr) {
                   3753:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      3754:       $i++;
1.191     harris41 3755:    }
1.15      www      3756:    return %returnhash;
1.27      www      3757: }
                   3758: 
                   3759: # --------------------------------------------------------------- del interface
                   3760: 
                   3761: sub del {
1.133     albertel 3762:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.27      www      3763:    my $items='';
1.800     albertel 3764:    foreach my $item (@$storearr) {
                   3765:        $items.=&escape($item).'&';
1.191     harris41 3766:    }
1.27      www      3767:    $items=~s/\&$//;
1.620     albertel 3768:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3769:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 3770:    my $uhome=&homeserver($uname,$udomain);
                   3771: 
                   3772:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      3773: }
                   3774: 
                   3775: # -------------------------------------------------------------- dump interface
                   3776: 
                   3777: sub dump {
1.755     albertel 3778:     my ($namespace,$udomain,$uname,$regexp,$range)=@_;
                   3779:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3780:     if (!$uname) { $uname=$env{'user.name'}; }
                   3781:     my $uhome=&homeserver($uname,$udomain);
                   3782:     if ($regexp) {
                   3783: 	$regexp=&escape($regexp);
                   3784:     } else {
                   3785: 	$regexp='.';
                   3786:     }
                   3787:     my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
                   3788:     my @pairs=split(/\&/,$rep);
                   3789:     my %returnhash=();
                   3790:     foreach my $item (@pairs) {
                   3791: 	my ($key,$value)=split(/=/,$item,2);
                   3792: 	$key = &unescape($key);
                   3793: 	next if ($key =~ /^error: 2 /);
                   3794: 	$returnhash{$key}=&thaw_unescape($value);
                   3795:     }
                   3796:     return %returnhash;
1.407     www      3797: }
                   3798: 
1.717     albertel 3799: # --------------------------------------------------------- dumpstore interface
                   3800: 
                   3801: sub dumpstore {
                   3802:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.822     albertel 3803:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3804:    if (!$uname) { $uname=$env{'user.name'}; }
                   3805:    my $uhome=&homeserver($uname,$udomain);
                   3806:    if ($regexp) {
                   3807:        $regexp=&escape($regexp);
                   3808:    } else {
                   3809:        $regexp='.';
                   3810:    }
                   3811:    my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
                   3812:    my @pairs=split(/\&/,$rep);
                   3813:    my %returnhash=();
                   3814:    foreach my $item (@pairs) {
                   3815:        my ($key,$value)=split(/=/,$item,2);
                   3816:        next if ($key =~ /^error: 2 /);
                   3817:        $returnhash{$key}=&thaw_unescape($value);
                   3818:    }
                   3819:    return %returnhash;
1.717     albertel 3820: }
                   3821: 
1.407     www      3822: # -------------------------------------------------------------- keys interface
                   3823: 
                   3824: sub getkeys {
                   3825:    my ($namespace,$udomain,$uname)=@_;
1.620     albertel 3826:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3827:    if (!$uname) { $uname=$env{'user.name'}; }
1.407     www      3828:    my $uhome=&homeserver($uname,$udomain);
                   3829:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
                   3830:    my @keyarray=();
1.800     albertel 3831:    foreach my $key (split(/\&/,$rep)) {
1.812     raeburn  3832:       next if ($key =~ /^error: 2 /);
1.800     albertel 3833:       push(@keyarray,&unescape($key));
1.407     www      3834:    }
                   3835:    return @keyarray;
1.318     matthew  3836: }
                   3837: 
1.319     matthew  3838: # --------------------------------------------------------------- currentdump
                   3839: sub currentdump {
1.328     matthew  3840:    my ($courseid,$sdom,$sname)=@_;
1.620     albertel 3841:    $courseid = $env{'request.course.id'} if (! defined($courseid));
                   3842:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
                   3843:    $sname    = $env{'user.name'}         if (! defined($sname));
1.326     matthew  3844:    my $uhome = &homeserver($sname,$sdom);
                   3845:    my $rep=reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
1.318     matthew  3846:    return if ($rep =~ /^(error:|no_such_host)/);
1.319     matthew  3847:    #
1.318     matthew  3848:    my %returnhash=();
1.319     matthew  3849:    #
                   3850:    if ($rep eq "unknown_cmd") { 
                   3851:        # an old lond will not know currentdump
                   3852:        # Do a dump and make it look like a currentdump
1.822     albertel 3853:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
1.319     matthew  3854:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
                   3855:        my %hash = @tmp;
                   3856:        @tmp=();
1.424     matthew  3857:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319     matthew  3858:    } else {
                   3859:        my @pairs=split(/\&/,$rep);
1.800     albertel 3860:        foreach my $pair (@pairs) {
                   3861:            my ($key,$value)=split(/=/,$pair,2);
1.319     matthew  3862:            my ($symb,$param) = split(/:/,$key);
                   3863:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
1.557     albertel 3864:                                                         &thaw_unescape($value);
1.319     matthew  3865:        }
1.191     harris41 3866:    }
1.12      www      3867:    return %returnhash;
1.424     matthew  3868: }
                   3869: 
                   3870: sub convert_dump_to_currentdump{
                   3871:     my %hash = %{shift()};
                   3872:     my %returnhash;
                   3873:     # Code ripped from lond, essentially.  The only difference
                   3874:     # here is the unescaping done by lonnet::dump().  Conceivably
                   3875:     # we might run in to problems with parameter names =~ /^v\./
                   3876:     while (my ($key,$value) = each(%hash)) {
                   3877:         my ($v,$symb,$param) = split(/:/,$key);
1.822     albertel 3878: 	$symb  = &unescape($symb);
                   3879: 	$param = &unescape($param);
1.424     matthew  3880:         next if ($v eq 'version' || $symb eq 'keys');
                   3881:         next if (exists($returnhash{$symb}) &&
                   3882:                  exists($returnhash{$symb}->{$param}) &&
                   3883:                  $returnhash{$symb}->{'v.'.$param} > $v);
                   3884:         $returnhash{$symb}->{$param}=$value;
                   3885:         $returnhash{$symb}->{'v.'.$param}=$v;
                   3886:     }
                   3887:     #
                   3888:     # Remove all of the keys in the hashes which keep track of
                   3889:     # the version of the parameter.
                   3890:     while (my ($symb,$param_hash) = each(%returnhash)) {
                   3891:         # use a foreach because we are going to delete from the hash.
                   3892:         foreach my $key (keys(%$param_hash)) {
                   3893:             delete($param_hash->{$key}) if ($key =~ /^v\./);
                   3894:         }
                   3895:     }
                   3896:     return \%returnhash;
1.12      www      3897: }
                   3898: 
1.627     albertel 3899: # ------------------------------------------------------ critical inc interface
                   3900: 
                   3901: sub cinc {
                   3902:     return &inc(@_,'critical');
                   3903: }
                   3904: 
1.449     matthew  3905: # --------------------------------------------------------------- inc interface
                   3906: 
                   3907: sub inc {
1.627     albertel 3908:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620     albertel 3909:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3910:     if (!$uname) { $uname=$env{'user.name'}; }
1.449     matthew  3911:     my $uhome=&homeserver($uname,$udomain);
                   3912:     my $items='';
                   3913:     if (! ref($store)) {
                   3914:         # got a single value, so use that instead
                   3915:         $items = &escape($store).'=&';
                   3916:     } elsif (ref($store) eq 'SCALAR') {
                   3917:         $items = &escape($$store).'=&';        
                   3918:     } elsif (ref($store) eq 'ARRAY') {
                   3919:         $items = join('=&',map {&escape($_);} @{$store});
                   3920:     } elsif (ref($store) eq 'HASH') {
                   3921:         while (my($key,$value) = each(%{$store})) {
                   3922:             $items.= &escape($key).'='.&escape($value).'&';
                   3923:         }
                   3924:     }
                   3925:     $items=~s/\&$//;
1.627     albertel 3926:     if ($critical) {
                   3927: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
                   3928:     } else {
                   3929: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
                   3930:     }
1.449     matthew  3931: }
                   3932: 
1.12      www      3933: # --------------------------------------------------------------- put interface
                   3934: 
                   3935: sub put {
1.134     albertel 3936:    my ($namespace,$storehash,$udomain,$uname)=@_;
1.620     albertel 3937:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3938:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 3939:    my $uhome=&homeserver($uname,$udomain);
1.12      www      3940:    my $items='';
1.800     albertel 3941:    foreach my $item (keys(%$storehash)) {
                   3942:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 3943:    }
1.12      www      3944:    $items=~s/\&$//;
1.134     albertel 3945:    return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.47      www      3946: }
                   3947: 
1.631     albertel 3948: # ------------------------------------------------------------ newput interface
                   3949: 
                   3950: sub newput {
                   3951:    my ($namespace,$storehash,$udomain,$uname)=@_;
                   3952:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3953:    if (!$uname) { $uname=$env{'user.name'}; }
                   3954:    my $uhome=&homeserver($uname,$udomain);
                   3955:    my $items='';
                   3956:    foreach my $key (keys(%$storehash)) {
                   3957:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
                   3958:    }
                   3959:    $items=~s/\&$//;
                   3960:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
                   3961: }
                   3962: 
                   3963: # ---------------------------------------------------------  putstore interface
                   3964: 
1.524     raeburn  3965: sub putstore {
1.715     albertel 3966:    my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
1.620     albertel 3967:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3968:    if (!$uname) { $uname=$env{'user.name'}; }
1.524     raeburn  3969:    my $uhome=&homeserver($uname,$udomain);
                   3970:    my $items='';
1.715     albertel 3971:    foreach my $key (keys(%$storehash)) {
                   3972:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524     raeburn  3973:    }
1.715     albertel 3974:    $items=~s/\&$//;
1.716     albertel 3975:    my $esc_symb=&escape($symb);
                   3976:    my $esc_v=&escape($version);
1.715     albertel 3977:    my $reply =
1.716     albertel 3978:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715     albertel 3979: 	      $uhome);
                   3980:    if ($reply eq 'unknown_cmd') {
1.716     albertel 3981:        # gfall back to way things use to be done
1.715     albertel 3982:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
                   3983: 			    $uname);
1.524     raeburn  3984:    }
1.715     albertel 3985:    return $reply;
                   3986: }
                   3987: 
                   3988: sub old_putstore {
1.716     albertel 3989:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
                   3990:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3991:     if (!$uname) { $uname=$env{'user.name'}; }
                   3992:     my $uhome=&homeserver($uname,$udomain);
                   3993:     my %newstorehash;
1.800     albertel 3994:     foreach my $item (keys(%$storehash)) {
                   3995: 	my $key = $version.':'.&escape($symb).':'.$item;
                   3996: 	$newstorehash{$key} = $storehash->{$item};
1.716     albertel 3997:     }
                   3998:     my $items='';
                   3999:     my %allitems = ();
1.800     albertel 4000:     foreach my $item (keys(%newstorehash)) {
                   4001: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
1.716     albertel 4002: 	    my $key = $1.':keys:'.$2;
                   4003: 	    $allitems{$key} .= $3.':';
                   4004: 	}
1.800     albertel 4005: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
1.716     albertel 4006:     }
1.800     albertel 4007:     foreach my $item (keys(%allitems)) {
                   4008: 	$allitems{$item} =~ s/\:$//;
                   4009: 	$items.= $item.'='.$allitems{$item}.'&';
1.716     albertel 4010:     }
                   4011:     $items=~s/\&$//;
                   4012:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524     raeburn  4013: }
                   4014: 
1.47      www      4015: # ------------------------------------------------------ critical put interface
                   4016: 
                   4017: sub cput {
1.134     albertel 4018:    my ($namespace,$storehash,$udomain,$uname)=@_;
1.620     albertel 4019:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   4020:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 4021:    my $uhome=&homeserver($uname,$udomain);
1.47      www      4022:    my $items='';
1.800     albertel 4023:    foreach my $item (keys(%$storehash)) {
                   4024:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 4025:    }
1.47      www      4026:    $items=~s/\&$//;
1.134     albertel 4027:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      4028: }
                   4029: 
                   4030: # -------------------------------------------------------------- eget interface
                   4031: 
                   4032: sub eget {
1.133     albertel 4033:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      4034:    my $items='';
1.800     albertel 4035:    foreach my $item (@$storearr) {
                   4036:        $items.=&escape($item).'&';
1.191     harris41 4037:    }
1.12      www      4038:    $items=~s/\&$//;
1.620     albertel 4039:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   4040:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 4041:    my $uhome=&homeserver($uname,$udomain);
                   4042:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      4043:    my @pairs=split(/\&/,$rep);
                   4044:    my %returnhash=();
1.42      www      4045:    my $i=0;
1.800     albertel 4046:    foreach my $item (@$storearr) {
                   4047:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      4048:       $i++;
1.191     harris41 4049:    }
1.12      www      4050:    return %returnhash;
                   4051: }
                   4052: 
1.667     albertel 4053: # ------------------------------------------------------------ tmpput interface
                   4054: sub tmpput {
1.802     raeburn  4055:     my ($storehash,$server,$context)=@_;
1.667     albertel 4056:     my $items='';
1.800     albertel 4057:     foreach my $item (keys(%$storehash)) {
                   4058: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.667     albertel 4059:     }
                   4060:     $items=~s/\&$//;
1.802     raeburn  4061:     if (defined($context)) {
                   4062:         $items .= ':'.&escape($context);
                   4063:     }
1.667     albertel 4064:     return &reply("tmpput:$items",$server);
                   4065: }
                   4066: 
                   4067: # ------------------------------------------------------------ tmpget interface
                   4068: sub tmpget {
1.688     albertel 4069:     my ($token,$server)=@_;
                   4070:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   4071:     my $rep=&reply("tmpget:$token",$server);
1.667     albertel 4072:     my %returnhash;
                   4073:     foreach my $item (split(/\&/,$rep)) {
                   4074: 	my ($key,$value)=split(/=/,$item);
1.951     raeburn  4075:         next if ($key =~ /^error: 2 /);
1.667     albertel 4076: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
                   4077:     }
                   4078:     return %returnhash;
                   4079: }
                   4080: 
1.688     albertel 4081: # ------------------------------------------------------------ tmpget interface
                   4082: sub tmpdel {
                   4083:     my ($token,$server)=@_;
                   4084:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   4085:     return &reply("tmpdel:$token",$server);
                   4086: }
                   4087: 
1.765     albertel 4088: # -------------------------------------------------- portfolio access checking
                   4089: 
                   4090: sub portfolio_access {
1.766     albertel 4091:     my ($requrl) = @_;
1.765     albertel 4092:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
                   4093:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group);
1.814     raeburn  4094:     if ($result) {
                   4095:         my %setters;
                   4096:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   4097:             my ($startblock,$endblock) =
                   4098:                 &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
                   4099:             if ($startblock && $endblock) {
                   4100:                 return 'B';
                   4101:             }
                   4102:         } else {
                   4103:             my ($startblock,$endblock) =
                   4104:                 &Apache::loncommon::blockcheck(\%setters,'port');
                   4105:             if ($startblock && $endblock) {
                   4106:                 return 'B';
                   4107:             }
                   4108:         }
                   4109:     }
1.765     albertel 4110:     if ($result eq 'ok') {
1.766     albertel 4111:        return 'F';
1.765     albertel 4112:     } elsif ($result =~ /^[^:]+:guest_/) {
1.766     albertel 4113:        return 'A';
1.765     albertel 4114:     }
1.766     albertel 4115:     return '';
1.765     albertel 4116: }
                   4117: 
                   4118: sub get_portfolio_access {
1.767     albertel 4119:     my ($udom,$unum,$file_name,$group,$access_hash) = @_;
                   4120: 
                   4121:     if (!ref($access_hash)) {
                   4122: 	my $current_perms = &get_portfile_permissions($udom,$unum);
                   4123: 	my %access_controls = &get_access_controls($current_perms,$group,
                   4124: 						   $file_name);
                   4125: 	$access_hash = $access_controls{$file_name};
                   4126:     }
                   4127: 
1.765     albertel 4128:     my ($public,$guest,@domains,@users,@courses,@groups);
                   4129:     my $now = time;
                   4130:     if (ref($access_hash) eq 'HASH') {
                   4131:         foreach my $key (keys(%{$access_hash})) {
                   4132:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                   4133:             if ($start > $now) {
                   4134:                 next;
                   4135:             }
                   4136:             if ($end && $end<$now) {
                   4137:                 next;
                   4138:             }
                   4139:             if ($scope eq 'public') {
                   4140:                 $public = $key;
                   4141:                 last;
                   4142:             } elsif ($scope eq 'guest') {
                   4143:                 $guest = $key;
                   4144:             } elsif ($scope eq 'domains') {
                   4145:                 push(@domains,$key);
                   4146:             } elsif ($scope eq 'users') {
                   4147:                 push(@users,$key);
                   4148:             } elsif ($scope eq 'course') {
                   4149:                 push(@courses,$key);
                   4150:             } elsif ($scope eq 'group') {
                   4151:                 push(@groups,$key);
                   4152:             }
                   4153:         }
                   4154:         if ($public) {
                   4155:             return 'ok';
                   4156:         }
                   4157:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   4158:             if ($guest) {
                   4159:                 return $guest;
                   4160:             }
                   4161:         } else {
                   4162:             if (@domains > 0) {
                   4163:                 foreach my $domkey (@domains) {
                   4164:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
                   4165:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
                   4166:                             return 'ok';
                   4167:                         }
                   4168:                     }
                   4169:                 }
                   4170:             }
                   4171:             if (@users > 0) {
                   4172:                 foreach my $userkey (@users) {
1.865     raeburn  4173:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
                   4174:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
                   4175:                             if (ref($item) eq 'HASH') {
                   4176:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
                   4177:                                     ($item->{'udom'} eq $env{'user.domain'})) {
                   4178:                                     return 'ok';
                   4179:                                 }
                   4180:                             }
                   4181:                         }
                   4182:                     } 
1.765     albertel 4183:                 }
                   4184:             }
                   4185:             my %roleshash;
                   4186:             my @courses_and_groups = @courses;
                   4187:             push(@courses_and_groups,@groups); 
                   4188:             if (@courses_and_groups > 0) {
                   4189:                 my (%allgroups,%allroles); 
                   4190:                 my ($start,$end,$role,$sec,$group);
                   4191:                 foreach my $envkey (%env) {
1.811     albertel 4192:                     if ($envkey =~ m-^user\.role\.(gr|cc|in|ta|ep|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 4193:                         my $cid = $2.'_'.$3; 
                   4194:                         if ($1 eq 'gr') {
                   4195:                             $group = $4;
                   4196:                             $allgroups{$cid}{$group} = $env{$envkey};
                   4197:                         } else {
                   4198:                             if ($4 eq '') {
                   4199:                                 $sec = 'none';
                   4200:                             } else {
                   4201:                                 $sec = $4;
                   4202:                             }
                   4203:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   4204:                         }
1.811     albertel 4205:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 4206:                         my $cid = $2.'_'.$3;
                   4207:                         if ($4 eq '') {
                   4208:                             $sec = 'none';
                   4209:                         } else {
                   4210:                             $sec = $4;
                   4211:                         }
                   4212:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   4213:                     }
                   4214:                 }
                   4215:                 if (keys(%allroles) == 0) {
                   4216:                     return;
                   4217:                 }
                   4218:                 foreach my $key (@courses_and_groups) {
                   4219:                     my %content = %{$$access_hash{$key}};
                   4220:                     my $cnum = $content{'number'};
                   4221:                     my $cdom = $content{'domain'};
                   4222:                     my $cid = $cdom.'_'.$cnum;
                   4223:                     if (!exists($allroles{$cid})) {
                   4224:                         next;
                   4225:                     }    
                   4226:                     foreach my $role_id (keys(%{$content{'roles'}})) {
                   4227:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
                   4228:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
                   4229:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
                   4230:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
                   4231:                         foreach my $role (keys(%{$allroles{$cid}})) {
                   4232:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
                   4233:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
                   4234:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
                   4235:                                         if (grep/^all$/,@sections) {
                   4236:                                             return 'ok';
                   4237:                                         } else {
                   4238:                                             if (grep/^$sec$/,@sections) {
                   4239:                                                 return 'ok';
                   4240:                                             }
                   4241:                                         }
                   4242:                                     }
                   4243:                                 }
                   4244:                                 if (keys(%{$allgroups{$cid}}) == 0) {
                   4245:                                     if (grep/^none$/,@groups) {
                   4246:                                         return 'ok';
                   4247:                                     }
                   4248:                                 } else {
                   4249:                                     if (grep/^all$/,@groups) {
                   4250:                                         return 'ok';
                   4251:                                     } 
                   4252:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
                   4253:                                         if (grep/^$group$/,@groups) {
                   4254:                                             return 'ok';
                   4255:                                         }
                   4256:                                     }
                   4257:                                 } 
                   4258:                             }
                   4259:                         }
                   4260:                     }
                   4261:                 }
                   4262:             }
                   4263:             if ($guest) {
                   4264:                 return $guest;
                   4265:             }
                   4266:         }
                   4267:     }
                   4268:     return;
                   4269: }
                   4270: 
                   4271: sub course_group_datechecker {
                   4272:     my ($dates,$now,$status) = @_;
                   4273:     my ($start,$end) = split(/\./,$dates);
                   4274:     if (!$start && !$end) {
                   4275:         return 'ok';
                   4276:     }
                   4277:     if (grep/^active$/,@{$status}) {
                   4278:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
                   4279:             return 'ok';
                   4280:         }
                   4281:     }
                   4282:     if (grep/^previous$/,@{$status}) {
                   4283:         if ($end > $now ) {
                   4284:             return 'ok';
                   4285:         }
                   4286:     }
                   4287:     if (grep/^future$/,@{$status}) {
                   4288:         if ($start > $now) {
                   4289:             return 'ok';
                   4290:         }
                   4291:     }
                   4292:     return; 
                   4293: }
                   4294: 
                   4295: sub parse_portfolio_url {
                   4296:     my ($url) = @_;
                   4297: 
                   4298:     my ($type,$udom,$unum,$group,$file_name);
                   4299:     
1.823     albertel 4300:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
1.765     albertel 4301: 	$type = 1;
                   4302:         $udom = $1;
                   4303:         $unum = $2;
                   4304:         $file_name = $3;
1.823     albertel 4305:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
1.765     albertel 4306: 	$type = 2;
                   4307:         $udom = $1;
                   4308:         $unum = $2;
                   4309:         $group = $3;
                   4310:         $file_name = $3.'/'.$4;
                   4311:     }
                   4312:     if (wantarray) {
                   4313: 	return ($type,$udom,$unum,$file_name,$group);
                   4314:     }
                   4315:     return $type;
                   4316: }
                   4317: 
                   4318: sub is_portfolio_url {
                   4319:     my ($url) = @_;
                   4320:     return scalar(&parse_portfolio_url($url));
                   4321: }
                   4322: 
1.798     raeburn  4323: sub is_portfolio_file {
                   4324:     my ($file) = @_;
1.820     raeburn  4325:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
1.798     raeburn  4326:         return 1;
                   4327:     }
                   4328:     return;
                   4329: }
                   4330: 
                   4331: 
1.341     www      4332: # ---------------------------------------------- Custom access rule evaluation
                   4333: 
                   4334: sub customaccess {
                   4335:     my ($priv,$uri)=@_;
1.807     albertel 4336:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819     www      4337:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807     albertel 4338:     $udom = &LONCAPA::clean_domain($udom);
                   4339:     $ucrs = &LONCAPA::clean_username($ucrs);
1.341     www      4340:     my $access=0;
1.800     albertel 4341:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.893     albertel 4342: 	my ($effect,$realm,$role,$type)=split(/\:/,$right);
                   4343: 	if ($type eq 'user') {
                   4344: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
1.896     albertel 4345: 		my ($tdom,$tuname)=split(m{/},$scope);
1.893     albertel 4346: 		if ($tdom) {
                   4347: 		    if ($tdom ne $env{'user.domain'}) { next; }
                   4348: 		}
1.896     albertel 4349: 		if ($tuname) {
                   4350: 		    if ($tuname ne $env{'user.name'}) { next; }
1.893     albertel 4351: 		}
                   4352: 		$access=($effect eq 'allow');
                   4353: 		last;
                   4354: 	    }
                   4355: 	} else {
                   4356: 	    if ($role) {
                   4357: 		if ($role ne $urole) { next; }
                   4358: 	    }
                   4359: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
                   4360: 		my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
                   4361: 		if ($tdom) {
                   4362: 		    if ($tdom ne $udom) { next; }
                   4363: 		}
                   4364: 		if ($tcrs) {
                   4365: 		    if ($tcrs ne $ucrs) { next; }
                   4366: 		}
                   4367: 		if ($tsec) {
                   4368: 		    if ($tsec ne $usec) { next; }
                   4369: 		}
                   4370: 		$access=($effect eq 'allow');
                   4371: 		last;
                   4372: 	    }
                   4373: 	    if ($realm eq '' && $role eq '') {
                   4374: 		$access=($effect eq 'allow');
                   4375: 	    }
1.402     bowersj2 4376: 	}
1.341     www      4377:     }
                   4378:     return $access;
                   4379: }
                   4380: 
1.103     harris41 4381: # ------------------------------------------------- Check for a user privilege
1.12      www      4382: 
                   4383: sub allowed {
1.810     raeburn  4384:     my ($priv,$uri,$symb,$role)=@_;
1.705     albertel 4385:     my $ver_orguri=$uri;
1.439     www      4386:     $uri=&deversion($uri);
1.152     www      4387:     my $orguri=$uri;
1.52      www      4388:     $uri=&declutter($uri);
1.809     raeburn  4389: 
1.810     raeburn  4390:     if ($priv eq 'evb') {
                   4391: # Evade communication block restrictions for specified role in a course
                   4392:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
                   4393:             return $1;
                   4394:         } else {
                   4395:             return;
                   4396:         }
                   4397:     }
                   4398: 
1.620     albertel 4399:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54      www      4400: # Free bre access to adm and meta resources
1.775     albertel 4401:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard)$})) 
1.769     albertel 4402: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
                   4403: 	&& ($priv eq 'bre')) {
1.14      www      4404: 	return 'F';
1.159     www      4405:     }
                   4406: 
1.545     banghart 4407: # Free bre access to user's own portfolio contents
1.714     raeburn  4408:     my ($space,$domain,$name,@dir)=split('/',$uri);
1.647     raeburn  4409:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
1.714     raeburn  4410: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814     raeburn  4411:         my %setters;
                   4412:         my ($startblock,$endblock) = 
                   4413:             &Apache::loncommon::blockcheck(\%setters,'port');
                   4414:         if ($startblock && $endblock) {
                   4415:             return 'B';
                   4416:         } else {
                   4417:             return 'F';
                   4418:         }
1.545     banghart 4419:     }
                   4420: 
1.762     raeburn  4421: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714     raeburn  4422:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
                   4423:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
                   4424:         if (exists($env{'request.course.id'})) {
                   4425:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   4426:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   4427:             if (($domain eq $cdom) && ($name eq $cnum)) {
                   4428:                 my $courseprivid=$env{'request.course.id'};
                   4429:                 $courseprivid=~s/\_/\//;
                   4430:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
                   4431:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
                   4432:                     return $1; 
1.762     raeburn  4433:                 } else {
                   4434:                     if ($env{'request.course.sec'}) {
                   4435:                         $courseprivid.='/'.$env{'request.course.sec'};
                   4436:                     }
                   4437:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
                   4438:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
                   4439:                         return $2;
                   4440:                     }
1.714     raeburn  4441:                 }
                   4442:             }
                   4443:         }
                   4444:     }
                   4445: 
1.159     www      4446: # Free bre to public access
                   4447: 
                   4448:     if ($priv eq 'bre') {
1.238     www      4449:         my $copyright=&metadata($uri,'copyright');
1.620     albertel 4450: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
1.301     www      4451:            return 'F'; 
                   4452:         }
1.238     www      4453:         if ($copyright eq 'priv') {
                   4454:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 4455: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238     www      4456: 		return '';
                   4457:             }
                   4458:         }
                   4459:         if ($copyright eq 'domain') {
                   4460:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 4461: 	    unless (($env{'user.domain'} eq $1) ||
                   4462:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238     www      4463: 		return '';
                   4464:             }
1.262     matthew  4465:         }
1.620     albertel 4466:         if ($env{'request.role'}=~ /li\.\//) {
1.262     matthew  4467:             # Library role, so allow browsing of resources in this domain.
                   4468:             return 'F';
1.238     www      4469:         }
1.341     www      4470:         if ($copyright eq 'custom') {
                   4471: 	    unless (&customaccess($priv,$uri)) { return ''; }
                   4472:         }
1.14      www      4473:     }
1.264     matthew  4474:     # Domain coordinator is trying to create a course
1.620     albertel 4475:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264     matthew  4476:         # uri is the requested domain in this case.
                   4477:         # comparison to 'request.role.domain' shows if the user has selected
1.678     raeburn  4478:         # a role of dc for the domain in question.
1.620     albertel 4479:         return 'F' if ($uri eq $env{'request.role.domain'});
1.264     matthew  4480:     }
1.29      www      4481: 
1.52      www      4482:     my $thisallowed='';
                   4483:     my $statecond=0;
                   4484:     my $courseprivid='';
                   4485: 
                   4486: # Course
                   4487: 
1.620     albertel 4488:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.52      www      4489:        $thisallowed.=$1;
                   4490:     }
1.29      www      4491: 
1.52      www      4492: # Domain
                   4493: 
1.620     albertel 4494:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479     albertel 4495:        =~/\Q$priv\E\&([^\:]*)/) {
1.12      www      4496:        $thisallowed.=$1;
                   4497:     }
1.52      www      4498: 
                   4499: # Course: uri itself is a course
1.66      www      4500:     my $courseuri=$uri;
                   4501:     $courseuri=~s/\_(\d)/\/$1/;
1.83      www      4502:     $courseuri=~s/^([^\/])/\/$1/;
1.81      www      4503: 
1.620     albertel 4504:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479     albertel 4505:        =~/\Q$priv\E\&([^\:]*)/) {
1.12      www      4506:        $thisallowed.=$1;
                   4507:     }
1.29      www      4508: 
1.665     albertel 4509: # URI is an uploaded document for this course, default permissions don't matter
1.611     albertel 4510: # not allowing 'edit' access (editupload) to uploaded course docs
1.492     albertel 4511:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665     albertel 4512: 	$thisallowed='';
1.671     raeburn  4513:         my ($match)=&is_on_map($uri);
                   4514:         if ($match) {
                   4515:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
                   4516:                   =~/\Q$priv\E\&([^\:]*)/) {
                   4517:                 $thisallowed.=$1;
                   4518:             }
                   4519:         } else {
1.705     albertel 4520:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671     raeburn  4521:             if ($refuri) {
                   4522:                 if ($refuri =~ m|^/adm/|) {
1.669     raeburn  4523:                     $thisallowed='F';
1.671     raeburn  4524:                 } else {
                   4525:                     $refuri=&declutter($refuri);
                   4526:                     my ($match) = &is_on_map($refuri);
                   4527:                     if ($match) {
                   4528:                         $thisallowed='F';
                   4529:                     }
1.669     raeburn  4530:                 }
1.671     raeburn  4531:             }
                   4532:         }
1.314     www      4533:     }
1.492     albertel 4534: 
1.766     albertel 4535:     if ($priv eq 'bre'
                   4536: 	&& $thisallowed ne 'F' 
                   4537: 	&& $thisallowed ne '2'
                   4538: 	&& &is_portfolio_url($uri)) {
                   4539: 	$thisallowed = &portfolio_access($uri);
                   4540:     }
                   4541:     
1.52      www      4542: # Full access at system, domain or course-wide level? Exit.
1.29      www      4543:     if ($thisallowed=~/F/) {
                   4544: 	return 'F';
                   4545:     }
                   4546: 
1.52      www      4547: # If this is generating or modifying users, exit with special codes
1.29      www      4548: 
1.643     www      4549:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
                   4550: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642     albertel 4551: 	    my ($audom,$auname)=split('/',$uri);
1.643     www      4552: # no author name given, so this just checks on the general right to make a co-author in this domain
                   4553: 	    unless ($auname) { return $thisallowed; }
                   4554: # an author name is given, so we are about to actually make a co-author for a certain account
1.642     albertel 4555: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
                   4556: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
                   4557: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
                   4558: 	}
1.52      www      4559: 	return $thisallowed;
                   4560:     }
                   4561: #
1.103     harris41 4562: # Gathered so far: system, domain and course wide privileges
1.52      www      4563: #
                   4564: # Course: See if uri or referer is an individual resource that is part of 
                   4565: # the course
                   4566: 
1.620     albertel 4567:     if ($env{'request.course.id'}) {
1.232     www      4568: 
1.620     albertel 4569:        $courseprivid=$env{'request.course.id'};
                   4570:        if ($env{'request.course.sec'}) {
                   4571:           $courseprivid.='/'.$env{'request.course.sec'};
1.52      www      4572:        }
                   4573:        $courseprivid=~s/\_/\//;
                   4574:        my $checkreferer=1;
1.232     www      4575:        my ($match,$cond)=&is_on_map($uri);
                   4576:        if ($match) {
                   4577:            $statecond=$cond;
1.620     albertel 4578:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 4579:                =~/\Q$priv\E\&([^\:]*)/) {
1.52      www      4580:                $thisallowed.=$1;
                   4581:                $checkreferer=0;
                   4582:            }
1.29      www      4583:        }
1.83      www      4584:        
1.148     www      4585:        if ($checkreferer) {
1.620     albertel 4586: 	  my $refuri=$env{'httpref.'.$orguri};
1.148     www      4587:             unless ($refuri) {
1.800     albertel 4588:                 foreach my $key (keys(%env)) {
                   4589: 		    if ($key=~/^httpref\..*\*/) {
                   4590: 			my $pattern=$key;
1.156     www      4591:                         $pattern=~s/^httpref\.\/res\///;
1.148     www      4592:                         $pattern=~s/\*/\[\^\/\]\+/g;
                   4593:                         $pattern=~s/\//\\\//g;
1.152     www      4594:                         if ($orguri=~/$pattern/) {
1.800     albertel 4595: 			    $refuri=$env{$key};
1.148     www      4596:                         }
                   4597:                     }
1.191     harris41 4598:                 }
1.148     www      4599:             }
1.232     www      4600: 
1.148     www      4601:          if ($refuri) { 
1.152     www      4602: 	  $refuri=&declutter($refuri);
1.232     www      4603:           my ($match,$cond)=&is_on_map($refuri);
                   4604:             if ($match) {
                   4605:               my $refstatecond=$cond;
1.620     albertel 4606:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 4607:                   =~/\Q$priv\E\&([^\:]*)/) {
1.52      www      4608:                   $thisallowed.=$1;
1.53      www      4609:                   $uri=$refuri;
                   4610:                   $statecond=$refstatecond;
1.52      www      4611:               }
                   4612:           }
1.148     www      4613:         }
1.29      www      4614:        }
1.52      www      4615:    }
1.29      www      4616: 
1.52      www      4617: #
1.103     harris41 4618: # Gathered now: all privileges that could apply, and condition number
1.52      www      4619: # 
                   4620: #
                   4621: # Full or no access?
                   4622: #
1.29      www      4623: 
1.52      www      4624:     if ($thisallowed=~/F/) {
                   4625: 	return 'F';
                   4626:     }
1.29      www      4627: 
1.52      www      4628:     unless ($thisallowed) {
                   4629:         return '';
                   4630:     }
1.29      www      4631: 
1.52      www      4632: # Restrictions exist, deal with them
                   4633: #
                   4634: #   C:according to course preferences
                   4635: #   R:according to resource settings
                   4636: #   L:unless locked
                   4637: #   X:according to user session state
                   4638: #
                   4639: 
                   4640: # Possibly locked functionality, check all courses
1.54      www      4641: # Locks might take effect only after 10 minutes cache expiration for other
                   4642: # courses, and 2 minutes for current course
1.52      www      4643: 
                   4644:     my $envkey;
                   4645:     if ($thisallowed=~/L/) {
1.620     albertel 4646:         foreach $envkey (keys %env) {
1.54      www      4647:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
                   4648:                my $courseid=$2;
                   4649:                my $roleid=$1.'.'.$2;
1.92      www      4650:                $courseid=~s/^\///;
1.54      www      4651:                my $expiretime=600;
1.620     albertel 4652:                if ($env{'request.role'} eq $roleid) {
1.54      www      4653: 		  $expiretime=120;
                   4654:                }
                   4655: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
                   4656:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620     albertel 4657:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731     albertel 4658: 		   &coursedescription($courseid,{'freshen_cache' => 1});
1.54      www      4659:                }
1.620     albertel 4660:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   4661:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
                   4662: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
                   4663:                        &log($env{'user.domain'},$env{'user.name'},
                   4664:                             $env{'user.home'},
1.57      www      4665:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52      www      4666:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 4667:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      4668: 		       return '';
                   4669:                    }
                   4670:                }
1.620     albertel 4671:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   4672:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
                   4673: 		   if ($env{'priv.'.$priv.'.lock.expire'}>time) {
                   4674:                        &log($env{'user.domain'},$env{'user.name'},
                   4675:                             $env{'user.home'},
1.57      www      4676:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52      www      4677:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 4678:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      4679: 		       return '';
                   4680:                    }
                   4681:                }
                   4682: 	   }
1.29      www      4683:        }
1.52      www      4684:     }
                   4685:    
                   4686: #
                   4687: # Rest of the restrictions depend on selected course
                   4688: #
                   4689: 
1.620     albertel 4690:     unless ($env{'request.course.id'}) {
1.766     albertel 4691: 	if ($thisallowed eq 'A') {
                   4692: 	    return 'A';
1.814     raeburn  4693:         } elsif ($thisallowed eq 'B') {
                   4694:             return 'B';
1.766     albertel 4695: 	} else {
                   4696: 	    return '1';
                   4697: 	}
1.52      www      4698:     }
1.29      www      4699: 
1.52      www      4700: #
                   4701: # Now user is definitely in a course
                   4702: #
1.53      www      4703: 
                   4704: 
                   4705: # Course preferences
                   4706: 
                   4707:    if ($thisallowed=~/C/) {
1.620     albertel 4708:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
                   4709:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
                   4710:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479     albertel 4711: 	   =~/\Q$rolecode\E/) {
1.689     albertel 4712: 	   if ($priv ne 'pch') { 
                   4713: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   4714: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
                   4715: 			$env{'request.course.id'});
                   4716: 	   }
1.237     www      4717:            return '';
                   4718:        }
                   4719: 
1.620     albertel 4720:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479     albertel 4721: 	   =~/\Q$unamedom\E/) {
1.689     albertel 4722: 	   if ($priv ne 'pch') { 
                   4723: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
                   4724: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
                   4725: 			$env{'request.course.id'});
                   4726: 	   }
1.54      www      4727:            return '';
                   4728:        }
1.53      www      4729:    }
                   4730: 
                   4731: # Resource preferences
                   4732: 
                   4733:    if ($thisallowed=~/R/) {
1.620     albertel 4734:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479     albertel 4735:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.689     albertel 4736: 	   if ($priv ne 'pch') { 
                   4737: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   4738: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
                   4739: 	   }
                   4740: 	   return '';
1.54      www      4741:        }
1.53      www      4742:    }
1.30      www      4743: 
1.246     www      4744: # Restricted by state or randomout?
1.30      www      4745: 
1.52      www      4746:    if ($thisallowed=~/X/) {
1.620     albertel 4747:       if ($env{'acc.randomout'}) {
1.579     albertel 4748: 	 if (!$symb) { $symb=&symbread($uri,1); }
1.620     albertel 4749:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
1.248     www      4750:             return ''; 
                   4751:          }
1.247     www      4752:       }
                   4753:       if (&condval($statecond)) {
1.52      www      4754: 	 return '2';
                   4755:       } else {
                   4756:          return '';
                   4757:       }
                   4758:    }
1.30      www      4759: 
1.766     albertel 4760:     if ($thisallowed eq 'A') {
                   4761: 	return 'A';
1.814     raeburn  4762:     } elsif ($thisallowed eq 'B') {
                   4763:         return 'B';
1.766     albertel 4764:     }
1.52      www      4765:    return 'F';
1.232     www      4766: }
                   4767: 
1.710     albertel 4768: sub split_uri_for_cond {
                   4769:     my $uri=&deversion(&declutter(shift));
                   4770:     my @uriparts=split(/\//,$uri);
                   4771:     my $filename=pop(@uriparts);
                   4772:     my $pathname=join('/',@uriparts);
                   4773:     return ($pathname,$filename);
                   4774: }
1.232     www      4775: # --------------------------------------------------- Is a resource on the map?
                   4776: 
                   4777: sub is_on_map {
1.710     albertel 4778:     my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289     bowersj2 4779:     #Trying to find the conditional for the file
1.620     albertel 4780:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289     bowersj2 4781: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
1.232     www      4782:     if ($match) {
1.289     bowersj2 4783: 	return (1,$1);
                   4784:     } else {
1.434     www      4785: 	return (0,0);
1.289     bowersj2 4786:     }
1.12      www      4787: }
                   4788: 
1.427     www      4789: # --------------------------------------------------------- Get symb from alias
                   4790: 
                   4791: sub get_symb_from_alias {
                   4792:     my $symb=shift;
                   4793:     my ($map,$resid,$url)=&decode_symb($symb);
                   4794: # Already is a symb
                   4795:     if ($url) { return $symb; }
                   4796: # Must be an alias
                   4797:     my $aliassymb='';
                   4798:     my %bighash;
1.620     albertel 4799:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427     www      4800:                             &GDBM_READER(),0640)) {
                   4801:         my $rid=$bighash{'mapalias_'.$symb};
                   4802: 	if ($rid) {
                   4803: 	    my ($mapid,$resid)=split(/\./,$rid);
1.429     albertel 4804: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
                   4805: 				    $resid,$bighash{'src_'.$rid});
1.427     www      4806: 	}
                   4807:         untie %bighash;
                   4808:     }
                   4809:     return $aliassymb;
                   4810: }
                   4811: 
1.12      www      4812: # ----------------------------------------------------------------- Define Role
                   4813: 
                   4814: sub definerole {
                   4815:   if (allowed('mcr','/')) {
                   4816:     my ($rolename,$sysrole,$domrole,$courole)=@_;
1.800     albertel 4817:     foreach my $role (split(':',$sysrole)) {
                   4818: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 4819:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
                   4820:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
                   4821: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      4822:                return "refused:s:$crole&$cqual"; 
                   4823:             }
                   4824:         }
1.191     harris41 4825:     }
1.800     albertel 4826:     foreach my $role (split(':',$domrole)) {
                   4827: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 4828:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
                   4829:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
                   4830: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
1.21      www      4831:                return "refused:d:$crole&$cqual"; 
                   4832:             }
                   4833:         }
1.191     harris41 4834:     }
1.800     albertel 4835:     foreach my $role (split(':',$courole)) {
                   4836: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 4837:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
                   4838:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
                   4839: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      4840:                return "refused:c:$crole&$cqual"; 
                   4841:             }
                   4842:         }
1.191     harris41 4843:     }
1.620     albertel 4844:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
                   4845:                 "$env{'user.domain'}:$env{'user.name'}:".
1.21      www      4846: 	        "rolesdef_$rolename=".
                   4847:                 escape($sysrole.'_'.$domrole.'_'.$courole);
1.620     albertel 4848:     return reply($command,$env{'user.home'});
1.12      www      4849:   } else {
                   4850:     return 'refused';
                   4851:   }
1.105     harris41 4852: }
                   4853: 
                   4854: # ---------------- Make a metadata query against the network of library servers
                   4855: 
                   4856: sub metadata_query {
1.244     matthew  4857:     my ($query,$custom,$customshow,$server_array)=@_;
1.120     harris41 4858:     my %rhash;
1.845     albertel 4859:     my %libserv = &all_library();
1.244     matthew  4860:     my @server_list = (defined($server_array) ? @$server_array
                   4861:                                               : keys(%libserv) );
                   4862:     for my $server (@server_list) {
1.118     harris41 4863: 	unless ($custom or $customshow) {
                   4864: 	    my $reply=&reply("querysend:".&escape($query),$server);
                   4865: 	    $rhash{$server}=$reply;
                   4866: 	}
                   4867: 	else {
                   4868: 	    my $reply=&reply("querysend:".&escape($query).':'.
                   4869: 			     &escape($custom).':'.&escape($customshow),
                   4870: 			     $server);
                   4871: 	    $rhash{$server}=$reply;
                   4872: 	}
1.112     harris41 4873:     }
1.118     harris41 4874:     return \%rhash;
1.240     www      4875: }
                   4876: 
                   4877: # ----------------------------------------- Send log queries and wait for reply
                   4878: 
                   4879: sub log_query {
                   4880:     my ($uname,$udom,$query,%filters)=@_;
                   4881:     my $uhome=&homeserver($uname,$udom);
                   4882:     if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838     albertel 4883:     my $uhost=&hostname($uhome);
1.800     albertel 4884:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240     www      4885:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
                   4886:                        $uhome);
1.479     albertel 4887:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242     www      4888:     return get_query_reply($queryid);
                   4889: }
                   4890: 
1.818     raeburn  4891: # -------------------------- Update MySQL table for portfolio file
                   4892: 
                   4893: sub update_portfolio_table {
1.821     raeburn  4894:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.818     raeburn  4895:     my $homeserver = &homeserver($uname,$udom);
                   4896:     my $queryid=
1.821     raeburn  4897:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
                   4898:                ':'.&escape($file_name).':'.$action,$homeserver);
1.818     raeburn  4899:     my $reply = &get_query_reply($queryid);
                   4900:     return $reply;
                   4901: }
                   4902: 
1.899     raeburn  4903: # -------------------------- Update MySQL allusers table
                   4904: 
                   4905: sub update_allusers_table {
                   4906:     my ($uname,$udom,$names) = @_;
                   4907:     my $homeserver = &homeserver($uname,$udom);
                   4908:     my $queryid=
                   4909:         &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
                   4910:                'lastname='.&escape($names->{'lastname'}).'%%'.
                   4911:                'firstname='.&escape($names->{'firstname'}).'%%'.
                   4912:                'middlename='.&escape($names->{'middlename'}).'%%'.
                   4913:                'generation='.&escape($names->{'generation'}).'%%'.
                   4914:                'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
                   4915:                'id='.&escape($names->{'id'}),$homeserver);
                   4916:     my $reply = &get_query_reply($queryid);
                   4917:     return $reply;
                   4918: }
                   4919: 
1.508     raeburn  4920: # ------- Request retrieval of institutional classlists for course(s)
1.506     raeburn  4921: 
                   4922: sub fetch_enrollment_query {
1.511     raeburn  4923:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.508     raeburn  4924:     my $homeserver;
1.547     raeburn  4925:     my $maxtries = 1;
1.508     raeburn  4926:     if ($context eq 'automated') {
                   4927:         $homeserver = $perlvar{'lonHostID'};
1.547     raeburn  4928:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508     raeburn  4929:     } else {
                   4930:         $homeserver = &homeserver($cnum,$dom);
                   4931:     }
1.838     albertel 4932:     my $host=&hostname($homeserver);
1.506     raeburn  4933:     my $cmd = '';
1.800     albertel 4934:     foreach my $affiliate (keys %{$affiliatesref}) {
                   4935:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506     raeburn  4936:     }
                   4937:     $cmd =~ s/%%$//;
                   4938:     $cmd = &escape($cmd);
                   4939:     my $query = 'fetchenrollment';
1.620     albertel 4940:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526     raeburn  4941:     unless ($queryid=~/^\Q$host\E\_/) { 
                   4942:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
                   4943:         return 'error: '.$queryid;
                   4944:     }
1.506     raeburn  4945:     my $reply = &get_query_reply($queryid);
1.547     raeburn  4946:     my $tries = 1;
                   4947:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   4948:         $reply = &get_query_reply($queryid);
                   4949:         $tries ++;
                   4950:     }
1.526     raeburn  4951:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620     albertel 4952:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526     raeburn  4953:     } else {
1.901     albertel 4954:         my @responses = split(/:/,$reply);
1.515     raeburn  4955:         if ($homeserver eq $perlvar{'lonHostID'}) {
1.800     albertel 4956:             foreach my $line (@responses) {
                   4957:                 my ($key,$value) = split(/=/,$line,2);
1.515     raeburn  4958:                 $$replyref{$key} = $value;
                   4959:             }
                   4960:         } else {
1.506     raeburn  4961:             my $pathname = $perlvar{'lonDaemons'}.'/tmp';
1.800     albertel 4962:             foreach my $line (@responses) {
                   4963:                 my ($key,$value) = split(/=/,$line);
1.506     raeburn  4964:                 $$replyref{$key} = $value;
                   4965:                 if ($value > 0) {
1.800     albertel 4966:                     foreach my $item (@{$$affiliatesref{$key}}) {
                   4967:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506     raeburn  4968:                         my $destname = $pathname.'/'.$filename;
                   4969:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526     raeburn  4970:                         if ($xml_classlist =~ /^error/) {
                   4971:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
                   4972:                         } else {
1.506     raeburn  4973:                             if ( open(FILE,">$destname") ) {
                   4974:                                 print FILE &unescape($xml_classlist);
                   4975:                                 close(FILE);
1.526     raeburn  4976:                             } else {
                   4977:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506     raeburn  4978:                             }
                   4979:                         }
                   4980:                     }
                   4981:                 }
                   4982:             }
                   4983:         }
                   4984:         return 'ok';
                   4985:     }
                   4986:     return 'error';
                   4987: }
                   4988: 
1.242     www      4989: sub get_query_reply {
                   4990:     my $queryid=shift;
1.240     www      4991:     my $replyfile=$perlvar{'lonDaemons'}.'/tmp/'.$queryid;
                   4992:     my $reply='';
                   4993:     for (1..100) {
                   4994: 	sleep 2;
                   4995:         if (-e $replyfile.'.end') {
1.448     albertel 4996: 	    if (open(my $fh,$replyfile)) {
1.904     albertel 4997: 		$reply = join('',<$fh>);
                   4998: 		close($fh);
1.240     www      4999: 	   } else { return 'error: reply_file_error'; }
1.242     www      5000:            return &unescape($reply);
                   5001: 	}
1.240     www      5002:     }
1.242     www      5003:     return 'timeout:'.$queryid;
1.240     www      5004: }
                   5005: 
                   5006: sub courselog_query {
1.241     www      5007: #
                   5008: # possible filters:
                   5009: # url: url or symb
                   5010: # username
                   5011: # domain
                   5012: # action: view, submit, grade
                   5013: # start: timestamp
                   5014: # end: timestamp
                   5015: #
1.240     www      5016:     my (%filters)=@_;
1.620     albertel 5017:     unless ($env{'request.course.id'}) { return 'no_course'; }
1.241     www      5018:     if ($filters{'url'}) {
                   5019: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
                   5020:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
                   5021:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
                   5022:     }
1.620     albertel 5023:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   5024:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240     www      5025:     return &log_query($cname,$cdom,'courselog',%filters);
                   5026: }
                   5027: 
                   5028: sub userlog_query {
1.858     raeburn  5029: #
                   5030: # possible filters:
                   5031: # action: log check role
                   5032: # start: timestamp
                   5033: # end: timestamp
                   5034: #
1.240     www      5035:     my ($uname,$udom,%filters)=@_;
                   5036:     return &log_query($uname,$udom,'userlog',%filters);
1.12      www      5037: }
                   5038: 
1.506     raeburn  5039: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
                   5040: 
                   5041: sub auto_run {
1.508     raeburn  5042:     my ($cnum,$cdom) = @_;
1.876     raeburn  5043:     my $response = 0;
                   5044:     my $settings;
                   5045:     my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
                   5046:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
                   5047:         $settings = $domconfig{'autoenroll'};
                   5048:         if ($settings->{'run'} eq '1') {
                   5049:             $response = 1;
                   5050:         }
                   5051:     } else {
1.934     raeburn  5052:         my $homeserver;
                   5053:         if (&is_course($cdom,$cnum)) {
                   5054:             $homeserver = &homeserver($cnum,$cdom);
                   5055:         } else {
                   5056:             $homeserver = &domain($cdom,'primary');
                   5057:         }
                   5058:         if ($homeserver ne 'no_host') {
                   5059:             $response = &reply('autorun:'.$cdom,$homeserver);
                   5060:         }
1.876     raeburn  5061:     }
1.506     raeburn  5062:     return $response;
                   5063: }
1.776     albertel 5064: 
1.506     raeburn  5065: sub auto_get_sections {
1.508     raeburn  5066:     my ($cnum,$cdom,$inst_coursecode) = @_;
                   5067:     my $homeserver = &homeserver($cnum,$cdom);
1.506     raeburn  5068:     my @secs = ();
1.511     raeburn  5069:     my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
1.506     raeburn  5070:     unless ($response eq 'refused') {
1.901     albertel 5071:         @secs = split(/:/,$response);
1.506     raeburn  5072:     }
                   5073:     return @secs;
                   5074: }
1.776     albertel 5075: 
1.506     raeburn  5076: sub auto_new_course {
1.508     raeburn  5077:     my ($cnum,$cdom,$inst_course_id,$owner) = @_;
                   5078:     my $homeserver = &homeserver($cnum,$cdom);
1.515     raeburn  5079:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.$owner.':'.$cdom,$homeserver));
1.506     raeburn  5080:     return $response;
                   5081: }
1.776     albertel 5082: 
1.506     raeburn  5083: sub auto_validate_courseID {
1.508     raeburn  5084:     my ($cnum,$cdom,$inst_course_id) = @_;
                   5085:     my $homeserver = &homeserver($cnum,$cdom);
1.511     raeburn  5086:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506     raeburn  5087:     return $response;
                   5088: }
1.776     albertel 5089: 
1.506     raeburn  5090: sub auto_create_password {
1.873     raeburn  5091:     my ($cnum,$cdom,$authparam,$udom) = @_;
                   5092:     my ($homeserver,$response);
1.506     raeburn  5093:     my $create_passwd = 0;
                   5094:     my $authchk = '';
1.873     raeburn  5095:     if ($udom =~ /^$match_domain$/) {
                   5096:         $homeserver = &domain($udom,'primary');
                   5097:     }
                   5098:     if ($homeserver eq '') {
                   5099:         if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
                   5100:             $homeserver = &homeserver($cnum,$cdom);
                   5101:         }
                   5102:     }
                   5103:     if ($homeserver eq '') {
                   5104:         $authchk = 'nodomain';
1.506     raeburn  5105:     } else {
1.873     raeburn  5106:         $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
                   5107:         if ($response eq 'refused') {
                   5108:             $authchk = 'refused';
                   5109:         } else {
1.901     albertel 5110:             ($authparam,$create_passwd,$authchk) = split(/:/,$response);
1.873     raeburn  5111:         }
1.506     raeburn  5112:     }
                   5113:     return ($authparam,$create_passwd,$authchk);
                   5114: }
                   5115: 
1.706     raeburn  5116: sub auto_photo_permission {
                   5117:     my ($cnum,$cdom,$students) = @_;
                   5118:     my $homeserver = &homeserver($cnum,$cdom);
1.707     albertel 5119:     my ($outcome,$perm_reqd,$conditions) = 
                   5120: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709     albertel 5121:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   5122: 	return (undef,undef);
                   5123:     }
1.706     raeburn  5124:     return ($outcome,$perm_reqd,$conditions);
                   5125: }
                   5126: 
                   5127: sub auto_checkphotos {
                   5128:     my ($uname,$udom,$pid) = @_;
                   5129:     my $homeserver = &homeserver($uname,$udom);
                   5130:     my ($result,$resulttype);
                   5131:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707     albertel 5132: 				   &escape($uname).':'.&escape($pid),
                   5133: 				   $homeserver));
1.709     albertel 5134:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   5135: 	return (undef,undef);
                   5136:     }
1.706     raeburn  5137:     if ($outcome) {
                   5138:         ($result,$resulttype) = split(/:/,$outcome);
                   5139:     } 
                   5140:     return ($result,$resulttype);
                   5141: }
                   5142: 
                   5143: sub auto_photochoice {
                   5144:     my ($cnum,$cdom) = @_;
                   5145:     my $homeserver = &homeserver($cnum,$cdom);
                   5146:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707     albertel 5147: 						       &escape($cdom),
                   5148: 						       $homeserver)));
1.709     albertel 5149:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   5150: 	return (undef,undef);
                   5151:     }
1.706     raeburn  5152:     return ($update,$comment);
                   5153: }
                   5154: 
                   5155: sub auto_photoupdate {
                   5156:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
                   5157:     my $homeserver = &homeserver($cnum,$dom);
1.838     albertel 5158:     my $host=&hostname($homeserver);
1.706     raeburn  5159:     my $cmd = '';
                   5160:     my $maxtries = 1;
1.800     albertel 5161:     foreach my $affiliate (keys(%{$affiliatesref})) {
                   5162:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706     raeburn  5163:     }
                   5164:     $cmd =~ s/%%$//;
                   5165:     $cmd = &escape($cmd);
                   5166:     my $query = 'institutionalphotos';
                   5167:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
                   5168:     unless ($queryid=~/^\Q$host\E\_/) {
                   5169:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
                   5170:         return 'error: '.$queryid;
                   5171:     }
                   5172:     my $reply = &get_query_reply($queryid);
                   5173:     my $tries = 1;
                   5174:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   5175:         $reply = &get_query_reply($queryid);
                   5176:         $tries ++;
                   5177:     }
                   5178:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   5179:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
                   5180:     } else {
                   5181:         my @responses = split(/:/,$reply);
                   5182:         my $outcome = shift(@responses); 
                   5183:         foreach my $item (@responses) {
                   5184:             my ($key,$value) = split(/=/,$item);
                   5185:             $$photo{$key} = $value;
                   5186:         }
                   5187:         return $outcome;
                   5188:     }
                   5189:     return 'error';
                   5190: }
                   5191: 
1.521     raeburn  5192: sub auto_instcode_format {
1.793     albertel 5193:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
                   5194: 	$cat_order) = @_;
1.521     raeburn  5195:     my $courses = '';
1.772     raeburn  5196:     my @homeservers;
1.521     raeburn  5197:     if ($caller eq 'global') {
1.841     albertel 5198: 	my %servers = &get_servers($codedom,'library');
                   5199: 	foreach my $tryserver (keys(%servers)) {
                   5200: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   5201: 		push(@homeservers,$tryserver);
                   5202: 	    }
1.584     raeburn  5203:         }
1.521     raeburn  5204:     } else {
1.772     raeburn  5205:         push(@homeservers,&homeserver($caller,$codedom));
1.521     raeburn  5206:     }
1.793     albertel 5207:     foreach my $code (keys(%{$instcodes})) {
                   5208:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521     raeburn  5209:     }
                   5210:     chop($courses);
1.772     raeburn  5211:     my $ok_response = 0;
                   5212:     my $response;
                   5213:     while (@homeservers > 0 && $ok_response == 0) {
                   5214:         my $server = shift(@homeservers); 
                   5215:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
                   5216:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
                   5217:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
1.901     albertel 5218: 		split(/:/,$response);
1.772     raeburn  5219:             %{$codes} = (%{$codes},&str2hash($codes_str));
                   5220:             push(@{$codetitles},&str2array($codetitles_str));
                   5221:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
                   5222:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
                   5223:             $ok_response = 1;
                   5224:         }
                   5225:     }
                   5226:     if ($ok_response) {
1.521     raeburn  5227:         return 'ok';
1.772     raeburn  5228:     } else {
                   5229:         return $response;
1.521     raeburn  5230:     }
                   5231: }
                   5232: 
1.792     raeburn  5233: sub auto_instcode_defaults {
                   5234:     my ($domain,$returnhash,$code_order) = @_;
                   5235:     my @homeservers;
1.841     albertel 5236: 
                   5237:     my %servers = &get_servers($domain,'library');
                   5238:     foreach my $tryserver (keys(%servers)) {
                   5239: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   5240: 	    push(@homeservers,$tryserver);
                   5241: 	}
1.792     raeburn  5242:     }
1.841     albertel 5243: 
1.792     raeburn  5244:     my $response;
1.841     albertel 5245:     foreach my $server (@homeservers) {
1.792     raeburn  5246:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841     albertel 5247:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
                   5248: 	
                   5249: 	foreach my $pair (split(/\&/,$response)) {
                   5250: 	    my ($name,$value)=split(/\=/,$pair);
                   5251: 	    if ($name eq 'code_order') {
                   5252: 		@{$code_order} = split(/\&/,&unescape($value));
                   5253: 	    } else {
                   5254: 		$returnhash->{&unescape($name)}=&unescape($value);
                   5255: 	    }
                   5256: 	}
                   5257: 	return 'ok';
1.792     raeburn  5258:     }
1.841     albertel 5259: 
                   5260:     return $response;
1.792     raeburn  5261: } 
                   5262: 
1.777     albertel 5263: sub auto_validate_class_sec {
1.918     raeburn  5264:     my ($cdom,$cnum,$owners,$inst_class) = @_;
1.773     raeburn  5265:     my $homeserver = &homeserver($cnum,$cdom);
1.918     raeburn  5266:     my $ownerlist;
                   5267:     if (ref($owners) eq 'ARRAY') {
                   5268:         $ownerlist = join(',',@{$owners});
                   5269:     } else {
                   5270:         $ownerlist = $owners;
                   5271:     }
1.773     raeburn  5272:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.918     raeburn  5273:                         &escape($ownerlist).':'.$cdom,$homeserver);
1.773     raeburn  5274:     return $response;
                   5275: }
                   5276: 
1.679     raeburn  5277: # ------------------------------------------------------- Course Group routines
                   5278: 
                   5279: sub get_coursegroups {
1.809     raeburn  5280:     my ($cdom,$cnum,$group,$namespace) = @_;
                   5281:     return(&dump($namespace,$cdom,$cnum,$group));
1.805     raeburn  5282: }
                   5283: 
1.679     raeburn  5284: sub modify_coursegroup {
                   5285:     my ($cdom,$cnum,$groupsettings) = @_;
                   5286:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
                   5287: }
                   5288: 
1.809     raeburn  5289: sub toggle_coursegroup_status {
                   5290:     my ($cdom,$cnum,$group,$action) = @_;
                   5291:     my ($from_namespace,$to_namespace);
                   5292:     if ($action eq 'delete') {
                   5293:         $from_namespace = 'coursegroups';
                   5294:         $to_namespace = 'deleted_groups';
                   5295:     } else {
                   5296:         $from_namespace = 'deleted_groups';
                   5297:         $to_namespace = 'coursegroups';
                   5298:     }
                   5299:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805     raeburn  5300:     if (my $tmp = &error(%curr_group)) {
                   5301:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
                   5302:         return ('read error',$tmp);
                   5303:     } else {
                   5304:         my %savedsettings = %curr_group; 
1.809     raeburn  5305:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805     raeburn  5306:         my $deloutcome;
                   5307:         if ($result eq 'ok') {
1.809     raeburn  5308:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805     raeburn  5309:         } else {
                   5310:             return ('write error',$result);
                   5311:         }
                   5312:         if ($deloutcome eq 'ok') {
                   5313:             return 'ok';
                   5314:         } else {
                   5315:             return ('delete error',$deloutcome);
                   5316:         }
                   5317:     }
                   5318: }
                   5319: 
1.679     raeburn  5320: sub modify_group_roles {
1.957     raeburn  5321:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
1.679     raeburn  5322:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
                   5323:     my $role = 'gr/'.&escape($userprivs);
                   5324:     my ($uname,$udom) = split(/:/,$user);
1.957     raeburn  5325:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
1.684     raeburn  5326:     if ($result eq 'ok') {
                   5327:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
                   5328:     }
1.679     raeburn  5329:     return $result;
                   5330: }
                   5331: 
                   5332: sub modify_coursegroup_membership {
                   5333:     my ($cdom,$cnum,$membership) = @_;
                   5334:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
                   5335:     return $result;
                   5336: }
                   5337: 
1.682     raeburn  5338: sub get_active_groups {
                   5339:     my ($udom,$uname,$cdom,$cnum) = @_;
                   5340:     my $now = time;
                   5341:     my %groups = ();
                   5342:     foreach my $key (keys(%env)) {
1.811     albertel 5343:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682     raeburn  5344:             my ($start,$end) = split(/\./,$env{$key});
                   5345:             if (($end!=0) && ($end<$now)) { next; }
                   5346:             if (($start!=0) && ($start>$now)) { next; }
                   5347:             if ($1 eq $cdom && $2 eq $cnum) {
                   5348:                 $groups{$3} = $env{$key} ;
                   5349:             }
                   5350:         }
                   5351:     }
                   5352:     return %groups;
                   5353: }
                   5354: 
1.683     raeburn  5355: sub get_group_membership {
                   5356:     my ($cdom,$cnum,$group) = @_;
                   5357:     return(&dump('groupmembership',$cdom,$cnum,$group));
                   5358: }
                   5359: 
                   5360: sub get_users_groups {
                   5361:     my ($udom,$uname,$courseid) = @_;
1.733     raeburn  5362:     my @usersgroups;
1.683     raeburn  5363:     my $cachetime=1800;
                   5364: 
                   5365:     my $hashid="$udom:$uname:$courseid";
1.733     raeburn  5366:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
                   5367:     if (defined($cached)) {
1.734     albertel 5368:         @usersgroups = split(/:/,$grouplist);
1.733     raeburn  5369:     } else {  
                   5370:         $grouplist = '';
1.816     raeburn  5371:         my $courseurl = &courseid_to_courseurl($courseid);
                   5372:         my %roleshash = &dump('roles',$udom,$uname,$courseurl);
1.817     raeburn  5373:         my $access_end = $env{'course.'.$courseid.
                   5374:                               '.default_enrollment_end_date'};
                   5375:         my $now = time;
                   5376:         foreach my $key (keys(%roleshash)) {
                   5377:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
                   5378:                 my $group = $1;
                   5379:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
                   5380:                     my $start = $2;
                   5381:                     my $end = $1;
                   5382:                     if ($start == -1) { next; } # deleted from group
                   5383:                     if (($start!=0) && ($start>$now)) { next; }
                   5384:                     if (($end!=0) && ($end<$now)) {
                   5385:                         if ($access_end && $access_end < $now) {
                   5386:                             if ($access_end - $end < 86400) {
                   5387:                                 push(@usersgroups,$group);
1.733     raeburn  5388:                             }
                   5389:                         }
1.817     raeburn  5390:                         next;
1.733     raeburn  5391:                     }
1.817     raeburn  5392:                     push(@usersgroups,$group);
1.683     raeburn  5393:                 }
                   5394:             }
                   5395:         }
1.817     raeburn  5396:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
                   5397:         $grouplist = join(':',@usersgroups);
                   5398:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683     raeburn  5399:     }
1.733     raeburn  5400:     return @usersgroups;
1.683     raeburn  5401: }
                   5402: 
                   5403: sub devalidate_getgroups_cache {
                   5404:     my ($udom,$uname,$cdom,$cnum)=@_;
                   5405:     my $courseid = $cdom.'_'.$cnum;
1.807     albertel 5406: 
1.683     raeburn  5407:     my $hashid="$udom:$uname:$courseid";
                   5408:     &devalidate_cache_new('getgroups',$hashid);
                   5409: }
                   5410: 
1.12      www      5411: # ------------------------------------------------------------------ Plain Text
                   5412: 
                   5413: sub plaintext {
1.742     raeburn  5414:     my ($short,$type,$cid) = @_;
1.758     albertel 5415:     if ($short =~ /^cr/) {
                   5416: 	return (split('/',$short))[-1];
                   5417:     }
1.742     raeburn  5418:     if (!defined($cid)) {
                   5419:         $cid = $env{'request.course.id'};
                   5420:     }
                   5421:     if (defined($cid) && defined($env{'course.'.$cid.'.'.$short.'.plaintext'})) {
                   5422:         return &Apache::lonlocal::mt($env{'course.'.$cid.'.'.$short.
                   5423:                                           '.plaintext'});
                   5424:     }
                   5425:     my %rolenames = (
                   5426:                       Course => 'std',
                   5427:                       Group => 'alt1',
                   5428:                     );
                   5429:     if (defined($type) && 
                   5430:          defined($rolenames{$type}) && 
                   5431:          defined($prp{$short}{$rolenames{$type}})) {
                   5432:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
                   5433:     } else {
                   5434:         return &Apache::lonlocal::mt($prp{$short}{'std'});
                   5435:     }
1.12      www      5436: }
                   5437: 
                   5438: # ----------------------------------------------------------------- Assign Role
                   5439: 
                   5440: sub assignrole {
1.957     raeburn  5441:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
                   5442:         $context)=@_;
1.21      www      5443:     my $mrole;
                   5444:     if ($role =~ /^cr\//) {
1.393     www      5445:         my $cwosec=$url;
1.811     albertel 5446:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.393     www      5447: 	unless (&allowed('ccr',$cwosec)) {
1.104     www      5448:            &logthis('Refused custom assignrole: '.
                   5449:              $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
1.620     albertel 5450: 		    $env{'user.name'}.' at '.$env{'user.domain'});
1.104     www      5451:            return 'refused'; 
                   5452:         }
1.21      www      5453:         $mrole='cr';
1.678     raeburn  5454:     } elsif ($role =~ /^gr\//) {
                   5455:         my $cwogrp=$url;
1.811     albertel 5456:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.678     raeburn  5457:         unless (&allowed('mdg',$cwogrp)) {
                   5458:             &logthis('Refused group assignrole: '.
                   5459:               $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
                   5460:                     $env{'user.name'}.' at '.$env{'user.domain'});
                   5461:             return 'refused';
                   5462:         }
                   5463:         $mrole='gr';
1.21      www      5464:     } else {
1.82      www      5465:         my $cwosec=$url;
1.811     albertel 5466:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.932     raeburn  5467:         if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
                   5468:             my $refused;
                   5469:             if (($env{'request.course.sec'}  ne '') && ($role eq 'st')) {
                   5470:                 if (!(&allowed('c'.$role,$url))) {
                   5471:                     $refused = 1;
                   5472:                 }
                   5473:             } else {
                   5474:                 $refused = 1;
                   5475:             }
1.947     raeburn  5476:             if ($refused) {
                   5477:                 if (($selfenroll == 1) && ($role eq 'st') && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
                   5478:                     $refused = '';
                   5479:                 } else {
                   5480:                     &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
                   5481:                              ' '.$role.' '.$end.' '.$start.' by '.
                   5482: 	  	             $env{'user.name'}.' at '.$env{'user.domain'});
                   5483:                     return 'refused';
                   5484:                 }
1.932     raeburn  5485:             }
1.104     www      5486:         }
1.21      www      5487:         $mrole=$role;
                   5488:     }
1.620     albertel 5489:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21      www      5490:                 "$udom:$uname:$url".'_'."$mrole=$role";
1.81      www      5491:     if ($end) { $command.='_'.$end; }
1.21      www      5492:     if ($start) {
                   5493: 	if ($end) { 
1.81      www      5494:            $command.='_'.$start; 
1.21      www      5495:         } else {
1.81      www      5496:            $command.='_0_'.$start;
1.21      www      5497:         }
                   5498:     }
1.739     raeburn  5499:     my $origstart = $start;
                   5500:     my $origend = $end;
1.957     raeburn  5501:     my $delflag;
1.357     www      5502: # actually delete
                   5503:     if ($deleteflag) {
1.373     www      5504: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357     www      5505: # modify command to delete the role
1.620     albertel 5506:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357     www      5507:                 "$udom:$uname:$url".'_'."$mrole";
1.620     albertel 5508: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
1.357     www      5509: # set start and finish to negative values for userrolelog
                   5510:            $start=-1;
                   5511:            $end=-1;
1.957     raeburn  5512:            $delflag = 1;
1.357     www      5513:         }
                   5514:     }
                   5515: # send command
1.349     www      5516:     my $answer=&reply($command,&homeserver($uname,$udom));
1.357     www      5517: # log new user role if status is ok
1.349     www      5518:     if ($answer eq 'ok') {
1.663     raeburn  5519: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
1.739     raeburn  5520: # for course roles, perform group memberships changes triggered by role change.
1.957     raeburn  5521:         &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,$selfenroll,$context);
1.739     raeburn  5522:         unless ($role =~ /^gr/) {
                   5523:             &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
1.957     raeburn  5524:                                              $origstart,$selfenroll,$context);
1.739     raeburn  5525:         }
1.349     www      5526:     }
                   5527:     return $answer;
1.169     harris41 5528: }
                   5529: 
                   5530: # -------------------------------------------------- Modify user authentication
1.197     www      5531: # Overrides without validation
                   5532: 
1.169     harris41 5533: sub modifyuserauth {
                   5534:     my ($udom,$uname,$umode,$upass)=@_;
                   5535:     my $uhome=&homeserver($uname,$udom);
1.197     www      5536:     unless (&allowed('mau',$udom)) { return 'refused'; }
                   5537:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620     albertel 5538:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   5539:              ' in domain '.$env{'request.role.domain'});  
1.169     harris41 5540:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
                   5541: 		     &escape($upass),$uhome);
1.620     albertel 5542:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197     www      5543:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
                   5544:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
                   5545:     &log($udom,,$uname,$uhome,
1.620     albertel 5546:         'Authentication changed by '.$env{'user.domain'}.', '.
                   5547:                                      $env{'user.name'}.', '.$umode.
1.197     www      5548:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
1.169     harris41 5549:     unless ($reply eq 'ok') {
1.197     www      5550:         &logthis('Authentication mode error: '.$reply);
1.169     harris41 5551: 	return 'error: '.$reply;
                   5552:     }   
1.170     harris41 5553:     return 'ok';
1.80      www      5554: }
                   5555: 
1.81      www      5556: # --------------------------------------------------------------- Modify a user
1.80      www      5557: 
1.81      www      5558: sub modifyuser {
1.206     matthew  5559:     my ($udom,    $uname, $uid,
                   5560:         $umode,   $upass, $first,
                   5561:         $middle,  $last,  $gene,
1.963     raeburn  5562:         $forceid, $desiredhome, $email, $inststatus)=@_;
1.807     albertel 5563:     $udom= &LONCAPA::clean_domain($udom);
                   5564:     $uname=&LONCAPA::clean_username($uname);
1.81      www      5565:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80      www      5566:              $umode.', '.$first.', '.$middle.', '.
1.206     matthew  5567: 	     $last.', '.$gene.'(forceid: '.$forceid.')'.
                   5568:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
                   5569:                                      ' desiredhome not specified'). 
1.620     albertel 5570:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   5571:              ' in domain '.$env{'request.role.domain'});
1.230     stredwic 5572:     my $uhome=&homeserver($uname,$udom,'true');
1.80      www      5573: # ----------------------------------------------------------------- Create User
1.406     albertel 5574:     if (($uhome eq 'no_host') && 
                   5575: 	(($umode && $upass) || ($umode eq 'localauth'))) {
1.80      www      5576:         my $unhome='';
1.844     albertel 5577:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
1.209     matthew  5578:             $unhome = $desiredhome;
1.620     albertel 5579: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
                   5580: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209     matthew  5581:         } else { # load balancing routine for determining $unhome
1.81      www      5582:             my $loadm=10000000;
1.841     albertel 5583: 	    my %servers = &get_servers($udom,'library');
                   5584: 	    foreach my $tryserver (keys(%servers)) {
                   5585: 		my $answer=reply('load',$tryserver);
                   5586: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
                   5587: 		    $loadm=$answer;
                   5588: 		    $unhome=$tryserver;
                   5589: 		}
1.80      www      5590: 	    }
                   5591:         }
                   5592:         if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206     matthew  5593: 	    return 'error: unable to find a home server for '.$uname.
                   5594:                    ' in domain '.$udom;
1.80      www      5595:         }
                   5596:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
                   5597:                          &escape($upass),$unhome);
                   5598: 	unless ($reply eq 'ok') {
                   5599:             return 'error: '.$reply;
                   5600:         }   
1.230     stredwic 5601:         $uhome=&homeserver($uname,$udom,'true');
1.80      www      5602:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386     matthew  5603: 	    return 'error: unable verify users home machine.';
1.80      www      5604:         }
1.209     matthew  5605:     }   # End of creation of new user
1.80      www      5606: # ---------------------------------------------------------------------- Add ID
                   5607:     if ($uid) {
                   5608:        $uid=~tr/A-Z/a-z/;
                   5609:        my %uidhash=&idrget($udom,$uname);
1.196     www      5610:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
                   5611:          && (!$forceid)) {
1.80      www      5612: 	  unless ($uid eq $uidhash{$uname}) {
1.386     matthew  5613: 	      return 'error: user id "'.$uid.'" does not match '.
                   5614:                   'current user id "'.$uidhash{$uname}.'".';
1.80      www      5615:           }
                   5616:        } else {
                   5617: 	  &idput($udom,($uname => $uid));
                   5618:        }
                   5619:     }
                   5620: # -------------------------------------------------------------- Add names, etc
1.313     matthew  5621:     my @tmp=&get('environment',
1.899     raeburn  5622: 		   ['firstname','middlename','lastname','generation','id',
1.963     raeburn  5623:                     'permanentemail','inststatus'],
1.134     albertel 5624: 		   $udom,$uname);
1.313     matthew  5625:     my %names;
                   5626:     if ($tmp[0] =~ m/^error:.*/) { 
                   5627:         %names=(); 
                   5628:     } else {
                   5629:         %names = @tmp;
                   5630:     }
1.388     www      5631: #
                   5632: # Make sure to not trash student environment if instructor does not bother
                   5633: # to supply name and email information
                   5634: #
                   5635:     if ($first)  { $names{'firstname'}  = $first; }
1.385     matthew  5636:     if (defined($middle)) { $names{'middlename'} = $middle; }
1.388     www      5637:     if ($last)   { $names{'lastname'}   = $last; }
1.385     matthew  5638:     if (defined($gene))   { $names{'generation'} = $gene; }
1.592     www      5639:     if ($email) {
                   5640:        $email=~s/[^\w\@\.\-\,]//gs;
1.963     raeburn  5641:        if ($email=~/\@/) { $names{'permanentemail'} = $email; }
1.592     www      5642:     }
1.899     raeburn  5643:     if ($uid) { $names{'id'}  = $uid; }
1.963     raeburn  5644:     if (defined($inststatus)) { $names{'inststatus'} = $inststatus; } 
1.134     albertel 5645:     my $reply = &put('environment', \%names, $udom,$uname);
                   5646:     if ($reply ne 'ok') { return 'error: '.$reply; }
1.899     raeburn  5647:     my $sqlresult = &update_allusers_table($uname,$udom,\%names);
1.680     www      5648:     &devalidate_cache_new('namescache',$uname.':'.$udom);
1.963     raeburn  5649:     my $logmsg = 'Success modifying user '.$udom.', '.$uname.', '.$uid.', '.
                   5650:                  $umode.', '.$first.', '.$middle.', '.
                   5651: 	         $last.', '.$gene.', '.$email.', '.$inststatus;
                   5652:     if ($env{'user.name'} ne '' && $env{'user.domain'}) {
                   5653:         $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
                   5654:     } else {
                   5655:         $logmsg .= ' during self creation';
                   5656:     }
                   5657:     &logthis($logmsg);
1.134     albertel 5658:     return 'ok';
1.80      www      5659: }
                   5660: 
1.81      www      5661: # -------------------------------------------------------------- Modify student
1.80      www      5662: 
1.81      www      5663: sub modifystudent {
                   5664:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.957     raeburn  5665:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
                   5666:         $selfenroll,$context)=@_;
1.455     albertel 5667:     if (!$cid) {
1.620     albertel 5668: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 5669: 	    return 'not_in_class';
                   5670: 	}
1.80      www      5671:     }
                   5672: # --------------------------------------------------------------- Make the user
1.81      www      5673:     my $reply=&modifyuser
1.209     matthew  5674: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.387     www      5675:          $desiredhome,$email);
1.80      www      5676:     unless ($reply eq 'ok') { return $reply; }
1.297     matthew  5677:     # This will cause &modify_student_enrollment to get the uid from the
                   5678:     # students environment
                   5679:     $uid = undef if (!$forceid);
1.455     albertel 5680:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.957     raeburn  5681: 					$gene,$usec,$end,$start,$type,$locktype,$cid,$selfenroll,$context);
1.297     matthew  5682:     return $reply;
                   5683: }
                   5684: 
                   5685: sub modify_student_enrollment {
1.957     raeburn  5686:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,$locktype,$cid,$selfenroll,$context) = @_;
1.455     albertel 5687:     my ($cdom,$cnum,$chome);
                   5688:     if (!$cid) {
1.620     albertel 5689: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 5690: 	    return 'not_in_class';
                   5691: 	}
1.620     albertel 5692: 	$cdom=$env{'course.'.$cid.'.domain'};
                   5693: 	$cnum=$env{'course.'.$cid.'.num'};
1.455     albertel 5694:     } else {
                   5695: 	($cdom,$cnum)=split(/_/,$cid);
                   5696:     }
1.620     albertel 5697:     $chome=$env{'course.'.$cid.'.home'};
1.455     albertel 5698:     if (!$chome) {
1.457     raeburn  5699: 	$chome=&homeserver($cnum,$cdom);
1.297     matthew  5700:     }
1.455     albertel 5701:     if (!$chome) { return 'unknown_course'; }
1.297     matthew  5702:     # Make sure the user exists
1.81      www      5703:     my $uhome=&homeserver($uname,$udom);
                   5704:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   5705: 	return 'error: no such user';
                   5706:     }
1.297     matthew  5707:     # Get student data if we were not given enough information
                   5708:     if (!defined($first)  || $first  eq '' || 
                   5709:         !defined($last)   || $last   eq '' || 
                   5710:         !defined($uid)    || $uid    eq '' || 
                   5711:         !defined($middle) || $middle eq '' || 
                   5712:         !defined($gene)   || $gene   eq '') {
1.294     matthew  5713:         # They did not supply us with enough data to enroll the student, so
                   5714:         # we need to pick up more information.
1.297     matthew  5715:         my %tmp = &get('environment',
1.294     matthew  5716:                        ['firstname','middlename','lastname', 'generation','id']
1.297     matthew  5717:                        ,$udom,$uname);
                   5718: 
1.800     albertel 5719:         #foreach my $key (keys(%tmp)) {
                   5720:         #    &logthis("key $key = ".$tmp{$key});
1.455     albertel 5721:         #}
1.294     matthew  5722:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
                   5723:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
                   5724:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
1.297     matthew  5725:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
1.294     matthew  5726:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
                   5727:     }
1.556     albertel 5728:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.487     albertel 5729:     my $reply=cput('classlist',
                   5730: 		   {"$uname:$udom" => 
1.515     raeburn  5731: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype) },
1.487     albertel 5732: 		   $cdom,$cnum);
1.81      www      5733:     unless (($reply eq 'ok') || ($reply eq 'delayed')) {
                   5734: 	return 'error: '.$reply;
1.652     albertel 5735:     } else {
                   5736: 	&devalidate_getsection_cache($udom,$uname,$cid);
1.81      www      5737:     }
1.297     matthew  5738:     # Add student role to user
1.83      www      5739:     my $uurl='/'.$cid;
1.81      www      5740:     $uurl=~s/\_/\//g;
                   5741:     if ($usec) {
                   5742: 	$uurl.='/'.$usec;
                   5743:     }
1.957     raeburn  5744:     return &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,$selfenroll,$context);
1.21      www      5745: }
                   5746: 
1.556     albertel 5747: sub format_name {
                   5748:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
                   5749:     my $name;
                   5750:     if ($first ne 'lastname') {
                   5751: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
                   5752:     } else {
                   5753: 	if ($lastname=~/\S/) {
                   5754: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
                   5755: 	    $name=~s/\s+,/,/;
                   5756: 	} else {
                   5757: 	    $name.= $firstname.' '.$middlename.' '.$generation;
                   5758: 	}
                   5759:     }
                   5760:     $name=~s/^\s+//;
                   5761:     $name=~s/\s+$//;
                   5762:     $name=~s/\s+/ /g;
                   5763:     return $name;
                   5764: }
                   5765: 
1.84      www      5766: # ------------------------------------------------- Write to course preferences
                   5767: 
                   5768: sub writecoursepref {
                   5769:     my ($courseid,%prefs)=@_;
                   5770:     $courseid=~s/^\///;
                   5771:     $courseid=~s/\_/\//g;
                   5772:     my ($cdomain,$cnum)=split(/\//,$courseid);
                   5773:     my $chome=homeserver($cnum,$cdomain);
                   5774:     if (($chome eq '') || ($chome eq 'no_host')) { 
                   5775: 	return 'error: no such course';
                   5776:     }
                   5777:     my $cstring='';
1.800     albertel 5778:     foreach my $pref (keys(%prefs)) {
                   5779: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191     harris41 5780:     }
1.84      www      5781:     $cstring=~s/\&$//;
                   5782:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
                   5783: }
                   5784: 
                   5785: # ---------------------------------------------------------- Make/modify course
                   5786: 
                   5787: sub createcourse {
1.741     raeburn  5788:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
                   5789:         $course_owner,$crstype)=@_;
1.84      www      5790:     $url=&declutter($url);
                   5791:     my $cid='';
1.264     matthew  5792:     unless (&allowed('ccc',$udom)) {
1.84      www      5793:         return 'refused';
                   5794:     }
                   5795: # ------------------------------------------------------------------- Create ID
1.674     www      5796:    my $uname=int(1+rand(9)).
                   5797:        ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
                   5798:        substr($$.time,0,5).unpack("H8",pack("I32",time)).
1.84      www      5799:        unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
                   5800: # ----------------------------------------------- Make sure that does not exist
1.230     stredwic 5801:    my $uhome=&homeserver($uname,$udom,'true');
1.84      www      5802:    unless (($uhome eq '') || ($uhome eq 'no_host')) {
                   5803:        $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   5804:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
1.230     stredwic 5805:        $uhome=&homeserver($uname,$udom,'true');       
1.84      www      5806:        unless (($uhome eq '') || ($uhome eq 'no_host')) {
                   5807:            return 'error: unable to generate unique course-ID';
                   5808:        } 
                   5809:    }
1.264     matthew  5810: # ------------------------------------------------ Check supplied server name
1.620     albertel 5811:     $course_server = $env{'user.homeserver'} if (! defined($course_server));
1.845     albertel 5812:     if (! &is_library($course_server)) {
1.264     matthew  5813:         return 'error:bad server name '.$course_server;
                   5814:     }
1.84      www      5815: # ------------------------------------------------------------- Make the course
                   5816:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264     matthew  5817:                       $course_server);
1.84      www      5818:     unless ($reply eq 'ok') { return 'error: '.$reply; }
1.230     stredwic 5819:     $uhome=&homeserver($uname,$udom,'true');
1.84      www      5820:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   5821: 	return 'error: no such course';
                   5822:     }
1.271     www      5823: # ----------------------------------------------------------------- Course made
1.516     raeburn  5824: # log existence
1.918     raeburn  5825:     my $newcourse = {
                   5826:                     $udom.'_'.$uname => {
1.921     raeburn  5827:                                      description => $description,
                   5828:                                      inst_code   => $inst_code,
                   5829:                                      owner       => $course_owner,
                   5830:                                      type        => $crstype,
1.918     raeburn  5831:                                                 },
                   5832:                     };
1.921     raeburn  5833:     &courseidput($udom,$newcourse,$uhome,'notime');
1.358     www      5834: # set toplevel url
1.271     www      5835:     my $topurl=$url;
                   5836:     unless ($nonstandard) {
                   5837: # ------------------------------------------ For standard courses, make top url
                   5838:         my $mapurl=&clutter($url);
1.278     www      5839:         if ($mapurl eq '/res/') { $mapurl=''; }
1.620     albertel 5840:         $env{'form.initmap'}=(<<ENDINITMAP);
1.271     www      5841: <map>
                   5842: <resource id="1" type="start"></resource>
                   5843: <resource id="2" src="$mapurl"></resource>
                   5844: <resource id="3" type="finish"></resource>
                   5845: <link index="1" from="1" to="2"></link>
                   5846: <link index="2" from="2" to="3"></link>
                   5847: </map>
                   5848: ENDINITMAP
                   5849:         $topurl=&declutter(
1.638     albertel 5850:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271     www      5851:                           );
                   5852:     }
                   5853: # ----------------------------------------------------------- Write preferences
1.84      www      5854:     &writecoursepref($udom.'_'.$uname,
                   5855:                      ('description' => $description,
1.271     www      5856:                       'url'         => $topurl));
1.84      www      5857:     return '/'.$udom.'/'.$uname;
                   5858: }
                   5859: 
1.813     albertel 5860: sub is_course {
                   5861:     my ($cdom,$cnum) = @_;
                   5862:     my %courses = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,
1.946     raeburn  5863: 				undef,'.');
1.813     albertel 5864:     if (exists($courses{$cdom.'_'.$cnum})) {
                   5865:         return 1;
                   5866:     }
                   5867:     return 0;
                   5868: }
                   5869: 
1.21      www      5870: # ---------------------------------------------------------- Assign Custom Role
                   5871: 
                   5872: sub assigncustomrole {
1.957     raeburn  5873:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
1.21      www      5874:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.957     raeburn  5875:                        $end,$start,$deleteflag,$selfenroll,$context);
1.21      www      5876: }
                   5877: 
                   5878: # ----------------------------------------------------------------- Revoke Role
                   5879: 
                   5880: sub revokerole {
1.957     raeburn  5881:     my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
1.21      www      5882:     my $now=time;
1.965     raeburn  5883:     return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
1.21      www      5884: }
                   5885: 
                   5886: # ---------------------------------------------------------- Revoke Custom Role
                   5887: 
                   5888: sub revokecustomrole {
1.957     raeburn  5889:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
1.21      www      5890:     my $now=time;
1.357     www      5891:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
1.957     raeburn  5892:            $deleteflag,$selfenroll,$context);
1.17      www      5893: }
                   5894: 
1.533     banghart 5895: # ------------------------------------------------------------ Disk usage
1.535     albertel 5896: sub diskusage {
1.955     raeburn  5897:     my ($udom,$uname,$directorypath,$getpropath)=@_;
                   5898:     $directorypath =~ s/\/$//;
                   5899:     my $listing=&reply('du2:'.&escape($directorypath).':'
                   5900:                        .&escape($getpropath).':'.&escape($uname).':'
                   5901:                        .&escape($udom),homeserver($uname,$udom));
                   5902:     if ($listing eq 'unknown_cmd') {
                   5903:         if ($getpropath) {
                   5904:             $directorypath = &propath($udom,$uname).'/'.$directorypath; 
                   5905:         }
                   5906:         $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
                   5907:     }
1.514     albertel 5908:     return $listing;
1.512     banghart 5909: }
                   5910: 
1.566     banghart 5911: sub is_locked {
                   5912:     my ($file_name, $domain, $user) = @_;
                   5913:     my @check;
                   5914:     my $is_locked;
                   5915:     push @check, $file_name;
1.613     albertel 5916:     my %locked = &get('file_permissions',\@check,
1.620     albertel 5917: 		      $env{'user.domain'},$env{'user.name'});
1.615     albertel 5918:     my ($tmp)=keys(%locked);
                   5919:     if ($tmp=~/^error:/) { undef(%locked); }
1.745     raeburn  5920:     
1.566     banghart 5921:     if (ref($locked{$file_name}) eq 'ARRAY') {
1.745     raeburn  5922:         $is_locked = 'false';
                   5923:         foreach my $entry (@{$locked{$file_name}}) {
                   5924:            if (ref($entry) eq 'ARRAY') { 
1.746     raeburn  5925:                $is_locked = 'true';
                   5926:                last;
1.745     raeburn  5927:            }
                   5928:        }
1.566     banghart 5929:     } else {
                   5930:         $is_locked = 'false';
                   5931:     }
                   5932: }
                   5933: 
1.759     albertel 5934: sub declutter_portfile {
                   5935:     my ($file) = @_;
1.833     albertel 5936:     $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759     albertel 5937:     return $file;
                   5938: }
                   5939: 
1.559     banghart 5940: # ------------------------------------------------------------- Mark as Read Only
                   5941: 
                   5942: sub mark_as_readonly {
                   5943:     my ($domain,$user,$files,$what) = @_;
1.613     albertel 5944:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 5945:     my ($tmp)=keys(%current_permissions);
                   5946:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560     banghart 5947:     foreach my $file (@{$files}) {
1.759     albertel 5948: 	$file = &declutter_portfile($file);
1.561     banghart 5949:         push(@{$current_permissions{$file}},$what);
1.559     banghart 5950:     }
1.613     albertel 5951:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 5952:     return;
                   5953: }
                   5954: 
1.572     banghart 5955: # ------------------------------------------------------------Save Selected Files
                   5956: 
                   5957: sub save_selected_files {
                   5958:     my ($user, $path, @files) = @_;
                   5959:     my $filename = $user."savedfiles";
1.573     banghart 5960:     my @other_files = &files_not_in_path($user, $path);
1.871     albertel 5961:     open (OUT, '>'.$tmpdir.$filename);
1.573     banghart 5962:     foreach my $file (@files) {
1.620     albertel 5963:         print (OUT $env{'form.currentpath'}.$file."\n");
1.573     banghart 5964:     }
                   5965:     foreach my $file (@other_files) {
1.574     banghart 5966:         print (OUT $file."\n");
1.572     banghart 5967:     }
1.574     banghart 5968:     close (OUT);
1.572     banghart 5969:     return 'ok';
                   5970: }
                   5971: 
1.574     banghart 5972: sub clear_selected_files {
                   5973:     my ($user) = @_;
                   5974:     my $filename = $user."savedfiles";
                   5975:     open (OUT, '>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
                   5976:     print (OUT undef);
                   5977:     close (OUT);
                   5978:     return ("ok");    
                   5979: }
                   5980: 
1.572     banghart 5981: sub files_in_path {
                   5982:     my ($user, $path) = @_;
                   5983:     my $filename = $user."savedfiles";
                   5984:     my %return_files;
1.574     banghart 5985:     open (IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
1.573     banghart 5986:     while (my $line_in = <IN>) {
1.574     banghart 5987:         chomp ($line_in);
                   5988:         my @paths_and_file = split (m!/!, $line_in);
                   5989:         my $file_part = pop (@paths_and_file);
                   5990:         my $path_part = join ('/', @paths_and_file);
1.573     banghart 5991:         $path_part.='/';
                   5992:         my $path_and_file = $path_part.$file_part;
                   5993:         if ($path_part eq $path) {
                   5994:             $return_files{$file_part}= 'selected';
                   5995:         }
                   5996:     }
1.574     banghart 5997:     close (IN);
                   5998:     return (\%return_files);
1.572     banghart 5999: }
                   6000: 
                   6001: # called in portfolio select mode, to show files selected NOT in current directory
                   6002: sub files_not_in_path {
                   6003:     my ($user, $path) = @_;
                   6004:     my $filename = $user."savedfiles";
                   6005:     my @return_files;
                   6006:     my $path_part;
1.800     albertel 6007:     open(IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
                   6008:     while (my $line = <IN>) {
1.572     banghart 6009:         #ok, I know it's clunky, but I want it to work
1.800     albertel 6010:         my @paths_and_file = split(m|/|, $line);
                   6011:         my $file_part = pop(@paths_and_file);
                   6012:         chomp($file_part);
                   6013:         my $path_part = join('/', @paths_and_file);
1.572     banghart 6014:         $path_part .= '/';
                   6015:         my $path_and_file = $path_part.$file_part;
                   6016:         if ($path_part ne $path) {
1.800     albertel 6017:             push(@return_files, ($path_and_file));
1.572     banghart 6018:         }
                   6019:     }
1.800     albertel 6020:     close(OUT);
1.574     banghart 6021:     return (@return_files);
1.572     banghart 6022: }
                   6023: 
1.745     raeburn  6024: #----------------------------------------------Get portfolio file permissions
1.629     banghart 6025: 
1.745     raeburn  6026: sub get_portfile_permissions {
                   6027:     my ($domain,$user) = @_;
1.613     albertel 6028:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 6029:     my ($tmp)=keys(%current_permissions);
                   6030:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  6031:     return \%current_permissions;
                   6032: }
                   6033: 
                   6034: #---------------------------------------------Get portfolio file access controls
                   6035: 
1.749     raeburn  6036: sub get_access_controls {
1.745     raeburn  6037:     my ($current_permissions,$group,$file) = @_;
1.769     albertel 6038:     my %access;
                   6039:     my $real_file = $file;
                   6040:     $file =~ s/\.meta$//;
1.745     raeburn  6041:     if (defined($file)) {
1.749     raeburn  6042:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
                   6043:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769     albertel 6044:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749     raeburn  6045:             }
                   6046:         }
1.745     raeburn  6047:     } else {
1.749     raeburn  6048:         foreach my $key (keys(%{$current_permissions})) {
                   6049:             if ($key =~ /\0accesscontrol$/) {
                   6050:                 if (defined($group)) {
                   6051:                     if ($key !~ m-^\Q$group\E/-) {
                   6052:                         next;
                   6053:                     }
                   6054:                 }
                   6055:                 my ($fullpath) = split(/\0/,$key);
                   6056:                 if (ref($$current_permissions{$key}) eq 'HASH') {
                   6057:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
                   6058:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
                   6059:                     }
                   6060:                 }
                   6061:             }
                   6062:         }
                   6063:     }
                   6064:     return %access;
                   6065: }
                   6066: 
                   6067: sub modify_access_controls {
                   6068:     my ($file_name,$changes,$domain,$user)=@_;
                   6069:     my ($outcome,$deloutcome);
                   6070:     my %store_permissions;
                   6071:     my %new_values;
                   6072:     my %new_control;
                   6073:     my %translation;
                   6074:     my @deletions = ();
                   6075:     my $now = time;
                   6076:     if (exists($$changes{'activate'})) {
                   6077:         if (ref($$changes{'activate'}) eq 'HASH') {
                   6078:             my @newitems = sort(keys(%{$$changes{'activate'}}));
                   6079:             my $numnew = scalar(@newitems);
                   6080:             for (my $i=0; $i<$numnew; $i++) {
                   6081:                 my $newkey = $newitems[$i];
                   6082:                 my $newid = &Apache::loncommon::get_cgi_id();
1.797     raeburn  6083:                 if ($newkey =~ /^\d+:/) { 
                   6084:                     $newkey =~ s/^(\d+)/$newid/;
                   6085:                     $translation{$1} = $newid;
                   6086:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
                   6087:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
                   6088:                     $translation{$1} = $newid;
                   6089:                 }
1.749     raeburn  6090:                 $new_values{$file_name."\0".$newkey} = 
                   6091:                                           $$changes{'activate'}{$newitems[$i]};
                   6092:                 $new_control{$newkey} = $now;
                   6093:             }
                   6094:         }
                   6095:     }
                   6096:     my %todelete;
                   6097:     my %changed_items;
                   6098:     foreach my $action ('delete','update') {
                   6099:         if (exists($$changes{$action})) {
                   6100:             if (ref($$changes{$action}) eq 'HASH') {
                   6101:                 foreach my $key (keys(%{$$changes{$action}})) {
                   6102:                     my ($itemnum) = ($key =~ /^([^:]+):/);
                   6103:                     if ($action eq 'delete') { 
                   6104:                         $todelete{$itemnum} = 1;
                   6105:                     } else {
                   6106:                         $changed_items{$itemnum} = $key;
                   6107:                     }
                   6108:                 }
1.745     raeburn  6109:             }
                   6110:         }
1.749     raeburn  6111:     }
                   6112:     # get lock on access controls for file.
                   6113:     my $lockhash = {
                   6114:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
                   6115:                                                        ':'.$env{'user.domain'},
                   6116:                    }; 
                   6117:     my $tries = 0;
                   6118:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   6119:    
                   6120:     while (($gotlock ne 'ok') && $tries <3) {
                   6121:         $tries ++;
                   6122:         sleep 1;
                   6123:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   6124:     }
                   6125:     if ($gotlock eq 'ok') {
                   6126:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
                   6127:         my ($tmp)=keys(%curr_permissions);
                   6128:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
                   6129:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
                   6130:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
                   6131:             if (ref($curr_controls) eq 'HASH') {
                   6132:                 foreach my $control_item (keys(%{$curr_controls})) {
                   6133:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
                   6134:                     if (defined($todelete{$itemnum})) {
                   6135:                         push(@deletions,$file_name."\0".$control_item);
                   6136:                     } else {
                   6137:                         if (defined($changed_items{$itemnum})) {
                   6138:                             $new_control{$changed_items{$itemnum}} = $now;
                   6139:                             push(@deletions,$file_name."\0".$control_item);
                   6140:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
                   6141:                         } else {
                   6142:                             $new_control{$control_item} = $$curr_controls{$control_item};
                   6143:                         }
                   6144:                     }
1.745     raeburn  6145:                 }
                   6146:             }
                   6147:         }
1.749     raeburn  6148:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
                   6149:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
                   6150:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
                   6151:         #  remove lock
                   6152:         my @del_lock = ($file_name."\0".'locked_access_records');
                   6153:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818     raeburn  6154:         my ($file,$group);
                   6155:         if (&is_course($domain,$user)) {
                   6156:             ($group,$file) = split(/\//,$file_name,2);
                   6157:         } else {
                   6158:             $file = $file_name;
                   6159:         }
                   6160:         my $sqlresult =
                   6161:             &update_portfolio_table($user,$domain,$file,'portfolio_access',
                   6162:                                     $group);
1.749     raeburn  6163:     } else {
                   6164:         $outcome = "error: could not obtain lockfile\n";  
1.745     raeburn  6165:     }
1.749     raeburn  6166:     return ($outcome,$deloutcome,\%new_values,\%translation);
1.745     raeburn  6167: }
                   6168: 
1.827     raeburn  6169: sub make_public_indefinitely {
                   6170:     my ($requrl) = @_;
                   6171:     my $now = time;
                   6172:     my $action = 'activate';
                   6173:     my $aclnum = 0;
                   6174:     if (&is_portfolio_url($requrl)) {
                   6175:         my (undef,$udom,$unum,$file_name,$group) =
                   6176:             &parse_portfolio_url($requrl);
                   6177:         my $current_perms = &get_portfile_permissions($udom,$unum);
                   6178:         my %access_controls = &get_access_controls($current_perms,
                   6179:                                                    $group,$file_name);
                   6180:         foreach my $key (keys(%{$access_controls{$file_name}})) {
                   6181:             my ($num,$scope,$end,$start) = 
                   6182:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                   6183:             if ($scope eq 'public') {
                   6184:                 if ($start <= $now && $end == 0) {
                   6185:                     $action = 'none';
                   6186:                 } else {
                   6187:                     $action = 'update';
                   6188:                     $aclnum = $num;
                   6189:                 }
                   6190:                 last;
                   6191:             }
                   6192:         }
                   6193:         if ($action eq 'none') {
                   6194:              return 'ok';
                   6195:         } else {
                   6196:             my %changes;
                   6197:             my $newend = 0;
                   6198:             my $newstart = $now;
                   6199:             my $newkey = $aclnum.':public_'.$newend.'_'.$newstart;
                   6200:             $changes{$action}{$newkey} = {
                   6201:                 type => 'public',
                   6202:                 time => {
                   6203:                     start => $newstart,
                   6204:                     end   => $newend,
                   6205:                 },
                   6206:             };
                   6207:             my ($outcome,$deloutcome,$new_values,$translation) =
                   6208:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
                   6209:             return $outcome;
                   6210:         }
                   6211:     } else {
                   6212:         return 'invalid';
                   6213:     }
                   6214: }
                   6215: 
1.745     raeburn  6216: #------------------------------------------------------Get Marked as Read Only
                   6217: 
                   6218: sub get_marked_as_readonly {
                   6219:     my ($domain,$user,$what,$group) = @_;
                   6220:     my $current_permissions = &get_portfile_permissions($domain,$user);
1.563     banghart 6221:     my @readonly_files;
1.629     banghart 6222:     my $cmp1=$what;
                   6223:     if (ref($what)) { $cmp1=join('',@{$what}) };
1.745     raeburn  6224:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   6225:         if (defined($group)) {
                   6226:             if ($file_name !~ m-^\Q$group\E/-) {
                   6227:                 next;
                   6228:             }
                   6229:         }
1.561     banghart 6230:         if (ref($value) eq "ARRAY"){
                   6231:             foreach my $stored_what (@{$value}) {
1.629     banghart 6232:                 my $cmp2=$stored_what;
1.759     albertel 6233:                 if (ref($stored_what) eq 'ARRAY') {
1.746     raeburn  6234:                     $cmp2=join('',@{$stored_what});
1.745     raeburn  6235:                 }
1.629     banghart 6236:                 if ($cmp1 eq $cmp2) {
1.561     banghart 6237:                     push(@readonly_files, $file_name);
1.745     raeburn  6238:                     last;
1.563     banghart 6239:                 } elsif (!defined($what)) {
                   6240:                     push(@readonly_files, $file_name);
1.745     raeburn  6241:                     last;
1.561     banghart 6242:                 }
                   6243:             }
1.745     raeburn  6244:         }
1.561     banghart 6245:     }
                   6246:     return @readonly_files;
                   6247: }
1.577     banghart 6248: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561     banghart 6249: 
1.577     banghart 6250: sub get_marked_as_readonly_hash {
1.745     raeburn  6251:     my ($current_permissions,$group,$what) = @_;
1.577     banghart 6252:     my %readonly_files;
1.745     raeburn  6253:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   6254:         if (defined($group)) {
                   6255:             if ($file_name !~ m-^\Q$group\E/-) {
                   6256:                 next;
                   6257:             }
                   6258:         }
1.577     banghart 6259:         if (ref($value) eq "ARRAY"){
                   6260:             foreach my $stored_what (@{$value}) {
1.745     raeburn  6261:                 if (ref($stored_what) eq 'ARRAY') {
1.750     banghart 6262:                     foreach my $lock_descriptor(@{$stored_what}) {
                   6263:                         if ($lock_descriptor eq 'graded') {
                   6264:                             $readonly_files{$file_name} = 'graded';
                   6265:                         } elsif ($lock_descriptor eq 'handback') {
                   6266:                             $readonly_files{$file_name} = 'handback';
                   6267:                         } else {
                   6268:                             if (!exists($readonly_files{$file_name})) {
                   6269:                                 $readonly_files{$file_name} = 'locked';
                   6270:                             }
                   6271:                         }
1.745     raeburn  6272:                     }
1.750     banghart 6273:                 } 
1.577     banghart 6274:             }
                   6275:         } 
                   6276:     }
                   6277:     return %readonly_files;
                   6278: }
1.559     banghart 6279: # ------------------------------------------------------------ Unmark as Read Only
                   6280: 
                   6281: sub unmark_as_readonly {
1.629     banghart 6282:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
                   6283:     # for portfolio submissions, $what contains [$symb,$crsid] 
1.745     raeburn  6284:     my ($domain,$user,$what,$file_name,$group) = @_;
1.759     albertel 6285:     $file_name = &declutter_portfile($file_name);
1.634     albertel 6286:     my $symb_crs = $what;
                   6287:     if (ref($what)) { $symb_crs=join('',@$what); }
1.745     raeburn  6288:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615     albertel 6289:     my ($tmp)=keys(%current_permissions);
                   6290:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  6291:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650     albertel 6292:     foreach my $file (@readonly_files) {
1.759     albertel 6293: 	my $clean_file = &declutter_portfile($file);
                   6294: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650     albertel 6295: 	my $current_locks = $current_permissions{$file};
1.563     banghart 6296:         my @new_locks;
                   6297:         my @del_keys;
                   6298:         if (ref($current_locks) eq "ARRAY"){
                   6299:             foreach my $locker (@{$current_locks}) {
1.632     albertel 6300:                 my $compare=$locker;
1.749     raeburn  6301:                 if (ref($locker) eq 'ARRAY') {
1.745     raeburn  6302:                     $compare=join('',@{$locker});
1.746     raeburn  6303:                     if ($compare ne $symb_crs) {
                   6304:                         push(@new_locks, $locker);
                   6305:                     }
1.563     banghart 6306:                 }
                   6307:             }
1.650     albertel 6308:             if (scalar(@new_locks) > 0) {
1.563     banghart 6309:                 $current_permissions{$file} = \@new_locks;
                   6310:             } else {
                   6311:                 push(@del_keys, $file);
1.613     albertel 6312:                 &del('file_permissions',\@del_keys, $domain, $user);
1.650     albertel 6313:                 delete($current_permissions{$file});
1.563     banghart 6314:             }
                   6315:         }
1.561     banghart 6316:     }
1.613     albertel 6317:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 6318:     return;
                   6319: }
1.512     banghart 6320: 
1.17      www      6321: # ------------------------------------------------------------ Directory lister
                   6322: 
                   6323: sub dirlist {
1.955     raeburn  6324:     my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
1.18      www      6325:     $uri=~s/^\///;
                   6326:     $uri=~s/\/$//;
1.253     stredwic 6327:     my ($udom, $uname);
1.955     raeburn  6328:     if ($getuserdir) {
1.253     stredwic 6329:         $udom = $userdomain;
                   6330:         $uname = $username;
1.955     raeburn  6331:     } else {
                   6332:         (undef,$udom,$uname)=split(/\//,$uri);
                   6333:         if(defined($userdomain)) {
                   6334:             $udom = $userdomain;
                   6335:         }
                   6336:         if(defined($username)) {
                   6337:             $uname = $username;
                   6338:         }
1.253     stredwic 6339:     }
1.955     raeburn  6340:     my ($dirRoot,$listing,@listing_results);
1.253     stredwic 6341: 
1.955     raeburn  6342:     $dirRoot = $perlvar{'lonDocRoot'};
                   6343:     if (defined($getpropath)) {
                   6344:         $dirRoot = &propath($udom,$uname);
1.253     stredwic 6345:         $dirRoot =~ s/\/$//;
1.955     raeburn  6346:     } elsif (defined($getuserdir)) {
                   6347:         my $subdir=$uname.'__';
                   6348:         $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
                   6349:         $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
                   6350:                    ."/$udom/$subdir/$uname";
                   6351:     } elsif (defined($alternateRoot)) {
                   6352:         $dirRoot = $alternateRoot;
1.751     banghart 6353:     }
1.253     stredwic 6354: 
                   6355:     if($udom) {
                   6356:         if($uname) {
1.955     raeburn  6357:             $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
1.956     raeburn  6358:                               .$getuserdir.':'.&escape($dirRoot)
1.955     raeburn  6359:                               .':'.&escape($uname).':'.&escape($udom),
                   6360:                               &homeserver($uname,$udom));
                   6361:             if ($listing eq 'unknown_cmd') {
                   6362:                 $listing = &reply('ls2:'.$dirRoot.'/'.$uri,
                   6363:                                   &homeserver($uname,$udom));
                   6364:             } else {
                   6365:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
                   6366:             }
1.605     matthew  6367:             if ($listing eq 'unknown_cmd') {
1.800     albertel 6368:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,
                   6369: 				  &homeserver($uname,$udom));
1.605     matthew  6370:                 @listing_results = split(/:/,$listing);
                   6371:             } else {
                   6372:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
                   6373:             }
                   6374:             return @listing_results;
1.955     raeburn  6375:         } elsif(!$alternateRoot) {
1.800     albertel 6376:             my %allusers;
1.841     albertel 6377: 	    my %servers = &get_servers($udom,'library');
1.955     raeburn  6378:  	    foreach my $tryserver (keys(%servers)) {
                   6379:                 $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
                   6380:                                   &escape($udom),$tryserver);
                   6381:                 if ($listing eq 'unknown_cmd') {
                   6382: 		    $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
                   6383: 				      $udom, $tryserver);
                   6384:                 } else {
                   6385:                     @listing_results = map { &unescape($_); } split(/:/,$listing);
                   6386:                 }
1.841     albertel 6387: 		if ($listing eq 'unknown_cmd') {
                   6388: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
                   6389: 				      $udom, $tryserver);
                   6390: 		    @listing_results = split(/:/,$listing);
                   6391: 		} else {
                   6392: 		    @listing_results =
                   6393: 			map { &unescape($_); } split(/:/,$listing);
                   6394: 		}
                   6395: 		if ($listing_results[0] ne 'no_such_dir' && 
                   6396: 		    $listing_results[0] ne 'empty'       &&
                   6397: 		    $listing_results[0] ne 'con_lost') {
                   6398: 		    foreach my $line (@listing_results) {
                   6399: 			my ($entry) = split(/&/,$line,2);
                   6400: 			$allusers{$entry} = 1;
                   6401: 		    }
                   6402: 		}
1.253     stredwic 6403:             }
                   6404:             my $alluserstr='';
1.800     albertel 6405:             foreach my $user (sort(keys(%allusers))) {
                   6406:                 $alluserstr.=$user.'&user:';
1.253     stredwic 6407:             }
                   6408:             $alluserstr=~s/:$//;
                   6409:             return split(/:/,$alluserstr);
                   6410:         } else {
1.800     albertel 6411:             return ('missing user name');
1.253     stredwic 6412:         }
1.955     raeburn  6413:     } elsif(!defined($getpropath)) {
1.841     albertel 6414:         my @all_domains = sort(&all_domains());
1.955     raeburn  6415:         foreach my $domain (@all_domains) {
                   6416:             $domain = $perlvar{'lonDocRoot'}.'/res/'.$domain.'/&domain';
                   6417:         }
                   6418:         return @all_domains;
                   6419:     } else {
1.800     albertel 6420:         return ('missing domain');
1.275     stredwic 6421:     }
                   6422: }
                   6423: 
                   6424: # --------------------------------------------- GetFileTimestamp
                   6425: # This function utilizes dirlist and returns the date stamp for
                   6426: # when it was last modified.  It will also return an error of -1
                   6427: # if an error occurs
                   6428: 
                   6429: sub GetFileTimestamp {
1.955     raeburn  6430:     my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
1.807     albertel 6431:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
                   6432:     $studentName   = &LONCAPA::clean_username($studentName);
1.955     raeburn  6433:     my ($fileStat) = 
                   6434:         &Apache::lonnet::dirlist($filename,$studentDomain,$studentName, 
                   6435:                                  undef,$getuserdir);
1.275     stredwic 6436:     my @stats = split('&', $fileStat);
                   6437:     if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
1.375     matthew  6438:         # @stats contains first the filename, then the stat output
                   6439:         return $stats[10]; # so this is 10 instead of 9.
1.275     stredwic 6440:     } else {
                   6441:         return -1;
1.253     stredwic 6442:     }
1.26      www      6443: }
                   6444: 
1.712     albertel 6445: sub stat_file {
                   6446:     my ($uri) = @_;
1.787     albertel 6447:     $uri = &clutter_with_no_wrapper($uri);
1.722     albertel 6448: 
1.955     raeburn  6449:     my ($udom,$uname,$file);
1.712     albertel 6450:     if ($uri =~ m-^/(uploaded|editupload)/-) {
                   6451: 	($udom,$uname,$file) =
1.811     albertel 6452: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712     albertel 6453: 	$file = 'userfiles/'.$file;
                   6454:     }
                   6455:     if ($uri =~ m-^/res/-) {
                   6456: 	($udom,$uname) = 
1.807     albertel 6457: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712     albertel 6458: 	$file = $uri;
                   6459:     }
                   6460: 
                   6461:     if (!$udom || !$uname || !$file) {
                   6462: 	# unable to handle the uri
                   6463: 	return ();
                   6464:     }
1.956     raeburn  6465:     my $getpropath;
                   6466:     if ($file =~ /^userfiles\//) {
                   6467:         $getpropath = 1;
                   6468:     }
1.955     raeburn  6469:     my ($result) = &dirlist($file,$udom,$uname,$getpropath);
1.712     albertel 6470:     my @stats = split('&', $result);
1.721     banghart 6471:     
1.712     albertel 6472:     if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
                   6473: 	shift(@stats); #filename is first
                   6474: 	return @stats;
                   6475:     }
                   6476:     return ();
                   6477: }
                   6478: 
1.26      www      6479: # -------------------------------------------------------- Value of a Condition
                   6480: 
1.713     albertel 6481: # gets the value of a specific preevaluated condition
                   6482: #    stored in the string  $env{user.state.<cid>}
                   6483: # or looks up a condition reference in the bighash and if if hasn't
                   6484: # already been evaluated recurses into docondval to get the value of
                   6485: # the condition, then memoizing it to 
                   6486: #   $env{user.state.<cid>.<condition>}
1.40      www      6487: sub directcondval {
                   6488:     my $number=shift;
1.620     albertel 6489:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555     albertel 6490: 	&Apache::lonuserstate::evalstate();
                   6491:     }
1.713     albertel 6492:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
                   6493: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
                   6494:     } elsif ($number =~ /^_/) {
                   6495: 	my $sub_condition;
                   6496: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
                   6497: 		&GDBM_READER(),0640)) {
                   6498: 	    $sub_condition=$bighash{'conditions'.$number};
                   6499: 	    untie(%bighash);
                   6500: 	}
                   6501: 	my $value = &docondval($sub_condition);
1.949     raeburn  6502: 	&appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
1.713     albertel 6503: 	return $value;
                   6504:     }
1.620     albertel 6505:     if ($env{'user.state.'.$env{'request.course.id'}}) {
                   6506:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40      www      6507:     } else {
                   6508:        return 2;
                   6509:     }
                   6510: }
                   6511: 
1.713     albertel 6512: # get the collection of conditions for this resource
1.26      www      6513: sub condval {
                   6514:     my $condidx=shift;
1.54      www      6515:     my $allpathcond='';
1.713     albertel 6516:     foreach my $cond (split(/\|/,$condidx)) {
                   6517: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
                   6518: 	    $allpathcond.=
                   6519: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
                   6520: 	}
1.191     harris41 6521:     }
1.54      www      6522:     $allpathcond=~s/\|$//;
1.713     albertel 6523:     return &docondval($allpathcond);
                   6524: }
                   6525: 
                   6526: #evaluates an expression of conditions
                   6527: sub docondval {
                   6528:     my ($allpathcond) = @_;
                   6529:     my $result=0;
                   6530:     if ($env{'request.course.id'}
                   6531: 	&& defined($allpathcond)) {
                   6532: 	my $operand='|';
                   6533: 	my @stack;
                   6534: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
                   6535: 	    if ($chunk eq '(') {
                   6536: 		push @stack,($operand,$result);
                   6537: 	    } elsif ($chunk eq ')') {
                   6538: 		my $before=pop @stack;
                   6539: 		if (pop @stack eq '&') {
                   6540: 		    $result=$result>$before?$before:$result;
                   6541: 		} else {
                   6542: 		    $result=$result>$before?$result:$before;
                   6543: 		}
                   6544: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
                   6545: 		$operand=$chunk;
                   6546: 	    } else {
                   6547: 		my $new=directcondval($chunk);
                   6548: 		if ($operand eq '&') {
                   6549: 		    $result=$result>$new?$new:$result;
                   6550: 		} else {
                   6551: 		    $result=$result>$new?$result:$new;
                   6552: 		}
                   6553: 	    }
                   6554: 	}
1.26      www      6555:     }
                   6556:     return $result;
1.421     albertel 6557: }
                   6558: 
                   6559: # ---------------------------------------------------- Devalidate courseresdata
                   6560: 
                   6561: sub devalidatecourseresdata {
                   6562:     my ($coursenum,$coursedomain)=@_;
                   6563:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 6564:     &devalidate_cache_new('courseres',$hashid);
1.28      www      6565: }
                   6566: 
1.763     www      6567: 
1.200     www      6568: # --------------------------------------------------- Course Resourcedata Query
1.878     foxr     6569: #
                   6570: #  Parameters:
                   6571: #      $coursenum    - Number of the course.
                   6572: #      $coursedomain - Domain at which the course was created.
                   6573: #  Returns:
                   6574: #     A hash of the course parameters along (I think) with timestamps
                   6575: #     and version info.
1.877     foxr     6576: 
1.624     albertel 6577: sub get_courseresdata {
                   6578:     my ($coursenum,$coursedomain)=@_;
1.200     www      6579:     my $coursehom=&homeserver($coursenum,$coursedomain);
                   6580:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 6581:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624     albertel 6582:     my %dumpreply;
1.417     albertel 6583:     unless (defined($cached)) {
1.624     albertel 6584: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417     albertel 6585: 	$result=\%dumpreply;
1.251     albertel 6586: 	my ($tmp) = keys(%dumpreply);
                   6587: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599     albertel 6588: 	    &do_cache_new('courseres',$hashid,$result,600);
1.306     albertel 6589: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
                   6590: 	    return $tmp;
1.416     albertel 6591: 	} elsif ($tmp =~ /^(error)/) {
1.417     albertel 6592: 	    $result=undef;
1.599     albertel 6593: 	    &do_cache_new('courseres',$hashid,$result,600);
1.250     albertel 6594: 	}
                   6595:     }
1.624     albertel 6596:     return $result;
                   6597: }
                   6598: 
1.633     albertel 6599: sub devalidateuserresdata {
                   6600:     my ($uname,$udom)=@_;
                   6601:     my $hashid="$udom:$uname";
                   6602:     &devalidate_cache_new('userres',$hashid);
                   6603: }
                   6604: 
1.624     albertel 6605: sub get_userresdata {
                   6606:     my ($uname,$udom)=@_;
                   6607:     #most student don\'t have any data set, check if there is some data
                   6608:     if (&EXT_cache_status($udom,$uname)) { return undef; }
                   6609: 
                   6610:     my $hashid="$udom:$uname";
                   6611:     my ($result,$cached)=&is_cached_new('userres',$hashid);
                   6612:     if (!defined($cached)) {
                   6613: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
                   6614: 	$result=\%resourcedata;
                   6615: 	&do_cache_new('userres',$hashid,$result,600);
                   6616:     }
                   6617:     my ($tmp)=keys(%$result);
                   6618:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
                   6619: 	return $result;
                   6620:     }
                   6621:     #error 2 occurs when the .db doesn't exist
                   6622:     if ($tmp!~/error: 2 /) {
1.672     albertel 6623: 	&logthis("<font color=\"blue\">WARNING:".
1.624     albertel 6624: 		 " Trying to get resource data for ".
                   6625: 		 $uname." at ".$udom.": ".
                   6626: 		 $tmp."</font>");
                   6627:     } elsif ($tmp=~/error: 2 /) {
1.633     albertel 6628: 	#&EXT_cache_set($udom,$uname);
                   6629: 	&do_cache_new('userres',$hashid,undef,600);
1.636     albertel 6630: 	undef($tmp); # not really an error so don't send it back
1.624     albertel 6631:     }
                   6632:     return $tmp;
                   6633: }
1.879     foxr     6634: #----------------------------------------------- resdata - return resource data
                   6635: #  Purpose:
                   6636: #    Return resource data for either users or for a course.
                   6637: #  Parameters:
                   6638: #     $name      - Course/user name.
                   6639: #     $domain    - Name of the domain the user/course is registered on.
                   6640: #     $type      - Type of thing $name is (must be 'course' or 'user'
                   6641: #     @which     - Array of names of resources desired.
                   6642: #  Returns:
                   6643: #     The value of the first reasource in @which that is found in the
                   6644: #     resource hash.
                   6645: #  Exceptional Conditions:
                   6646: #     If the $type passed in is not valid (not the string 'course' or 
                   6647: #     'user', an undefined  reference is returned.
                   6648: #     If none of the resources are found, an undef is returned
1.624     albertel 6649: sub resdata {
                   6650:     my ($name,$domain,$type,@which)=@_;
                   6651:     my $result;
                   6652:     if ($type eq 'course') {
                   6653: 	$result=&get_courseresdata($name,$domain);
                   6654:     } elsif ($type eq 'user') {
                   6655: 	$result=&get_userresdata($name,$domain);
                   6656:     }
                   6657:     if (!ref($result)) { return $result; }    
1.251     albertel 6658:     foreach my $item (@which) {
1.927     albertel 6659: 	if (defined($result->{$item->[0]})) {
                   6660: 	    return [$result->{$item->[0]},$item->[1]];
1.251     albertel 6661: 	}
1.250     albertel 6662:     }
1.291     albertel 6663:     return undef;
1.200     www      6664: }
                   6665: 
1.379     matthew  6666: #
                   6667: # EXT resource caching routines
                   6668: #
                   6669: 
                   6670: sub clear_EXT_cache_status {
1.383     albertel 6671:     &delenv('cache.EXT.');
1.379     matthew  6672: }
                   6673: 
                   6674: sub EXT_cache_status {
                   6675:     my ($target_domain,$target_user) = @_;
1.383     albertel 6676:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620     albertel 6677:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379     matthew  6678:         # We know already the user has no data
                   6679:         return 1;
                   6680:     } else {
                   6681:         return 0;
                   6682:     }
                   6683: }
                   6684: 
                   6685: sub EXT_cache_set {
                   6686:     my ($target_domain,$target_user) = @_;
1.383     albertel 6687:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.949     raeburn  6688:     #&appenv({$cachename => time});
1.379     matthew  6689: }
                   6690: 
1.28      www      6691: # --------------------------------------------------------- Value of a Variable
1.58      www      6692: sub EXT {
1.715     albertel 6693: 
1.395     albertel 6694:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse)=@_;
1.68      www      6695:     unless ($varname) { return ''; }
1.218     albertel 6696:     #get real user name/domain, courseid and symb
                   6697:     my $courseid;
1.359     albertel 6698:     my $publicuser;
1.427     www      6699:     if ($symbparm) {
                   6700: 	$symbparm=&get_symb_from_alias($symbparm);
                   6701:     }
1.218     albertel 6702:     if (!($uname && $udom)) {
1.790     albertel 6703:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218     albertel 6704:       if (!$symbparm) {	$symbparm=$cursymb; }
                   6705:     } else {
1.620     albertel 6706: 	$courseid=$env{'request.course.id'};
1.218     albertel 6707:     }
1.48      www      6708:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
                   6709:     my $rest;
1.320     albertel 6710:     if (defined($therest[0])) {
1.48      www      6711:        $rest=join('.',@therest);
                   6712:     } else {
                   6713:        $rest='';
                   6714:     }
1.320     albertel 6715: 
1.57      www      6716:     my $qualifierrest=$qualifier;
                   6717:     if ($rest) { $qualifierrest.='.'.$rest; }
                   6718:     my $spacequalifierrest=$space;
                   6719:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28      www      6720:     if ($realm eq 'user') {
1.48      www      6721: # --------------------------------------------------------------- user.resource
                   6722: 	if ($space eq 'resource') {
1.651     albertel 6723: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
                   6724: 		  || defined($Apache::lonhomework::parsing_a_task))
                   6725: 		 &&
1.744     albertel 6726: 		 ($symbparm eq &symbread()) ) {	
                   6727: 		# if we are in the middle of processing the resource the
                   6728: 		# get the value we are planning on committing
                   6729:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
                   6730:                     return $Apache::lonhomework::results{$qualifierrest};
                   6731:                 } else {
                   6732:                     return $Apache::lonhomework::history{$qualifierrest};
                   6733:                 }
1.335     albertel 6734: 	    } else {
1.359     albertel 6735: 		my %restored;
1.620     albertel 6736: 		if ($publicuser || $env{'request.state'} eq 'construct') {
1.359     albertel 6737: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
                   6738: 		} else {
                   6739: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
                   6740: 		}
1.335     albertel 6741: 		return $restored{$qualifierrest};
                   6742: 	    }
1.48      www      6743: # ----------------------------------------------------------------- user.access
                   6744:         } elsif ($space eq 'access') {
1.218     albertel 6745: 	    # FIXME - not supporting calls for a specific user
1.48      www      6746:             return &allowed($qualifier,$rest);
                   6747: # ------------------------------------------ user.preferences, user.environment
                   6748:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620     albertel 6749: 	    if (($uname eq $env{'user.name'}) &&
                   6750: 		($udom eq $env{'user.domain'})) {
                   6751: 		return $env{join('.',('environment',$qualifierrest))};
1.218     albertel 6752: 	    } else {
1.359     albertel 6753: 		my %returnhash;
                   6754: 		if (!$publicuser) {
                   6755: 		    %returnhash=&userenvironment($udom,$uname,
                   6756: 						 $qualifierrest);
                   6757: 		}
1.218     albertel 6758: 		return $returnhash{$qualifierrest};
                   6759: 	    }
1.48      www      6760: # ----------------------------------------------------------------- user.course
                   6761:         } elsif ($space eq 'course') {
1.218     albertel 6762: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 6763:             return $env{join('.',('request.course',$qualifier))};
1.48      www      6764: # ------------------------------------------------------------------- user.role
                   6765:         } elsif ($space eq 'role') {
1.218     albertel 6766: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 6767:             my ($role,$where)=split(/\./,$env{'request.role'});
1.48      www      6768:             if ($qualifier eq 'value') {
                   6769: 		return $role;
                   6770:             } elsif ($qualifier eq 'extent') {
                   6771:                 return $where;
                   6772:             }
                   6773: # ----------------------------------------------------------------- user.domain
                   6774:         } elsif ($space eq 'domain') {
1.218     albertel 6775:             return $udom;
1.48      www      6776: # ------------------------------------------------------------------- user.name
                   6777:         } elsif ($space eq 'name') {
1.218     albertel 6778:             return $uname;
1.48      www      6779: # ---------------------------------------------------- Any other user namespace
1.29      www      6780:         } else {
1.359     albertel 6781: 	    my %reply;
                   6782: 	    if (!$publicuser) {
                   6783: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
                   6784: 	    }
                   6785: 	    return $reply{$qualifierrest};
1.48      www      6786:         }
1.236     www      6787:     } elsif ($realm eq 'query') {
                   6788: # ---------------------------------------------- pull stuff out of query string
1.384     albertel 6789:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   6790: 						[$spacequalifierrest]);
1.620     albertel 6791: 	return $env{'form.'.$spacequalifierrest}; 
1.236     www      6792:    } elsif ($realm eq 'request') {
1.48      www      6793: # ------------------------------------------------------------- request.browser
                   6794:         if ($space eq 'browser') {
1.430     www      6795: 	    if ($qualifier eq 'textremote') {
1.676     albertel 6796: 		if (&Apache::lonlocal::mt('textual_remote_display') eq 'on') {
1.430     www      6797: 		    return 1;
                   6798: 		} else {
                   6799: 		    return 0;
                   6800: 		}
                   6801: 	    } else {
1.620     albertel 6802: 		return $env{'browser.'.$qualifier};
1.430     www      6803: 	    }
1.57      www      6804: # ------------------------------------------------------------ request.filename
                   6805:         } else {
1.620     albertel 6806:             return $env{'request.'.$spacequalifierrest};
1.29      www      6807:         }
1.28      www      6808:     } elsif ($realm eq 'course') {
1.48      www      6809: # ---------------------------------------------------------- course.description
1.620     albertel 6810:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57      www      6811:     } elsif ($realm eq 'resource') {
1.165     www      6812: 
1.620     albertel 6813: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539     albertel 6814: 	    if (!$symbparm) { $symbparm=&symbread(); }
                   6815: 	}
1.693     albertel 6816: 
                   6817: 	if ($space eq 'title') {
                   6818: 	    if (!$symbparm) { $symbparm = $env{'request.filename'}; }
                   6819: 	    return &gettitle($symbparm);
                   6820: 	}
                   6821: 	
                   6822: 	if ($space eq 'map') {
                   6823: 	    my ($map) = &decode_symb($symbparm);
                   6824: 	    return &symbread($map);
                   6825: 	}
1.905     albertel 6826: 	if ($space eq 'filename') {
                   6827: 	    if ($symbparm) {
                   6828: 		return &clutter((&decode_symb($symbparm))[2]);
                   6829: 	    }
                   6830: 	    return &hreflocation('',$env{'request.filename'});
                   6831: 	}
1.693     albertel 6832: 
                   6833: 	my ($section, $group, @groups);
1.593     albertel 6834: 	my ($courselevelm,$courselevel);
1.539     albertel 6835: 	if ($symbparm && defined($courseid) && 
1.620     albertel 6836: 	    $courseid eq $env{'request.course.id'}) {
1.165     www      6837: 
1.218     albertel 6838: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165     www      6839: 
1.60      www      6840: # ----------------------------------------------------- Cascading lookup scheme
1.218     albertel 6841: 	    my $symbp=$symbparm;
1.735     albertel 6842: 	    my $mapp=&deversion((&decode_symb($symbp))[0]);
1.218     albertel 6843: 
                   6844: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
                   6845: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
                   6846: 
1.620     albertel 6847: 	    if (($env{'user.name'} eq $uname) &&
                   6848: 		($env{'user.domain'} eq $udom)) {
                   6849: 		$section=$env{'request.course.sec'};
1.733     raeburn  6850:                 @groups = split(/:/,$env{'request.course.groups'});  
                   6851:                 @groups=&sort_course_groups($courseid,@groups); 
1.218     albertel 6852: 	    } else {
1.539     albertel 6853: 		if (! defined($usection)) {
1.551     albertel 6854: 		    $section=&getsection($udom,$uname,$courseid);
1.539     albertel 6855: 		} else {
                   6856: 		    $section = $usection;
                   6857: 		}
1.733     raeburn  6858:                 @groups = &get_users_groups($udom,$uname,$courseid);
1.218     albertel 6859: 	    }
                   6860: 
                   6861: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
                   6862: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
                   6863: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
                   6864: 
1.593     albertel 6865: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
1.218     albertel 6866: 	    my $courselevelr=$courseid.'.'.$symbparm;
1.593     albertel 6867: 	    $courselevelm=$courseid.'.'.$mapparm;
1.69      www      6868: 
1.60      www      6869: # ----------------------------------------------------------- first, check user
1.624     albertel 6870: 
                   6871: 	    my $userreply=&resdata($uname,$udom,'user',
1.927     albertel 6872: 				       ([$courselevelr,'resource'],
                   6873: 					[$courselevelm,'map'     ],
                   6874: 					[$courselevel, 'course'  ]));
1.931     albertel 6875: 	    if (defined($userreply)) { return &get_reply($userreply); }
1.95      www      6876: 
1.594     albertel 6877: # ------------------------------------------------ second, check some of course
1.684     raeburn  6878:             my $coursereply;
1.691     raeburn  6879:             if (@groups > 0) {
                   6880:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
                   6881:                                        $mapparm,$spacequalifierrest);
1.927     albertel 6882:                 if (defined($coursereply)) { return &get_reply($coursereply); }
1.684     raeburn  6883:             }
1.96      www      6884: 
1.684     raeburn  6885: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.927     albertel 6886: 				  $env{'course.'.$courseid.'.domain'},
                   6887: 				  'course',
                   6888: 				  ([$seclevelr,   'resource'],
                   6889: 				   [$seclevelm,   'map'     ],
                   6890: 				   [$seclevel,    'course'  ],
                   6891: 				   [$courselevelr,'resource']));
                   6892: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
1.200     www      6893: 
1.60      www      6894: # ------------------------------------------------------ third, check map parms
1.218     albertel 6895: 	    my %parmhash=();
                   6896: 	    my $thisparm='';
                   6897: 	    if (tie(%parmhash,'GDBM_File',
1.620     albertel 6898: 		    $env{'request.course.fn'}.'_parms.db',
1.256     albertel 6899: 		    &GDBM_READER(),0640)) {
1.218     albertel 6900: 		$thisparm=$parmhash{$symbparm};
                   6901: 		untie(%parmhash);
                   6902: 	    }
1.927     albertel 6903: 	    if ($thisparm) { return &get_reply([$thisparm,'resource']); }
1.218     albertel 6904: 	}
1.594     albertel 6905: # ------------------------------------------ fourth, look in resource metadata
1.71      www      6906: 
1.218     albertel 6907: 	$spacequalifierrest=~s/\./\_/;
1.282     albertel 6908: 	my $filename;
                   6909: 	if (!$symbparm) { $symbparm=&symbread(); }
                   6910: 	if ($symbparm) {
1.409     www      6911: 	    $filename=(&decode_symb($symbparm))[2];
1.282     albertel 6912: 	} else {
1.620     albertel 6913: 	    $filename=$env{'request.filename'};
1.282     albertel 6914: 	}
                   6915: 	my $metadata=&metadata($filename,$spacequalifierrest);
1.927     albertel 6916: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.282     albertel 6917: 	$metadata=&metadata($filename,'parameter_'.$spacequalifierrest);
1.927     albertel 6918: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.142     www      6919: 
1.927     albertel 6920: # ---------------------------------------------- fourth, look in rest of course
1.593     albertel 6921: 	if ($symbparm && defined($courseid) && 
1.620     albertel 6922: 	    $courseid eq $env{'request.course.id'}) {
1.624     albertel 6923: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
                   6924: 				     $env{'course.'.$courseid.'.domain'},
                   6925: 				     'course',
1.927     albertel 6926: 				     ([$courselevelm,'map'   ],
                   6927: 				      [$courselevel, 'course']));
                   6928: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
1.593     albertel 6929: 	}
1.145     www      6930: # ------------------------------------------------------------------ Cascade up
1.218     albertel 6931: 	unless ($space eq '0') {
1.336     albertel 6932: 	    my @parts=split(/_/,$space);
                   6933: 	    my $id=pop(@parts);
                   6934: 	    my $part=join('_',@parts);
                   6935: 	    if ($part eq '') { $part='0'; }
1.927     albertel 6936: 	    my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395     albertel 6937: 				 $symbparm,$udom,$uname,$section,1);
1.938     raeburn  6938: 	    if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
1.218     albertel 6939: 	}
1.395     albertel 6940: 	if ($recurse) { return undef; }
                   6941: 	my $pack_def=&packages_tab_default($filename,$varname);
1.927     albertel 6942: 	if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
1.48      www      6943: # ---------------------------------------------------- Any other user namespace
                   6944:     } elsif ($realm eq 'environment') {
                   6945: # ----------------------------------------------------------------- environment
1.620     albertel 6946: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
                   6947: 	    return $env{'environment.'.$spacequalifierrest};
1.219     albertel 6948: 	} else {
1.770     albertel 6949: 	    if ($uname eq 'anonymous' && $udom eq '') {
                   6950: 		return '';
                   6951: 	    }
1.219     albertel 6952: 	    my %returnhash=&userenvironment($udom,$uname,
                   6953: 					    $spacequalifierrest);
                   6954: 	    return $returnhash{$spacequalifierrest};
                   6955: 	}
1.28      www      6956:     } elsif ($realm eq 'system') {
1.48      www      6957: # ----------------------------------------------------------------- system.time
                   6958: 	if ($space eq 'time') {
                   6959: 	    return time;
                   6960:         }
1.696     albertel 6961:     } elsif ($realm eq 'server') {
                   6962: # ----------------------------------------------------------------- system.time
                   6963: 	if ($space eq 'name') {
                   6964: 	    return $ENV{'SERVER_NAME'};
                   6965:         }
1.28      www      6966:     }
1.48      www      6967:     return '';
1.61      www      6968: }
                   6969: 
1.927     albertel 6970: sub get_reply {
                   6971:     my ($reply_value) = @_;
1.940     raeburn  6972:     if (ref($reply_value) eq 'ARRAY') {
                   6973:         if (wantarray) {
                   6974: 	    return @$reply_value;
                   6975:         }
                   6976:         return $reply_value->[0];
                   6977:     } else {
                   6978:         return $reply_value;
1.927     albertel 6979:     }
                   6980: }
                   6981: 
1.691     raeburn  6982: sub check_group_parms {
                   6983:     my ($courseid,$groups,$symbparm,$mapparm,$what) = @_;
                   6984:     my @groupitems = ();
                   6985:     my $resultitem;
1.927     albertel 6986:     my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$what,'course']);
1.691     raeburn  6987:     foreach my $group (@{$groups}) {
                   6988:         foreach my $level (@levels) {
1.927     albertel 6989:              my $item = $courseid.'.['.$group.'].'.$level->[0];
                   6990:              push(@groupitems,[$item,$level->[1]]);
1.691     raeburn  6991:         }
                   6992:     }
                   6993:     my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
                   6994:                             $env{'course.'.$courseid.'.domain'},
                   6995:                                      'course',@groupitems);
                   6996:     return $coursereply;
                   6997: }
                   6998: 
                   6999: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733     raeburn  7000:     my ($courseid,@groups) = @_;
                   7001:     @groups = sort(@groups);
1.691     raeburn  7002:     return @groups;
                   7003: }
                   7004: 
1.395     albertel 7005: sub packages_tab_default {
                   7006:     my ($uri,$varname)=@_;
                   7007:     my (undef,$part,$name)=split(/\./,$varname);
1.738     albertel 7008: 
                   7009:     my (@extension,@specifics,$do_default);
                   7010:     foreach my $package (split(/,/,&metadata($uri,'packages'))) {
1.395     albertel 7011: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738     albertel 7012: 	if ($pack_type eq 'default') {
                   7013: 	    $do_default=1;
                   7014: 	} elsif ($pack_type eq 'extension') {
                   7015: 	    push(@extension,[$package,$pack_type,$pack_part]);
1.885     albertel 7016: 	} elsif ($pack_part eq $part || $pack_type eq 'part') {
1.848     albertel 7017: 	    # only look at packages defaults for packages that this id is
1.738     albertel 7018: 	    push(@specifics,[$package,$pack_type,$pack_part]);
                   7019: 	}
                   7020:     }
                   7021:     # first look for a package that matches the requested part id
                   7022:     foreach my $package (@specifics) {
                   7023: 	my (undef,$pack_type,$pack_part)=@{$package};
                   7024: 	next if ($pack_part ne $part);
                   7025: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   7026: 	    return $packagetab{"$pack_type&$name&default"};
                   7027: 	}
                   7028:     }
                   7029:     # look for any possible matching non extension_ package
                   7030:     foreach my $package (@specifics) {
                   7031: 	my (undef,$pack_type,$pack_part)=@{$package};
1.468     albertel 7032: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   7033: 	    return $packagetab{"$pack_type&$name&default"};
                   7034: 	}
1.585     albertel 7035: 	if ($pack_type eq 'part') { $pack_part='0'; }
1.468     albertel 7036: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
                   7037: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395     albertel 7038: 	}
                   7039:     }
1.738     albertel 7040:     # look for any posible extension_ match
                   7041:     foreach my $package (@extension) {
                   7042: 	my ($package,$pack_type)=@{$package};
                   7043: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   7044: 	    return $packagetab{"$pack_type&$name&default"};
                   7045: 	}
                   7046: 	if (defined($packagetab{$package."&$name&default"})) {
                   7047: 	    return $packagetab{$package."&$name&default"};
                   7048: 	}
                   7049:     }
                   7050:     # look for a global default setting
                   7051:     if ($do_default && defined($packagetab{"default&$name&default"})) {
                   7052: 	return $packagetab{"default&$name&default"};
                   7053:     }
1.395     albertel 7054:     return undef;
                   7055: }
                   7056: 
1.334     albertel 7057: sub add_prefix_and_part {
                   7058:     my ($prefix,$part)=@_;
                   7059:     my $keyroot;
                   7060:     if (defined($prefix) && $prefix !~ /^__/) {
                   7061: 	# prefix that has a part already
                   7062: 	$keyroot=$prefix;
                   7063:     } elsif (defined($prefix)) {
                   7064: 	# prefix that is missing a part
                   7065: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
                   7066:     } else {
                   7067: 	# no prefix at all
                   7068: 	if (defined($part)) { $keyroot='_'.$part; }
                   7069:     }
                   7070:     return $keyroot;
                   7071: }
                   7072: 
1.71      www      7073: # ---------------------------------------------------------------- Get metadata
                   7074: 
1.599     albertel 7075: my %metaentry;
1.71      www      7076: sub metadata {
1.176     www      7077:     my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
1.71      www      7078:     $uri=&declutter($uri);
1.288     albertel 7079:     # if it is a non metadata possible uri return quickly
1.529     albertel 7080:     if (($uri eq '') || 
                   7081: 	(($uri =~ m|^/*adm/|) && 
1.698     albertel 7082: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m|/bulletinboard$|)) ||
1.924     albertel 7083:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) ) {
                   7084: 	return undef;
                   7085:     }
                   7086:     if (($uri =~ /^~/ || $uri =~ m{home/$match_username/public_html/}) 
                   7087: 	&& &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
1.468     albertel 7088: 	return undef;
1.288     albertel 7089:     }
1.73      www      7090:     my $filename=$uri;
                   7091:     $uri=~s/\.meta$//;
1.172     www      7092: #
                   7093: # Is the metadata already cached?
1.177     www      7094: # Look at timestamp of caching
1.172     www      7095: # Everything is cached by the main uri, libraries are never directly cached
                   7096: #
1.428     albertel 7097:     if (!defined($liburi)) {
1.599     albertel 7098: 	my ($result,$cached)=&is_cached_new('meta',$uri);
1.428     albertel 7099: 	if (defined($cached)) { return $result->{':'.$what}; }
                   7100:     }
                   7101:     {
1.172     www      7102: #
                   7103: # Is this a recursive call for a library?
                   7104: #
1.599     albertel 7105: #	if (! exists($metacache{$uri})) {
                   7106: #	    $metacache{$uri}={};
                   7107: #	}
1.924     albertel 7108: 	my $cachetime = 60*60;
1.171     www      7109:         if ($liburi) {
                   7110: 	    $liburi=&declutter($liburi);
                   7111:             $filename=$liburi;
1.401     bowersj2 7112:         } else {
1.599     albertel 7113: 	    &devalidate_cache_new('meta',$uri);
                   7114: 	    undef(%metaentry);
1.401     bowersj2 7115: 	}
1.140     www      7116:         my %metathesekeys=();
1.73      www      7117:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489     albertel 7118: 	my $metastring;
1.924     albertel 7119: 	if ($uri =~ /^~/ || $uri =~ m{home/$match_username/public_html/}) {
1.929     albertel 7120: 	    my $which = &hreflocation('','/'.($liburi || $uri));
1.924     albertel 7121: 	    $metastring = 
1.929     albertel 7122: 		&Apache::lonnet::ssi_body($which,
1.924     albertel 7123: 					  ('grade_target' => 'meta'));
                   7124: 	    $cachetime = 1; # only want this cached in the child not long term
                   7125: 	} elsif ($uri !~ m -^(editupload)/-) {
1.543     albertel 7126: 	    my $file=&filelocation('',&clutter($filename));
1.599     albertel 7127: 	    #push(@{$metaentry{$uri.'.file'}},$file);
1.543     albertel 7128: 	    $metastring=&getfile($file);
1.489     albertel 7129: 	}
1.208     albertel 7130:         my $parser=HTML::LCParser->new(\$metastring);
1.71      www      7131:         my $token;
1.140     www      7132:         undef %metathesekeys;
1.71      www      7133:         while ($token=$parser->get_token) {
1.339     albertel 7134: 	    if ($token->[0] eq 'S') {
                   7135: 		if (defined($token->[2]->{'package'})) {
1.172     www      7136: #
                   7137: # This is a package - get package info
                   7138: #
1.339     albertel 7139: 		    my $package=$token->[2]->{'package'};
                   7140: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   7141: 		    if (defined($token->[2]->{'id'})) { 
                   7142: 			$keyroot.='_'.$token->[2]->{'id'}; 
                   7143: 		    }
1.599     albertel 7144: 		    if ($metaentry{':packages'}) {
                   7145: 			$metaentry{':packages'}.=','.$package.$keyroot;
1.339     albertel 7146: 		    } else {
1.599     albertel 7147: 			$metaentry{':packages'}=$package.$keyroot;
1.339     albertel 7148: 		    }
1.736     albertel 7149: 		    foreach my $pack_entry (keys(%packagetab)) {
1.432     albertel 7150: 			my $part=$keyroot;
                   7151: 			$part=~s/^\_//;
1.736     albertel 7152: 			if ($pack_entry=~/^\Q$package\E\&/ || 
                   7153: 			    $pack_entry=~/^\Q$package\E_0\&/) {
                   7154: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395     albertel 7155: 			    # ignore package.tab specified default values
                   7156:                             # here &package_tab_default() will fetch those
                   7157: 			    if ($subp eq 'default') { next; }
1.736     albertel 7158: 			    my $value=$packagetab{$pack_entry};
1.432     albertel 7159: 			    my $unikey;
                   7160: 			    if ($pack =~ /_0$/) {
                   7161: 				$unikey='parameter_0_'.$name;
                   7162: 				$part=0;
                   7163: 			    } else {
                   7164: 				$unikey='parameter'.$keyroot.'_'.$name;
                   7165: 			    }
1.339     albertel 7166: 			    if ($subp eq 'display') {
                   7167: 				$value.=' [Part: '.$part.']';
                   7168: 			    }
1.599     albertel 7169: 			    $metaentry{':'.$unikey.'.part'}=$part;
1.395     albertel 7170: 			    $metathesekeys{$unikey}=1;
1.599     albertel 7171: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   7172: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
1.339     albertel 7173: 			    }
1.599     albertel 7174: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
                   7175: 				$metaentry{':'.$unikey}=
                   7176: 				    $metaentry{':'.$unikey.'.default'};
1.356     albertel 7177: 			    }
1.339     albertel 7178: 			}
                   7179: 		    }
                   7180: 		} else {
1.172     www      7181: #
                   7182: # This is not a package - some other kind of start tag
1.339     albertel 7183: #
                   7184: 		    my $entry=$token->[1];
                   7185: 		    my $unikey;
                   7186: 		    if ($entry eq 'import') {
                   7187: 			$unikey='';
                   7188: 		    } else {
                   7189: 			$unikey=$entry;
                   7190: 		    }
                   7191: 		    $unikey.=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   7192: 
                   7193: 		    if (defined($token->[2]->{'id'})) { 
                   7194: 			$unikey.='_'.$token->[2]->{'id'}; 
                   7195: 		    }
1.175     www      7196: 
1.339     albertel 7197: 		    if ($entry eq 'import') {
1.175     www      7198: #
                   7199: # Importing a library here
1.339     albertel 7200: #
                   7201: 			if ($depthcount<20) {
                   7202: 			    my $location=$parser->get_text('/import');
                   7203: 			    my $dir=$filename;
                   7204: 			    $dir=~s|[^/]*$||;
                   7205: 			    $location=&filelocation($dir,$location);
1.736     albertel 7206: 			    my $metadata = 
                   7207: 				&metadata($uri,'keys', $location,$unikey,
                   7208: 					  $depthcount+1);
                   7209: 			    foreach my $meta (split(',',$metadata)) {
                   7210: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
                   7211: 				$metathesekeys{$meta}=1;
1.339     albertel 7212: 			    }
                   7213: 			}
                   7214: 		    } else { 
                   7215: 			
                   7216: 			if (defined($token->[2]->{'name'})) { 
                   7217: 			    $unikey.='_'.$token->[2]->{'name'}; 
                   7218: 			}
                   7219: 			$metathesekeys{$unikey}=1;
1.736     albertel 7220: 			foreach my $param (@{$token->[3]}) {
                   7221: 			    $metaentry{':'.$unikey.'.'.$param} =
                   7222: 				$token->[2]->{$param};
1.339     albertel 7223: 			}
                   7224: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599     albertel 7225: 			my $default=$metaentry{':'.$unikey.'.default'};
1.339     albertel 7226: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
                   7227: 		 # only ws inside the tag, and not in default, so use default
                   7228: 		 # as value
1.599     albertel 7229: 			    $metaentry{':'.$unikey}=$default;
1.908     albertel 7230: 			} elsif ( $internaltext =~ /\S/ ) {
                   7231: 		  # something interesting inside the tag
                   7232: 			    $metaentry{':'.$unikey}=$internaltext;
1.339     albertel 7233: 			} else {
1.908     albertel 7234: 		  # no interesting values, don't set a default
1.339     albertel 7235: 			}
1.172     www      7236: # end of not-a-package not-a-library import
1.339     albertel 7237: 		    }
1.172     www      7238: # end of not-a-package start tag
1.339     albertel 7239: 		}
1.172     www      7240: # the next is the end of "start tag"
1.339     albertel 7241: 	    }
                   7242: 	}
1.483     albertel 7243: 	my ($extension) = ($uri =~ /\.(\w+)$/);
1.883     albertel 7244: 	$extension = lc($extension);
                   7245: 	if ($extension eq 'htm') { $extension='html'; }
                   7246: 
1.737     albertel 7247: 	foreach my $key (keys(%packagetab)) {
1.483     albertel 7248: 	    #no specific packages #how's our extension
                   7249: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488     albertel 7250: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483     albertel 7251: 					 \%metathesekeys);
                   7252: 	}
1.883     albertel 7253: 
                   7254: 	if (!exists($metaentry{':packages'})
                   7255: 	    || $packagetab{"import_defaults&extension_$extension"}) {
1.737     albertel 7256: 	    foreach my $key (keys(%packagetab)) {
1.483     albertel 7257: 		#no specific packages well let's get default then
                   7258: 		if ($key!~/^default&/) { next; }
1.488     albertel 7259: 		&metadata_create_package_def($uri,$key,'default',
1.483     albertel 7260: 					     \%metathesekeys);
                   7261: 	    }
                   7262: 	}
1.338     www      7263: # are there custom rights to evaluate
1.599     albertel 7264: 	if ($metaentry{':copyright'} eq 'custom') {
1.339     albertel 7265: 
1.338     www      7266:     #
                   7267:     # Importing a rights file here
1.339     albertel 7268:     #
                   7269: 	    unless ($depthcount) {
1.599     albertel 7270: 		my $location=$metaentry{':customdistributionfile'};
1.339     albertel 7271: 		my $dir=$filename;
                   7272: 		$dir=~s|[^/]*$||;
                   7273: 		$location=&filelocation($dir,$location);
1.736     albertel 7274: 		my $rights_metadata =
                   7275: 		    &metadata($uri,'keys',$location,'_rights',
                   7276: 			      $depthcount+1);
                   7277: 		foreach my $rights (split(',',$rights_metadata)) {
                   7278: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
                   7279: 		    $metathesekeys{$rights}=1;
1.339     albertel 7280: 		}
                   7281: 	    }
                   7282: 	}
1.737     albertel 7283: 	# uniqifiy package listing
                   7284: 	my %seen;
                   7285: 	my @uniq_packages =
                   7286: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
                   7287: 	$metaentry{':packages'} = join(',',@uniq_packages);
                   7288: 
                   7289: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599     albertel 7290: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
                   7291: 	$metaentry{':allpossiblekeys'}=join(',',keys %metathesekeys);
1.924     albertel 7292: 	&do_cache_new('meta',$uri,\%metaentry,$cachetime);
1.177     www      7293: # this is the end of "was not already recently cached
1.71      www      7294:     }
1.599     albertel 7295:     return $metaentry{':'.$what};
1.261     albertel 7296: }
                   7297: 
1.488     albertel 7298: sub metadata_create_package_def {
1.483     albertel 7299:     my ($uri,$key,$package,$metathesekeys)=@_;
                   7300:     my ($pack,$name,$subp)=split(/\&/,$key);
                   7301:     if ($subp eq 'default') { next; }
                   7302:     
1.599     albertel 7303:     if (defined($metaentry{':packages'})) {
                   7304: 	$metaentry{':packages'}.=','.$package;
1.483     albertel 7305:     } else {
1.599     albertel 7306: 	$metaentry{':packages'}=$package;
1.483     albertel 7307:     }
                   7308:     my $value=$packagetab{$key};
                   7309:     my $unikey;
                   7310:     $unikey='parameter_0_'.$name;
1.599     albertel 7311:     $metaentry{':'.$unikey.'.part'}=0;
1.483     albertel 7312:     $$metathesekeys{$unikey}=1;
1.599     albertel 7313:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   7314: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
1.483     albertel 7315:     }
1.599     albertel 7316:     if (defined($metaentry{':'.$unikey.'.default'})) {
                   7317: 	$metaentry{':'.$unikey}=
                   7318: 	    $metaentry{':'.$unikey.'.default'};
1.483     albertel 7319:     }
                   7320: }
                   7321: 
1.261     albertel 7322: sub metadata_generate_part0 {
                   7323:     my ($metadata,$metacache,$uri) = @_;
                   7324:     my %allnames;
1.737     albertel 7325:     foreach my $metakey (keys(%$metadata)) {
1.261     albertel 7326: 	if ($metakey=~/^parameter\_(.*)/) {
1.428     albertel 7327: 	  my $part=$$metacache{':'.$metakey.'.part'};
                   7328: 	  my $name=$$metacache{':'.$metakey.'.name'};
1.356     albertel 7329: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261     albertel 7330: 	    $allnames{$name}=$part;
                   7331: 	  }
                   7332: 	}
                   7333:     }
                   7334:     foreach my $name (keys(%allnames)) {
                   7335:       $$metadata{"parameter_0_$name"}=1;
1.428     albertel 7336:       my $key=":parameter_0_$name";
1.261     albertel 7337:       $$metacache{"$key.part"}='0';
                   7338:       $$metacache{"$key.name"}=$name;
1.428     albertel 7339:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261     albertel 7340: 					   $allnames{$name}.'_'.$name.
                   7341: 					   '.type'};
1.428     albertel 7342:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261     albertel 7343: 			     '.display'};
1.644     www      7344:       my $expr='[Part: '.$allnames{$name}.']';
1.479     albertel 7345:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261     albertel 7346:       $$metacache{"$key.display"}=$olddis;
                   7347:     }
1.71      www      7348: }
                   7349: 
1.764     albertel 7350: # ------------------------------------------------------ Devalidate title cache
                   7351: 
                   7352: sub devalidate_title_cache {
                   7353:     my ($url)=@_;
                   7354:     if (!$env{'request.course.id'}) { return; }
                   7355:     my $symb=&symbread($url);
                   7356:     if (!$symb) { return; }
                   7357:     my $key=$env{'request.course.id'}."\0".$symb;
                   7358:     &devalidate_cache_new('title',$key);
                   7359: }
                   7360: 
1.301     www      7361: # ------------------------------------------------- Get the title of a resource
                   7362: 
                   7363: sub gettitle {
                   7364:     my $urlsymb=shift;
                   7365:     my $symb=&symbread($urlsymb);
1.534     albertel 7366:     if ($symb) {
1.620     albertel 7367: 	my $key=$env{'request.course.id'}."\0".$symb;
1.599     albertel 7368: 	my ($result,$cached)=&is_cached_new('title',$key);
1.575     albertel 7369: 	if (defined($cached)) { 
                   7370: 	    return $result;
                   7371: 	}
1.534     albertel 7372: 	my ($map,$resid,$url)=&decode_symb($symb);
                   7373: 	my $title='';
1.907     albertel 7374: 	if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
                   7375: 	    $title = $env{'course.'.$env{'request.course.id'}.'.description'};
                   7376: 	} else {
                   7377: 	    if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
                   7378: 		    &GDBM_READER(),0640)) {
                   7379: 		my $mapid=$bighash{'map_pc_'.&clutter($map)};
                   7380: 		$title=$bighash{'title_'.$mapid.'.'.$resid};
                   7381: 		untie(%bighash);
                   7382: 	    }
1.534     albertel 7383: 	}
                   7384: 	$title=~s/\&colon\;/\:/gs;
                   7385: 	if ($title) {
1.599     albertel 7386: 	    return &do_cache_new('title',$key,$title,600);
1.534     albertel 7387: 	}
                   7388: 	$urlsymb=$url;
                   7389:     }
                   7390:     my $title=&metadata($urlsymb,'title');
                   7391:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
                   7392:     return $title;
1.301     www      7393: }
1.613     albertel 7394: 
1.614     albertel 7395: sub get_slot {
                   7396:     my ($which,$cnum,$cdom)=@_;
                   7397:     if (!$cnum || !$cdom) {
1.790     albertel 7398: 	(undef,my $courseid)=&whichuser();
1.620     albertel 7399: 	$cdom=$env{'course.'.$courseid.'.domain'};
                   7400: 	$cnum=$env{'course.'.$courseid.'.num'};
1.614     albertel 7401:     }
1.703     albertel 7402:     my $key=join("\0",'slots',$cdom,$cnum,$which);
                   7403:     my %slotinfo;
                   7404:     if (exists($remembered{$key})) {
                   7405: 	$slotinfo{$which} = $remembered{$key};
                   7406:     } else {
                   7407: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
                   7408: 	&Apache::lonhomework::showhash(%slotinfo);
                   7409: 	my ($tmp)=keys(%slotinfo);
                   7410: 	if ($tmp=~/^error:/) { return (); }
                   7411: 	$remembered{$key} = $slotinfo{$which};
                   7412:     }
1.616     albertel 7413:     if (ref($slotinfo{$which}) eq 'HASH') {
                   7414: 	return %{$slotinfo{$which}};
                   7415:     }
                   7416:     return $slotinfo{$which};
1.614     albertel 7417: }
1.31      www      7418: # ------------------------------------------------- Update symbolic store links
                   7419: 
                   7420: sub symblist {
                   7421:     my ($mapname,%newhash)=@_;
1.438     www      7422:     $mapname=&deversion(&declutter($mapname));
1.31      www      7423:     my %hash;
1.620     albertel 7424:     if (($env{'request.course.fn'}) && (%newhash)) {
                   7425:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256     albertel 7426:                       &GDBM_WRCREAT(),0640)) {
1.711     albertel 7427: 	    foreach my $url (keys %newhash) {
                   7428: 		next if ($url eq 'last_known'
                   7429: 			 && $env{'form.no_update_last_known'});
                   7430: 		$hash{declutter($url)}=&encode_symb($mapname,
                   7431: 						    $newhash{$url}->[1],
                   7432: 						    $newhash{$url}->[0]);
1.191     harris41 7433:             }
1.31      www      7434:             if (untie(%hash)) {
                   7435: 		return 'ok';
                   7436:             }
                   7437:         }
                   7438:     }
                   7439:     return 'error';
1.212     www      7440: }
                   7441: 
                   7442: # --------------------------------------------------------------- Verify a symb
                   7443: 
                   7444: sub symbverify {
1.510     www      7445:     my ($symb,$thisurl)=@_;
                   7446:     my $thisfn=$thisurl;
1.439     www      7447:     $thisfn=&declutter($thisfn);
1.215     www      7448: # direct jump to resource in page or to a sequence - will construct own symbs
                   7449:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
                   7450: # check URL part
1.409     www      7451:     my ($map,$resid,$url)=&decode_symb($symb);
1.439     www      7452: 
1.431     www      7453:     unless ($url eq $thisfn) { return 0; }
1.213     www      7454: 
1.216     www      7455:     $symb=&symbclean($symb);
1.510     www      7456:     $thisurl=&deversion($thisurl);
1.439     www      7457:     $thisfn=&deversion($thisfn);
1.213     www      7458: 
                   7459:     my %bighash;
                   7460:     my $okay=0;
1.431     www      7461: 
1.620     albertel 7462:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 7463:                             &GDBM_READER(),0640)) {
1.510     www      7464:         my $ids=$bighash{'ids_'.&clutter($thisurl)};
1.216     www      7465:         unless ($ids) { 
1.510     www      7466:            $ids=$bighash{'ids_/'.$thisurl};
1.216     www      7467:         }
                   7468:         if ($ids) {
                   7469: # ------------------------------------------------------------------- Has ID(s)
1.800     albertel 7470: 	    foreach my $id (split(/\,/,$ids)) {
                   7471: 	       my ($mapid,$resid)=split(/\./,$id);
1.216     www      7472:                if (
                   7473:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
                   7474:    eq $symb) { 
1.620     albertel 7475: 		   if (($env{'request.role.adv'}) ||
1.800     albertel 7476: 		       $bighash{'encrypted_'.$id} eq $env{'request.enc'}) {
1.582     albertel 7477: 		       $okay=1; 
                   7478: 		   }
                   7479: 	       }
1.216     www      7480: 	   }
                   7481:         }
1.213     www      7482: 	untie(%bighash);
                   7483:     }
                   7484:     return $okay;
1.31      www      7485: }
                   7486: 
1.210     www      7487: # --------------------------------------------------------------- Clean-up symb
                   7488: 
                   7489: sub symbclean {
                   7490:     my $symb=shift;
1.568     albertel 7491:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210     www      7492: # remove version from map
                   7493:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215     www      7494: 
1.210     www      7495: # remove version from URL
                   7496:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213     www      7497: 
1.507     www      7498: # remove wrapper
                   7499: 
1.510     www      7500:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694     albertel 7501:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210     www      7502:     return $symb;
1.409     www      7503: }
                   7504: 
                   7505: # ---------------------------------------------- Split symb to find map and url
1.429     albertel 7506: 
                   7507: sub encode_symb {
                   7508:     my ($map,$resid,$url)=@_;
                   7509:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
                   7510: }
1.409     www      7511: 
                   7512: sub decode_symb {
1.568     albertel 7513:     my $symb=shift;
                   7514:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
                   7515:     my ($map,$resid,$url)=split(/___/,$symb);
1.413     www      7516:     return (&fixversion($map),$resid,&fixversion($url));
                   7517: }
                   7518: 
                   7519: sub fixversion {
                   7520:     my $fn=shift;
1.609     banghart 7521:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435     www      7522:     my %bighash;
                   7523:     my $uri=&clutter($fn);
1.620     albertel 7524:     my $key=$env{'request.course.id'}.'_'.$uri;
1.440     www      7525: # is this cached?
1.599     albertel 7526:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440     www      7527:     if (defined($cached)) { return $result; }
                   7528: # unfortunately not cached, or expired
1.620     albertel 7529:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440     www      7530: 	    &GDBM_READER(),0640)) {
                   7531:  	if ($bighash{'version_'.$uri}) {
                   7532:  	    my $version=$bighash{'version_'.$uri};
1.444     www      7533:  	    unless (($version eq 'mostrecent') || 
                   7534: 		    ($version==&getversion($uri))) {
1.440     www      7535:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
                   7536:  	    }
                   7537:  	}
                   7538:  	untie %bighash;
1.413     www      7539:     }
1.599     albertel 7540:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438     www      7541: }
                   7542: 
                   7543: sub deversion {
                   7544:     my $url=shift;
                   7545:     $url=~s/\.\d+\.(\w+)$/\.$1/;
                   7546:     return $url;
1.210     www      7547: }
                   7548: 
1.31      www      7549: # ------------------------------------------------------ Return symb list entry
                   7550: 
                   7551: sub symbread {
1.249     www      7552:     my ($thisfn,$donotrecurse)=@_;
1.542     albertel 7553:     my $cache_str='request.symbread.cached.'.$thisfn;
1.620     albertel 7554:     if (defined($env{$cache_str})) { return $env{$cache_str}; }
1.242     www      7555: # no filename provided? try from environment
1.44      www      7556:     unless ($thisfn) {
1.620     albertel 7557:         if ($env{'request.symb'}) {
                   7558: 	    return $env{$cache_str}=&symbclean($env{'request.symb'});
1.539     albertel 7559: 	}
1.620     albertel 7560: 	$thisfn=$env{'request.filename'};
1.44      www      7561:     }
1.569     albertel 7562:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242     www      7563: # is that filename actually a symb? Verify, clean, and return
                   7564:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539     albertel 7565: 	if (&symbverify($thisfn,$1)) {
1.620     albertel 7566: 	    return $env{$cache_str}=&symbclean($thisfn);
1.539     albertel 7567: 	}
1.242     www      7568:     }
1.44      www      7569:     $thisfn=declutter($thisfn);
1.31      www      7570:     my %hash;
1.37      www      7571:     my %bighash;
                   7572:     my $syval='';
1.620     albertel 7573:     if (($env{'request.course.fn'}) && ($thisfn)) {
1.481     raeburn  7574:         my $targetfn = $thisfn;
1.609     banghart 7575:         if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
1.481     raeburn  7576:             $targetfn = 'adm/wrapper/'.$thisfn;
                   7577:         }
1.687     albertel 7578: 	if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
                   7579: 	    $targetfn=$1;
                   7580: 	}
1.620     albertel 7581:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256     albertel 7582:                       &GDBM_READER(),0640)) {
1.481     raeburn  7583: 	    $syval=$hash{$targetfn};
1.37      www      7584:             untie(%hash);
                   7585:         }
                   7586: # ---------------------------------------------------------- There was an entry
                   7587:         if ($syval) {
1.601     albertel 7588: 	    #unless ($syval=~/\_\d+$/) {
1.620     albertel 7589: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.949     raeburn  7590: 		    #&appenv({'request.ambiguous' => $thisfn});
1.620     albertel 7591: 		    #return $env{$cache_str}='';
1.601     albertel 7592: 		#}    
                   7593: 		#$syval.=$1;
                   7594: 	    #}
1.37      www      7595:         } else {
                   7596: # ------------------------------------------------------- Was not in symb table
1.620     albertel 7597:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 7598:                             &GDBM_READER(),0640)) {
1.37      www      7599: # ---------------------------------------------- Get ID(s) for current resource
1.280     www      7600:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65      www      7601:               unless ($ids) { 
                   7602:                  $ids=$bighash{'ids_/'.$thisfn};
1.242     www      7603:               }
                   7604:               unless ($ids) {
                   7605: # alias?
                   7606: 		  $ids=$bighash{'mapalias_'.$thisfn};
1.65      www      7607:               }
1.37      www      7608:               if ($ids) {
                   7609: # ------------------------------------------------------------------- Has ID(s)
                   7610:                  my @possibilities=split(/\,/,$ids);
1.39      www      7611:                  if ($#possibilities==0) {
                   7612: # ----------------------------------------------- There is only one possibility
1.37      www      7613: 		     my ($mapid,$resid)=split(/\./,$ids);
1.626     albertel 7614: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
                   7615: 						    $resid,$thisfn);
1.249     www      7616:                  } elsif (!$donotrecurse) {
1.39      www      7617: # ------------------------------------------ There is more than one possibility
                   7618:                      my $realpossible=0;
1.800     albertel 7619:                      foreach my $id (@possibilities) {
                   7620: 			 my $file=$bighash{'src_'.$id};
1.39      www      7621:                          if (&allowed('bre',$file)) {
1.800     albertel 7622:          		    my ($mapid,$resid)=split(/\./,$id);
1.39      www      7623:                             if ($bighash{'map_type_'.$mapid} ne 'page') {
                   7624: 				$realpossible++;
1.626     albertel 7625:                                 $syval=&encode_symb($bighash{'map_id_'.$mapid},
                   7626: 						    $resid,$thisfn);
1.39      www      7627:                             }
                   7628: 			 }
1.191     harris41 7629:                      }
1.39      www      7630: 		     if ($realpossible!=1) { $syval=''; }
1.249     www      7631:                  } else {
                   7632:                      $syval='';
1.37      www      7633:                  }
                   7634: 	      }
                   7635:               untie(%bighash)
1.481     raeburn  7636:            }
1.31      www      7637:         }
1.62      www      7638:         if ($syval) {
1.620     albertel 7639: 	    return $env{$cache_str}=$syval;
1.62      www      7640:         }
1.31      www      7641:     }
1.949     raeburn  7642:     &appenv({'request.ambiguous' => $thisfn});
1.620     albertel 7643:     return $env{$cache_str}='';
1.31      www      7644: }
                   7645: 
                   7646: # ---------------------------------------------------------- Return random seed
                   7647: 
1.32      www      7648: sub numval {
                   7649:     my $txt=shift;
                   7650:     $txt=~tr/A-J/0-9/;
                   7651:     $txt=~tr/a-j/0-9/;
                   7652:     $txt=~tr/K-T/0-9/;
                   7653:     $txt=~tr/k-t/0-9/;
                   7654:     $txt=~tr/U-Z/0-5/;
                   7655:     $txt=~tr/u-z/0-5/;
                   7656:     $txt=~s/\D//g;
1.564     albertel 7657:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32      www      7658:     return int($txt);
1.368     albertel 7659: }
                   7660: 
1.484     albertel 7661: sub numval2 {
                   7662:     my $txt=shift;
                   7663:     $txt=~tr/A-J/0-9/;
                   7664:     $txt=~tr/a-j/0-9/;
                   7665:     $txt=~tr/K-T/0-9/;
                   7666:     $txt=~tr/k-t/0-9/;
                   7667:     $txt=~tr/U-Z/0-5/;
                   7668:     $txt=~tr/u-z/0-5/;
                   7669:     $txt=~s/\D//g;
                   7670:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   7671:     my $total;
                   7672:     foreach my $val (@txts) { $total+=$val; }
1.564     albertel 7673:     if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484     albertel 7674:     return int($total);
                   7675: }
                   7676: 
1.575     albertel 7677: sub numval3 {
                   7678:     use integer;
                   7679:     my $txt=shift;
                   7680:     $txt=~tr/A-J/0-9/;
                   7681:     $txt=~tr/a-j/0-9/;
                   7682:     $txt=~tr/K-T/0-9/;
                   7683:     $txt=~tr/k-t/0-9/;
                   7684:     $txt=~tr/U-Z/0-5/;
                   7685:     $txt=~tr/u-z/0-5/;
                   7686:     $txt=~s/\D//g;
                   7687:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   7688:     my $total;
                   7689:     foreach my $val (@txts) { $total+=$val; }
                   7690:     if ($_64bit) { $total=(($total<<32)>>32); }
                   7691:     return $total;
                   7692: }
                   7693: 
1.675     albertel 7694: sub digest {
                   7695:     my ($data)=@_;
                   7696:     my $digest=&Digest::MD5::md5($data);
                   7697:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
                   7698:     my ($e,$f);
                   7699:     {
                   7700:         use integer;
                   7701:         $e=($a+$b);
                   7702:         $f=($c+$d);
                   7703:         if ($_64bit) {
                   7704:             $e=(($e<<32)>>32);
                   7705:             $f=(($f<<32)>>32);
                   7706:         }
                   7707:     }
                   7708:     if (wantarray) {
                   7709: 	return ($e,$f);
                   7710:     } else {
                   7711: 	my $g;
                   7712: 	{
                   7713: 	    use integer;
                   7714: 	    $g=($e+$f);
                   7715: 	    if ($_64bit) {
                   7716: 		$g=(($g<<32)>>32);
                   7717: 	    }
                   7718: 	}
                   7719: 	return $g;
                   7720:     }
                   7721: }
                   7722: 
1.368     albertel 7723: sub latest_rnd_algorithm_id {
1.675     albertel 7724:     return '64bit5';
1.366     albertel 7725: }
1.32      www      7726: 
1.503     albertel 7727: sub get_rand_alg {
                   7728:     my ($courseid)=@_;
1.790     albertel 7729:     if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503     albertel 7730:     if ($courseid) {
1.620     albertel 7731: 	return $env{"course.$courseid.rndseed"};
1.503     albertel 7732:     }
                   7733:     return &latest_rnd_algorithm_id();
                   7734: }
                   7735: 
1.562     albertel 7736: sub validCODE {
                   7737:     my ($CODE)=@_;
                   7738:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
                   7739:     return 0;
                   7740: }
                   7741: 
1.491     albertel 7742: sub getCODE {
1.620     albertel 7743:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618     albertel 7744:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
                   7745: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
                   7746: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491     albertel 7747: 	return $Apache::lonhomework::history{'resource.CODE'};
                   7748:     }
                   7749:     return undef;
                   7750: }
                   7751: 
1.31      www      7752: sub rndseed {
1.155     albertel 7753:     my ($symb,$courseid,$domain,$username)=@_;
1.790     albertel 7754:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.896     albertel 7755:     if (!defined($symb)) {
1.366     albertel 7756: 	unless ($symb=$wsymb) { return time; }
                   7757:     }
                   7758:     if (!$courseid) { $courseid=$wcourseid; }
                   7759:     if (!$domain) { $domain=$wdomain; }
                   7760:     if (!$username) { $username=$wusername }
1.503     albertel 7761:     my $which=&get_rand_alg();
1.803     albertel 7762: 
1.491     albertel 7763:     if (defined(&getCODE())) {
1.675     albertel 7764: 	if ($which eq '64bit5') {
                   7765: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
                   7766: 	} elsif ($which eq '64bit4') {
1.575     albertel 7767: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
                   7768: 	} else {
                   7769: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
                   7770: 	}
1.675     albertel 7771:     } elsif ($which eq '64bit5') {
                   7772: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575     albertel 7773:     } elsif ($which eq '64bit4') {
                   7774: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501     albertel 7775:     } elsif ($which eq '64bit3') {
                   7776: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443     albertel 7777:     } elsif ($which eq '64bit2') {
                   7778: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366     albertel 7779:     } elsif ($which eq '64bit') {
                   7780: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
                   7781:     }
                   7782:     return &rndseed_32bit($symb,$courseid,$domain,$username);
                   7783: }
                   7784: 
                   7785: sub rndseed_32bit {
                   7786:     my ($symb,$courseid,$domain,$username)=@_;
                   7787:     {
                   7788: 	use integer;
                   7789: 	my $symbchck=unpack("%32C*",$symb) << 27;
                   7790: 	my $symbseed=numval($symb) << 22;
                   7791: 	my $namechck=unpack("%32C*",$username) << 17;
                   7792: 	my $nameseed=numval($username) << 12;
                   7793: 	my $domainseed=unpack("%32C*",$domain) << 7;
                   7794: 	my $courseseed=unpack("%32C*",$courseid);
                   7795: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790     albertel 7796: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   7797: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 7798: 	if ($_64bit) { $num=(($num<<32)>>32); }
1.366     albertel 7799: 	return $num;
                   7800:     }
                   7801: }
                   7802: 
                   7803: sub rndseed_64bit {
                   7804:     my ($symb,$courseid,$domain,$username)=@_;
                   7805:     {
                   7806: 	use integer;
                   7807: 	my $symbchck=unpack("%32S*",$symb) << 21;
                   7808: 	my $symbseed=numval($symb) << 10;
                   7809: 	my $namechck=unpack("%32S*",$username);
                   7810: 	
                   7811: 	my $nameseed=numval($username) << 21;
                   7812: 	my $domainseed=unpack("%32S*",$domain) << 10;
                   7813: 	my $courseseed=unpack("%32S*",$courseid);
                   7814: 	
                   7815: 	my $num1=$symbchck+$symbseed+$namechck;
                   7816: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 7817: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   7818: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 7819: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366     albertel 7820: 	return "$num1,$num2";
1.155     albertel 7821:     }
1.366     albertel 7822: }
                   7823: 
1.443     albertel 7824: sub rndseed_64bit2 {
                   7825:     my ($symb,$courseid,$domain,$username)=@_;
                   7826:     {
                   7827: 	use integer;
                   7828: 	# strings need to be an even # of cahracters long, it it is odd the
                   7829:         # last characters gets thrown away
                   7830: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   7831: 	my $symbseed=numval($symb) << 10;
                   7832: 	my $namechck=unpack("%32S*",$username.' ');
                   7833: 	
                   7834: 	my $nameseed=numval($username) << 21;
1.501     albertel 7835: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   7836: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   7837: 	
                   7838: 	my $num1=$symbchck+$symbseed+$namechck;
                   7839: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 7840: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   7841: 	#&logthis("rndseed :$num:$symb");
1.803     albertel 7842: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501     albertel 7843: 	return "$num1,$num2";
                   7844:     }
                   7845: }
                   7846: 
                   7847: sub rndseed_64bit3 {
                   7848:     my ($symb,$courseid,$domain,$username)=@_;
                   7849:     {
                   7850: 	use integer;
                   7851: 	# strings need to be an even # of cahracters long, it it is odd the
                   7852:         # last characters gets thrown away
                   7853: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   7854: 	my $symbseed=numval2($symb) << 10;
                   7855: 	my $namechck=unpack("%32S*",$username.' ');
                   7856: 	
                   7857: 	my $nameseed=numval2($username) << 21;
1.443     albertel 7858: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   7859: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   7860: 	
                   7861: 	my $num1=$symbchck+$symbseed+$namechck;
                   7862: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 7863: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   7864: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.564     albertel 7865: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
                   7866: 	
1.503     albertel 7867: 	return "$num1:$num2";
1.443     albertel 7868:     }
                   7869: }
                   7870: 
1.575     albertel 7871: sub rndseed_64bit4 {
                   7872:     my ($symb,$courseid,$domain,$username)=@_;
                   7873:     {
                   7874: 	use integer;
                   7875: 	# strings need to be an even # of cahracters long, it it is odd the
                   7876:         # last characters gets thrown away
                   7877: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   7878: 	my $symbseed=numval3($symb) << 10;
                   7879: 	my $namechck=unpack("%32S*",$username.' ');
                   7880: 	
                   7881: 	my $nameseed=numval3($username) << 21;
                   7882: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   7883: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   7884: 	
                   7885: 	my $num1=$symbchck+$symbseed+$namechck;
                   7886: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 7887: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   7888: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.575     albertel 7889: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
                   7890: 	
                   7891: 	return "$num1:$num2";
                   7892:     }
                   7893: }
                   7894: 
1.675     albertel 7895: sub rndseed_64bit5 {
                   7896:     my ($symb,$courseid,$domain,$username)=@_;
                   7897:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
                   7898:     return "$num1:$num2";
                   7899: }
                   7900: 
1.366     albertel 7901: sub rndseed_CODE_64bit {
                   7902:     my ($symb,$courseid,$domain,$username)=@_;
1.155     albertel 7903:     {
1.366     albertel 7904: 	use integer;
1.443     albertel 7905: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484     albertel 7906: 	my $symbseed=numval2($symb);
1.491     albertel 7907: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   7908: 	my $CODEseed=numval(&getCODE());
1.443     albertel 7909: 	my $courseseed=unpack("%32S*",$courseid.' ');
1.484     albertel 7910: 	my $num1=$symbseed+$CODEchck;
                   7911: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 7912: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   7913: 	#&logthis("rndseed :$num1:$num2:$symb");
1.564     albertel 7914: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   7915: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503     albertel 7916: 	return "$num1:$num2";
1.366     albertel 7917:     }
                   7918: }
                   7919: 
1.575     albertel 7920: sub rndseed_CODE_64bit4 {
                   7921:     my ($symb,$courseid,$domain,$username)=@_;
                   7922:     {
                   7923: 	use integer;
                   7924: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
                   7925: 	my $symbseed=numval3($symb);
                   7926: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   7927: 	my $CODEseed=numval3(&getCODE());
                   7928: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   7929: 	my $num1=$symbseed+$CODEchck;
                   7930: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 7931: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   7932: 	#&logthis("rndseed :$num1:$num2:$symb");
1.575     albertel 7933: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   7934: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
                   7935: 	return "$num1:$num2";
                   7936:     }
                   7937: }
                   7938: 
1.675     albertel 7939: sub rndseed_CODE_64bit5 {
                   7940:     my ($symb,$courseid,$domain,$username)=@_;
                   7941:     my $code = &getCODE();
                   7942:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
                   7943:     return "$num1:$num2";
                   7944: }
                   7945: 
1.366     albertel 7946: sub setup_random_from_rndseed {
                   7947:     my ($rndseed)=@_;
1.503     albertel 7948:     if ($rndseed =~/([,:])/) {
                   7949: 	my ($num1,$num2)=split(/[,:]/,$rndseed);
1.366     albertel 7950: 	&Math::Random::random_set_seed(abs($num1),abs($num2));
                   7951:     } else {
                   7952: 	&Math::Random::random_set_seed_from_phrase($rndseed);
1.98      albertel 7953:     }
1.36      albertel 7954: }
                   7955: 
1.474     albertel 7956: sub latest_receipt_algorithm_id {
1.835     albertel 7957:     return 'receipt3';
1.474     albertel 7958: }
                   7959: 
1.480     www      7960: sub recunique {
                   7961:     my $fucourseid=shift;
                   7962:     my $unique;
1.835     albertel 7963:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
                   7964: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 7965: 	$unique=$env{"course.$fucourseid.internal.encseed"};
1.480     www      7966:     } else {
                   7967: 	$unique=$perlvar{'lonReceipt'};
                   7968:     }
                   7969:     return unpack("%32C*",$unique);
                   7970: }
                   7971: 
                   7972: sub recprefix {
                   7973:     my $fucourseid=shift;
                   7974:     my $prefix;
1.835     albertel 7975:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
                   7976: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 7977: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
1.480     www      7978:     } else {
                   7979: 	$prefix=$perlvar{'lonHostID'};
                   7980:     }
                   7981:     return unpack("%32C*",$prefix);
                   7982: }
                   7983: 
1.76      www      7984: sub ireceipt {
1.474     albertel 7985:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835     albertel 7986: 
                   7987:     my $return =&recprefix($fucourseid).'-';
                   7988: 
                   7989:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
                   7990: 	$env{'request.state'} eq 'construct') {
                   7991: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
                   7992: 	return $return;
                   7993:     }
                   7994: 
1.76      www      7995:     my $cuname=unpack("%32C*",$funame);
                   7996:     my $cudom=unpack("%32C*",$fudom);
                   7997:     my $cucourseid=unpack("%32C*",$fucourseid);
                   7998:     my $cusymb=unpack("%32C*",$fusymb);
1.480     www      7999:     my $cunique=&recunique($fucourseid);
1.474     albertel 8000:     my $cpart=unpack("%32S*",$part);
1.835     albertel 8001:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
                   8002: 
1.790     albertel 8003: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474     albertel 8004: 			       
                   8005: 	$return.= ($cunique%$cuname+
                   8006: 		   $cunique%$cudom+
                   8007: 		   $cusymb%$cuname+
                   8008: 		   $cusymb%$cudom+
                   8009: 		   $cucourseid%$cuname+
                   8010: 		   $cucourseid%$cudom+
                   8011: 		   $cpart%$cuname+
                   8012: 		   $cpart%$cudom);
                   8013:     } else {
                   8014: 	$return.= ($cunique%$cuname+
                   8015: 		   $cunique%$cudom+
                   8016: 		   $cusymb%$cuname+
                   8017: 		   $cusymb%$cudom+
                   8018: 		   $cucourseid%$cuname+
                   8019: 		   $cucourseid%$cudom);
                   8020:     }
                   8021:     return $return;
1.76      www      8022: }
                   8023: 
                   8024: sub receipt {
1.474     albertel 8025:     my ($part)=@_;
1.790     albertel 8026:     my ($symb,$courseid,$domain,$name) = &whichuser();
1.474     albertel 8027:     return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76      www      8028: }
1.260     ng       8029: 
1.790     albertel 8030: sub whichuser {
                   8031:     my ($passedsymb)=@_;
                   8032:     my ($symb,$courseid,$domain,$name,$publicuser);
                   8033:     if (defined($env{'form.grade_symb'})) {
                   8034: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
                   8035: 	my $allowed=&allowed('vgr',$tmp_courseid);
                   8036: 	if (!$allowed &&
                   8037: 	    exists($env{'request.course.sec'}) &&
                   8038: 	    $env{'request.course.sec'} !~ /^\s*$/) {
                   8039: 	    $allowed=&allowed('vgr',$tmp_courseid.
                   8040: 			      '/'.$env{'request.course.sec'});
                   8041: 	}
                   8042: 	if ($allowed) {
                   8043: 	    ($symb)=&get_env_multiple('form.grade_symb');
                   8044: 	    $courseid=$tmp_courseid;
                   8045: 	    ($domain)=&get_env_multiple('form.grade_domain');
                   8046: 	    ($name)=&get_env_multiple('form.grade_username');
                   8047: 	    return ($symb,$courseid,$domain,$name,$publicuser);
                   8048: 	}
                   8049:     }
                   8050:     if (!$passedsymb) {
                   8051: 	$symb=&symbread();
                   8052:     } else {
                   8053: 	$symb=$passedsymb;
                   8054:     }
                   8055:     $courseid=$env{'request.course.id'};
                   8056:     $domain=$env{'user.domain'};
                   8057:     $name=$env{'user.name'};
                   8058:     if ($name eq 'public' && $domain eq 'public') {
                   8059: 	if (!defined($env{'form.username'})) {
                   8060: 	    $env{'form.username'}.=time.rand(10000000);
                   8061: 	}
                   8062: 	$name.=$env{'form.username'};
                   8063:     }
                   8064:     return ($symb,$courseid,$domain,$name,$publicuser);
                   8065: 
                   8066: }
                   8067: 
1.36      albertel 8068: # ------------------------------------------------------------ Serves up a file
1.472     albertel 8069: # returns either the contents of the file or 
                   8070: # -1 if the file doesn't exist
1.481     raeburn  8071: #
                   8072: # if the target is a file that was uploaded via DOCS, 
                   8073: # a check will be made to see if a current copy exists on the local server,
                   8074: # if it does this will be served, otherwise a copy will be retrieved from
                   8075: # the home server for the course and stored in /home/httpd/html/userfiles on
                   8076: # the local server.   
1.472     albertel 8077: 
1.36      albertel 8078: sub getfile {
1.538     albertel 8079:     my ($file) = @_;
1.609     banghart 8080:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538     albertel 8081:     &repcopy($file);
                   8082:     return &readfile($file);
                   8083: }
                   8084: 
                   8085: sub repcopy_userfile {
                   8086:     my ($file)=@_;
1.609     banghart 8087:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.610     albertel 8088:     if ($file =~ m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1.538     albertel 8089:     my ($cdom,$cnum,$filename) = 
1.811     albertel 8090: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538     albertel 8091:     my $uri="/uploaded/$cdom/$cnum/$filename";
                   8092:     if (-e "$file") {
1.828     www      8093: # we already have a local copy, check it out
1.538     albertel 8094: 	my @fileinfo = stat($file);
1.828     www      8095: 	my $rtncode;
                   8096: 	my $info;
1.538     albertel 8097: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482     albertel 8098: 	if ($lwpresp ne 'ok') {
1.828     www      8099: # there is no such file anymore, even though we had a local copy
1.482     albertel 8100: 	    if ($rtncode eq '404') {
1.538     albertel 8101: 		unlink($file);
1.482     albertel 8102: 	    }
                   8103: 	    return -1;
                   8104: 	}
                   8105: 	if ($info < $fileinfo[9]) {
1.828     www      8106: # nice, the file we have is up-to-date, just say okay
1.607     raeburn  8107: 	    return 'ok';
1.828     www      8108: 	} else {
                   8109: # the file is outdated, get rid of it
                   8110: 	    unlink($file);
1.482     albertel 8111: 	}
1.828     www      8112:     }
                   8113: # one way or the other, at this point, we don't have the file
                   8114: # construct the correct path for the file
                   8115:     my @parts = ($cdom,$cnum); 
                   8116:     if ($filename =~ m|^(.+)/[^/]+$|) {
                   8117: 	push @parts, split(/\//,$1);
                   8118:     }
                   8119:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
                   8120:     foreach my $part (@parts) {
                   8121: 	$path .= '/'.$part;
                   8122: 	if (!-e $path) {
                   8123: 	    mkdir($path,0770);
1.482     albertel 8124: 	}
                   8125:     }
1.828     www      8126: # now the path exists for sure
                   8127: # get a user agent
                   8128:     my $ua=new LWP::UserAgent;
                   8129:     my $transferfile=$file.'.in.transfer';
                   8130: # FIXME: this should flock
                   8131:     if (-e $transferfile) { return 'ok'; }
                   8132:     my $request;
                   8133:     $uri=~s/^\///;
1.838     albertel 8134:     $request=new HTTP::Request('GET','http://'.&hostname(&homeserver($cnum,$cdom)).'/raw/'.$uri);
1.828     www      8135:     my $response=$ua->request($request,$transferfile);
                   8136: # did it work?
                   8137:     if ($response->is_error()) {
                   8138: 	unlink($transferfile);
                   8139: 	&logthis("Userfile repcopy failed for $uri");
                   8140: 	return -1;
                   8141:     }
                   8142: # worked, rename the transfer file
                   8143:     rename($transferfile,$file);
1.607     raeburn  8144:     return 'ok';
1.481     raeburn  8145: }
                   8146: 
1.517     albertel 8147: sub tokenwrapper {
                   8148:     my $uri=shift;
1.552     albertel 8149:     $uri=~s|^http\://([^/]+)||;
                   8150:     $uri=~s|^/||;
1.620     albertel 8151:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517     albertel 8152:     my $token=$1;
1.552     albertel 8153:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
                   8154:     if ($udom && $uname && $file) {
                   8155: 	$file=~s|(\?\.*)*$||;
1.949     raeburn  8156:         &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
1.838     albertel 8157:         return 'http://'.&hostname(&homeserver($uname,$udom)).'/'.$uri.
1.517     albertel 8158:                (($uri=~/\?/)?'&':'?').'token='.$token.
                   8159:                                '&tokenissued='.$perlvar{'lonHostID'};
                   8160:     } else {
                   8161:         return '/adm/notfound.html';
                   8162:     }
                   8163: }
                   8164: 
1.828     www      8165: # call with reqtype HEAD: get last modification time
                   8166: # call with reqtype GET: get the file contents
                   8167: # Do not call this with reqtype GET for large files! It loads everything into memory
                   8168: #
1.481     raeburn  8169: sub getuploaded {
                   8170:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
                   8171:     $uri=~s/^\///;
1.838     albertel 8172:     $uri = 'http://'.&hostname(&homeserver($cnum,$cdom)).'/raw/'.$uri;
1.481     raeburn  8173:     my $ua=new LWP::UserAgent;
                   8174:     my $request=new HTTP::Request($reqtype,$uri);
                   8175:     my $response=$ua->request($request);
                   8176:     $$rtncode = $response->code;
1.482     albertel 8177:     if (! $response->is_success()) {
                   8178: 	return 'failed';
                   8179:     }      
                   8180:     if ($reqtype eq 'HEAD') {
1.486     www      8181: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482     albertel 8182:     } elsif ($reqtype eq 'GET') {
                   8183: 	$$info = $response->content;
1.472     albertel 8184:     }
1.482     albertel 8185:     return 'ok';
1.36      albertel 8186: }
                   8187: 
1.481     raeburn  8188: sub readfile {
                   8189:     my $file = shift;
                   8190:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
                   8191:     my $fh;
                   8192:     open($fh,"<$file");
                   8193:     my $a='';
1.800     albertel 8194:     while (my $line = <$fh>) { $a .= $line; }
1.481     raeburn  8195:     return $a;
                   8196: }
                   8197: 
1.36      albertel 8198: sub filelocation {
1.590     banghart 8199:     my ($dir,$file) = @_;
                   8200:     my $location;
                   8201:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700     albertel 8202: 
                   8203:     if ($file =~ m-^/adm/-) {
                   8204: 	$file=~s-^/adm/wrapper/-/-;
                   8205: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
                   8206:     }
1.882     albertel 8207: 
1.590     banghart 8208:     if ($file=~m:^/~:) { # is a contruction space reference
                   8209:         $location = $file;
                   8210:         $location =~ s:/~(.*?)/(.*):/home/$1/public_html/$2:;
1.807     albertel 8211:     } elsif ($file=~m{^/home/$match_username/public_html/}) {
1.649     albertel 8212: 	# is a correct contruction space reference
                   8213:         $location = $file;
1.956     raeburn  8214:     } elsif ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
                   8215:         $location = $file;
1.609     banghart 8216:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590     banghart 8217:         my ($udom,$uname,$filename)=
1.811     albertel 8218:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590     banghart 8219:         my $home=&homeserver($uname,$udom);
                   8220:         my $is_me=0;
                   8221:         my @ids=&current_machine_ids();
                   8222:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
                   8223:         if ($is_me) {
1.955     raeburn  8224:   	    $location=&propath($udom,$uname).'/userfiles/'.$filename;
1.590     banghart 8225:         } else {
                   8226:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
                   8227:   	      $udom.'/'.$uname.'/'.$filename;
                   8228:         }
1.882     albertel 8229:     } elsif ($file =~ m-^/adm/-) {
                   8230: 	$location = $perlvar{'lonDocRoot'}.'/'.$file;
1.590     banghart 8231:     } else {
                   8232:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
                   8233:         $file=~s:^/res/:/:;
                   8234:         if ( !( $file =~ m:^/:) ) {
                   8235:             $location = $dir. '/'.$file;
                   8236:         } else {
                   8237:             $location = '/home/httpd/html/res'.$file;
                   8238:         }
1.59      albertel 8239:     }
1.590     banghart 8240:     $location=~s://+:/:g; # remove duplicate /
1.930     albertel 8241:     while ($location=~m{/\.\./}) {
                   8242: 	if ($location =~ m{/[^/]+/\.\./}) {
                   8243: 	    $location=~ s{/[^/]+/\.\./}{/}g;
                   8244: 	} else {
                   8245: 	    $location=~ s{/\.\./}{/}g;
                   8246: 	}
                   8247:     } #remove dir/..
1.590     banghart 8248:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
                   8249:     return $location;
1.46      www      8250: }
1.36      albertel 8251: 
1.46      www      8252: sub hreflocation {
                   8253:     my ($dir,$file)=@_;
1.460     albertel 8254:     unless (($file=~m-^http://-i) || ($file=~m-^/-)) {
1.666     albertel 8255: 	$file=filelocation($dir,$file);
1.700     albertel 8256:     } elsif ($file=~m-^/adm/-) {
                   8257: 	$file=~s-^/adm/wrapper/-/-;
                   8258: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
1.666     albertel 8259:     }
                   8260:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
                   8261: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
1.807     albertel 8262:     } elsif ($file=~m-/home/($match_username)/public_html/-) {
                   8263: 	$file=~s-^/home/($match_username)/public_html/-/~$1/-;
1.666     albertel 8264:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.811     albertel 8265: 	$file=~s-^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/
1.666     albertel 8266: 	    -/uploaded/$1/$2/-x;
1.46      www      8267:     }
1.913     albertel 8268:     if ($file=~ m{^/userfiles/}) {
                   8269: 	$file =~ s{^/userfiles/}{/uploaded/};
                   8270:     }
1.462     albertel 8271:     return $file;
1.465     albertel 8272: }
                   8273: 
                   8274: sub current_machine_domains {
1.853     albertel 8275:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
                   8276: }
                   8277: 
                   8278: sub machine_domains {
                   8279:     my ($hostname) = @_;
1.465     albertel 8280:     my @domains;
1.838     albertel 8281:     my %hostname = &all_hostnames();
1.465     albertel 8282:     while( my($id, $name) = each(%hostname)) {
1.467     matthew  8283: #	&logthis("-$id-$name-$hostname-");
1.465     albertel 8284: 	if ($hostname eq $name) {
1.844     albertel 8285: 	    push(@domains,&host_domain($id));
1.465     albertel 8286: 	}
                   8287:     }
                   8288:     return @domains;
                   8289: }
                   8290: 
                   8291: sub current_machine_ids {
1.853     albertel 8292:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
                   8293: }
                   8294: 
                   8295: sub machine_ids {
                   8296:     my ($hostname) = @_;
                   8297:     $hostname ||= &hostname($perlvar{'lonHostID'});
1.465     albertel 8298:     my @ids;
1.888     albertel 8299:     my %name_to_host = &all_names();
1.889     albertel 8300:     if (ref($name_to_host{$hostname}) eq 'ARRAY') {
                   8301: 	return @{ $name_to_host{$hostname} };
                   8302:     }
                   8303:     return;
1.31      www      8304: }
                   8305: 
1.824     raeburn  8306: sub additional_machine_domains {
                   8307:     my @domains;
                   8308:     open(my $fh,"<$perlvar{'lonTabDir'}/expected_domains.tab");
                   8309:     while( my $line = <$fh>) {
                   8310:         $line =~ s/\s//g;
                   8311:         push(@domains,$line);
                   8312:     }
                   8313:     return @domains;
                   8314: }
                   8315: 
                   8316: sub default_login_domain {
                   8317:     my $domain = $perlvar{'lonDefDomain'};
                   8318:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
                   8319:     foreach my $posdom (&current_machine_domains(),
                   8320:                         &additional_machine_domains()) {
                   8321:         if (lc($posdom) eq lc($testdomain)) {
                   8322:             $domain=$posdom;
                   8323:             last;
                   8324:         }
                   8325:     }
                   8326:     return $domain;
                   8327: }
                   8328: 
1.31      www      8329: # ------------------------------------------------------------- Declutters URLs
                   8330: 
                   8331: sub declutter {
                   8332:     my $thisfn=shift;
1.569     albertel 8333:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.479     albertel 8334:     $thisfn=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.31      www      8335:     $thisfn=~s/^\///;
1.697     albertel 8336:     $thisfn=~s|^adm/wrapper/||;
                   8337:     $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31      www      8338:     $thisfn=~s/^res\///;
1.235     www      8339:     $thisfn=~s/\?.+$//;
1.268     www      8340:     return $thisfn;
                   8341: }
                   8342: 
                   8343: # ------------------------------------------------------------- Clutter up URLs
                   8344: 
                   8345: sub clutter {
                   8346:     my $thisfn='/'.&declutter(shift);
1.887     albertel 8347:     if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
1.884     albertel 8348: 	|| $thisfn =~ m{^/adm/(includes|pages)} ) { 
1.270     www      8349:        $thisfn='/res'.$thisfn; 
                   8350:     }
1.694     albertel 8351:     if ($thisfn !~m|/adm|) {
1.695     albertel 8352: 	if ($thisfn =~ m|/ext/|) {
1.694     albertel 8353: 	    $thisfn='/adm/wrapper'.$thisfn;
1.695     albertel 8354: 	} else {
                   8355: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
                   8356: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698     albertel 8357: 	    if ($embstyle eq 'ssi'
                   8358: 		|| ($embstyle eq 'hdn')
                   8359: 		|| ($embstyle eq 'rat')
                   8360: 		|| ($embstyle eq 'prv')
                   8361: 		|| ($embstyle eq 'ign')) {
                   8362: 		#do nothing with these
                   8363: 	    } elsif (($embstyle eq 'img') 
1.695     albertel 8364: 		|| ($embstyle eq 'emb')
                   8365: 		|| ($embstyle eq 'wrp')) {
                   8366: 		$thisfn='/adm/wrapper'.$thisfn;
1.698     albertel 8367: 	    } elsif ($embstyle eq 'unk'
                   8368: 		     && $thisfn!~/\.(sequence|page)$/) {
1.695     albertel 8369: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698     albertel 8370: 	    } else {
1.718     www      8371: #		&logthis("Got a blank emb style");
1.695     albertel 8372: 	    }
1.694     albertel 8373: 	}
                   8374:     }
1.31      www      8375:     return $thisfn;
1.12      www      8376: }
                   8377: 
1.787     albertel 8378: sub clutter_with_no_wrapper {
                   8379:     my $uri = &clutter(shift);
                   8380:     if ($uri =~ m-^/adm/-) {
                   8381: 	$uri =~ s-^/adm/wrapper/-/-;
                   8382: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
                   8383:     }
                   8384:     return $uri;
                   8385: }
                   8386: 
1.557     albertel 8387: sub freeze_escape {
                   8388:     my ($value)=@_;
                   8389:     if (ref($value)) {
                   8390: 	$value=&nfreeze($value);
                   8391: 	return '__FROZEN__'.&escape($value);
                   8392:     }
                   8393:     return &escape($value);
                   8394: }
                   8395: 
1.11      www      8396: 
1.557     albertel 8397: sub thaw_unescape {
                   8398:     my ($value)=@_;
                   8399:     if ($value =~ /^__FROZEN__/) {
                   8400: 	substr($value,0,10,undef);
                   8401: 	$value=&unescape($value);
                   8402: 	return &thaw($value);
                   8403:     }
                   8404:     return &unescape($value);
                   8405: }
                   8406: 
1.436     albertel 8407: sub correct_line_ends {
                   8408:     my ($result)=@_;
                   8409:     $$result =~s/\r\n/\n/mg;
                   8410:     $$result =~s/\r/\n/mg;
1.415     albertel 8411: }
1.1       albertel 8412: # ================================================================ Main Program
                   8413: 
1.184     www      8414: sub goodbye {
1.204     albertel 8415:    &logthis("Starting Shut down");
1.443     albertel 8416: #not converted to using infrastruture and probably shouldn't be
1.870     albertel 8417:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
1.443     albertel 8418: #converted
1.599     albertel 8419: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
1.870     albertel 8420:    &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
                   8421: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
                   8422: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
1.425     albertel 8423: #1.1 only
1.870     albertel 8424: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
                   8425: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
                   8426: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
                   8427: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
                   8428:    &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
1.599     albertel 8429:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
                   8430:    &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184     www      8431:    &flushcourselogs();
                   8432:    &logthis("Shutting down");
                   8433: }
                   8434: 
1.852     albertel 8435: sub get_dns {
1.869     albertel 8436:     my ($url,$func,$ignore_cache) = @_;
                   8437:     if (!$ignore_cache) {
                   8438: 	my ($content,$cached)=
                   8439: 	    &Apache::lonnet::is_cached_new('dns',$url);
                   8440: 	if ($cached) {
                   8441: 	    &$func($content);
                   8442: 	    return;
                   8443: 	}
                   8444:     }
                   8445: 
                   8446:     my %alldns;
1.852     albertel 8447:     open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
                   8448:     foreach my $dns (<$config>) {
                   8449: 	next if ($dns !~ /^\^(\S*)/x);
1.869     albertel 8450: 	$alldns{$1} = 1;
                   8451:     }
                   8452:     while (%alldns) {
                   8453: 	my ($dns) = keys(%alldns);
                   8454: 	delete($alldns{$dns});
1.852     albertel 8455: 	my $ua=new LWP::UserAgent;
                   8456: 	my $request=new HTTP::Request('GET',"http://$dns$url");
                   8457: 	my $response=$ua->request($request);
                   8458: 	next if ($response->is_error());
                   8459: 	my @content = split("\n",$response->content);
1.869     albertel 8460: 	&Apache::lonnet::do_cache_new('dns',$url,\@content,30*24*60*60);
1.852     albertel 8461: 	&$func(\@content);
1.869     albertel 8462: 	return;
1.852     albertel 8463:     }
                   8464:     close($config);
1.871     albertel 8465:     my $which = (split('/',$url))[3];
                   8466:     &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
                   8467:     open($config,"<$perlvar{'lonTabDir'}/dns_$which.tab");
1.869     albertel 8468:     my @content = <$config>;
                   8469:     &$func(\@content);
                   8470:     return;
1.852     albertel 8471: }
1.327     albertel 8472: # ------------------------------------------------------------ Read domain file
                   8473: {
1.852     albertel 8474:     my $loaded;
1.846     albertel 8475:     my %domain;
                   8476: 
1.852     albertel 8477:     sub parse_domain_tab {
                   8478: 	my ($lines) = @_;
                   8479: 	foreach my $line (@$lines) {
                   8480: 	    next if ($line =~ /^(\#|\s*$ )/x);
1.403     www      8481: 
1.846     albertel 8482: 	    chomp($line);
1.852     albertel 8483: 	    my ($name,@elements) = split(/:/,$line,9);
1.846     albertel 8484: 	    my %this_domain;
                   8485: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
                   8486: 			       'lang_def', 'city', 'longi', 'lati',
                   8487: 			       'primary') {
                   8488: 		$this_domain{$field} = shift(@elements);
                   8489: 	    }
                   8490: 	    $domain{$name} = \%this_domain;
1.852     albertel 8491: 	}
                   8492:     }
1.864     albertel 8493: 
                   8494:     sub reset_domain_info {
                   8495: 	undef($loaded);
                   8496: 	undef(%domain);
                   8497:     }
                   8498: 
1.852     albertel 8499:     sub load_domain_tab {
1.869     albertel 8500: 	my ($ignore_cache) = @_;
                   8501: 	&get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache);
1.852     albertel 8502: 	my $fh;
                   8503: 	if (open($fh,"<".$perlvar{'lonTabDir'}.'/domain.tab')) {
                   8504: 	    my @lines = <$fh>;
                   8505: 	    &parse_domain_tab(\@lines);
1.448     albertel 8506: 	}
1.852     albertel 8507: 	close($fh);
                   8508: 	$loaded = 1;
1.327     albertel 8509:     }
1.846     albertel 8510: 
                   8511:     sub domain {
1.852     albertel 8512: 	&load_domain_tab() if (!$loaded);
                   8513: 
1.846     albertel 8514: 	my ($name,$what) = @_;
                   8515: 	return if ( !exists($domain{$name}) );
                   8516: 
                   8517: 	if (!$what) {
                   8518: 	    return $domain{$name}{'description'};
                   8519: 	}
                   8520: 	return $domain{$name}{$what};
                   8521:     }
1.327     albertel 8522: }
                   8523: 
                   8524: 
1.1       albertel 8525: # ------------------------------------------------------------- Read hosts file
                   8526: {
1.838     albertel 8527:     my %hostname;
1.844     albertel 8528:     my %hostdom;
1.845     albertel 8529:     my %libserv;
1.852     albertel 8530:     my $loaded;
1.888     albertel 8531:     my %name_to_host;
1.852     albertel 8532: 
                   8533:     sub parse_hosts_tab {
                   8534: 	my ($file) = @_;
                   8535: 	foreach my $configline (@$file) {
                   8536: 	    next if ($configline =~ /^(\#|\s*$ )/x);
                   8537: 	    next if ($configline =~ /^\^/);
                   8538: 	    chomp($configline);
                   8539: 	    my ($id,$domain,$role,$name)=split(/:/,$configline);
                   8540: 	    $name=~s/\s//g;
                   8541: 	    if ($id && $domain && $role && $name) {
                   8542: 		$hostname{$id}=$name;
1.888     albertel 8543: 		push(@{$name_to_host{$name}}, $id);
1.852     albertel 8544: 		$hostdom{$id}=$domain;
                   8545: 		if ($role eq 'library') { $libserv{$id}=$name; }
                   8546: 	    }
                   8547: 	}
                   8548:     }
1.864     albertel 8549:     
                   8550:     sub reset_hosts_info {
1.897     albertel 8551: 	&purge_remembered();
1.864     albertel 8552: 	&reset_domain_info();
                   8553: 	&reset_hosts_ip_info();
1.892     albertel 8554: 	undef(%name_to_host);
1.864     albertel 8555: 	undef(%hostname);
                   8556: 	undef(%hostdom);
                   8557: 	undef(%libserv);
                   8558: 	undef($loaded);
                   8559:     }
1.1       albertel 8560: 
1.852     albertel 8561:     sub load_hosts_tab {
1.869     albertel 8562: 	my ($ignore_cache) = @_;
                   8563: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache);
1.852     albertel 8564: 	open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
                   8565: 	my @config = <$config>;
                   8566: 	&parse_hosts_tab(\@config);
                   8567: 	close($config);
                   8568: 	$loaded=1;
1.1       albertel 8569:     }
1.852     albertel 8570: 
1.838     albertel 8571:     sub hostname {
1.852     albertel 8572: 	&load_hosts_tab() if (!$loaded);
                   8573: 
1.838     albertel 8574: 	my ($lonid) = @_;
                   8575: 	return $hostname{$lonid};
                   8576:     }
1.845     albertel 8577: 
1.838     albertel 8578:     sub all_hostnames {
1.852     albertel 8579: 	&load_hosts_tab() if (!$loaded);
                   8580: 
1.838     albertel 8581: 	return %hostname;
                   8582:     }
1.845     albertel 8583: 
1.888     albertel 8584:     sub all_names {
                   8585: 	&load_hosts_tab() if (!$loaded);
                   8586: 
                   8587: 	return %name_to_host;
                   8588:     }
                   8589: 
1.845     albertel 8590:     sub is_library {
1.852     albertel 8591: 	&load_hosts_tab() if (!$loaded);
                   8592: 
1.845     albertel 8593: 	return exists($libserv{$_[0]});
                   8594:     }
                   8595: 
                   8596:     sub all_library {
1.852     albertel 8597: 	&load_hosts_tab() if (!$loaded);
                   8598: 
1.845     albertel 8599: 	return %libserv;
                   8600:     }
                   8601: 
1.841     albertel 8602:     sub get_servers {
1.852     albertel 8603: 	&load_hosts_tab() if (!$loaded);
                   8604: 
1.841     albertel 8605: 	my ($domain,$type) = @_;
                   8606: 	my %possible_hosts = ($type eq 'library') ? %libserv
                   8607: 	                                          : %hostname;
                   8608: 	my %result;
1.842     albertel 8609: 	if (ref($domain) eq 'ARRAY') {
                   8610: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843     albertel 8611: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842     albertel 8612: 		    $result{$host} = $hostname;
                   8613: 		}
                   8614: 	    }
                   8615: 	} else {
                   8616: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
                   8617: 		if ($hostdom{$host} eq $domain) {
                   8618: 		    $result{$host} = $hostname;
                   8619: 		}
1.841     albertel 8620: 	    }
                   8621: 	}
                   8622: 	return %result;
                   8623:     }
1.845     albertel 8624: 
1.844     albertel 8625:     sub host_domain {
1.852     albertel 8626: 	&load_hosts_tab() if (!$loaded);
                   8627: 
1.844     albertel 8628: 	my ($lonid) = @_;
                   8629: 	return $hostdom{$lonid};
                   8630:     }
                   8631: 
1.841     albertel 8632:     sub all_domains {
1.852     albertel 8633: 	&load_hosts_tab() if (!$loaded);
                   8634: 
1.841     albertel 8635: 	my %seen;
                   8636: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
                   8637: 	return @uniq;
                   8638:     }
1.1       albertel 8639: }
                   8640: 
1.847     albertel 8641: { 
                   8642:     my %iphost;
1.856     albertel 8643:     my %name_to_ip;
                   8644:     my %lonid_to_ip;
1.869     albertel 8645: 
1.847     albertel 8646:     sub get_hosts_from_ip {
                   8647: 	my ($ip) = @_;
                   8648: 	my %iphosts = &get_iphost();
                   8649: 	if (ref($iphosts{$ip})) {
                   8650: 	    return @{$iphosts{$ip}};
                   8651: 	}
                   8652: 	return;
1.839     albertel 8653:     }
1.864     albertel 8654:     
                   8655:     sub reset_hosts_ip_info {
                   8656: 	undef(%iphost);
                   8657: 	undef(%name_to_ip);
                   8658: 	undef(%lonid_to_ip);
                   8659:     }
1.856     albertel 8660: 
                   8661:     sub get_host_ip {
                   8662: 	my ($lonid) = @_;
                   8663: 	if (exists($lonid_to_ip{$lonid})) {
                   8664: 	    return $lonid_to_ip{$lonid};
                   8665: 	}
                   8666: 	my $name=&hostname($lonid);
                   8667:    	my $ip = gethostbyname($name);
                   8668: 	return if (!$ip || length($ip) ne 4);
                   8669: 	$ip=inet_ntoa($ip);
                   8670: 	$name_to_ip{$name}   = $ip;
                   8671: 	$lonid_to_ip{$lonid} = $ip;
                   8672: 	return $ip;
                   8673:     }
1.847     albertel 8674:     
                   8675:     sub get_iphost {
1.869     albertel 8676: 	my ($ignore_cache) = @_;
1.894     albertel 8677: 
1.869     albertel 8678: 	if (!$ignore_cache) {
                   8679: 	    if (%iphost) {
                   8680: 		return %iphost;
                   8681: 	    }
                   8682: 	    my ($ip_info,$cached)=
                   8683: 		&Apache::lonnet::is_cached_new('iphost','iphost');
                   8684: 	    if ($cached) {
                   8685: 		%iphost      = %{$ip_info->[0]};
                   8686: 		%name_to_ip  = %{$ip_info->[1]};
                   8687: 		%lonid_to_ip = %{$ip_info->[2]};
                   8688: 		return %iphost;
                   8689: 	    }
                   8690: 	}
1.894     albertel 8691: 
                   8692: 	# get yesterday's info for fallback
                   8693: 	my %old_name_to_ip;
                   8694: 	my ($ip_info,$cached)=
                   8695: 	    &Apache::lonnet::is_cached_new('iphost','iphost');
                   8696: 	if ($cached) {
                   8697: 	    %old_name_to_ip = %{$ip_info->[1]};
                   8698: 	}
                   8699: 
1.888     albertel 8700: 	my %name_to_host = &all_names();
                   8701: 	foreach my $name (keys(%name_to_host)) {
1.847     albertel 8702: 	    my $ip;
                   8703: 	    if (!exists($name_to_ip{$name})) {
                   8704: 		$ip = gethostbyname($name);
                   8705: 		if (!$ip || length($ip) ne 4) {
1.894     albertel 8706: 		    if (defined($old_name_to_ip{$name})) {
                   8707: 			$ip = $old_name_to_ip{$name};
                   8708: 			&logthis("Can't find $name defaulting to old $ip");
                   8709: 		    } else {
                   8710: 			&logthis("Name $name no IP found");
                   8711: 			next;
                   8712: 		    }
                   8713: 		} else {
                   8714: 		    $ip=inet_ntoa($ip);
1.847     albertel 8715: 		}
                   8716: 		$name_to_ip{$name} = $ip;
                   8717: 	    } else {
                   8718: 		$ip = $name_to_ip{$name};
1.653     albertel 8719: 	    }
1.888     albertel 8720: 	    foreach my $id (@{ $name_to_host{$name} }) {
                   8721: 		$lonid_to_ip{$id} = $ip;
                   8722: 	    }
                   8723: 	    push(@{$iphost{$ip}},@{$name_to_host{$name}});
1.598     albertel 8724: 	}
1.869     albertel 8725: 	&Apache::lonnet::do_cache_new('iphost','iphost',
                   8726: 				      [\%iphost,\%name_to_ip,\%lonid_to_ip],
1.894     albertel 8727: 				      48*60*60);
1.869     albertel 8728: 
1.847     albertel 8729: 	return %iphost;
1.598     albertel 8730:     }
                   8731: }
                   8732: 
1.862     albertel 8733: BEGIN {
                   8734: 
                   8735: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
                   8736:     unless ($readit) {
                   8737: {
                   8738:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
                   8739:     %perlvar = (%perlvar,%{$configvars});
                   8740: }
                   8741: 
                   8742: 
1.1       albertel 8743: # ------------------------------------------------------ Read spare server file
                   8744: {
1.448     albertel 8745:     open(my $config,"<$perlvar{'lonTabDir'}/spare.tab");
1.1       albertel 8746: 
                   8747:     while (my $configline=<$config>) {
                   8748:        chomp($configline);
1.284     matthew  8749:        if ($configline) {
1.784     albertel 8750: 	   my ($host,$type) = split(':',$configline,2);
1.785     albertel 8751: 	   if (!defined($type) || $type eq '') { $type = 'default' };
1.784     albertel 8752: 	   push(@{ $spareid{$type} }, $host);
1.1       albertel 8753:        }
                   8754:     }
1.448     albertel 8755:     close($config);
1.1       albertel 8756: }
1.11      www      8757: # ------------------------------------------------------------ Read permissions
                   8758: {
1.448     albertel 8759:     open(my $config,"<$perlvar{'lonTabDir'}/roles.tab");
1.11      www      8760: 
                   8761:     while (my $configline=<$config>) {
1.448     albertel 8762: 	chomp($configline);
                   8763: 	if ($configline) {
                   8764: 	    my ($role,$perm)=split(/ /,$configline);
                   8765: 	    if ($perm ne '') { $pr{$role}=$perm; }
                   8766: 	}
1.11      www      8767:     }
1.448     albertel 8768:     close($config);
1.11      www      8769: }
                   8770: 
                   8771: # -------------------------------------------- Read plain texts for permissions
                   8772: {
1.448     albertel 8773:     open(my $config,"<$perlvar{'lonTabDir'}/rolesplain.tab");
1.11      www      8774: 
                   8775:     while (my $configline=<$config>) {
1.448     albertel 8776: 	chomp($configline);
                   8777: 	if ($configline) {
1.742     raeburn  8778: 	    my ($short,@plain)=split(/:/,$configline);
                   8779:             %{$prp{$short}} = ();
                   8780: 	    if (@plain > 0) {
                   8781:                 $prp{$short}{'std'} = $plain[0];
                   8782:                 for (my $i=1; $i<@plain; $i++) {
                   8783:                     $prp{$short}{'alt'.$i} = $plain[$i];  
                   8784:                 }
                   8785:             }
1.448     albertel 8786: 	}
1.135     www      8787:     }
1.448     albertel 8788:     close($config);
1.135     www      8789: }
                   8790: 
                   8791: # ---------------------------------------------------------- Read package table
                   8792: {
1.448     albertel 8793:     open(my $config,"<$perlvar{'lonTabDir'}/packages.tab");
1.135     www      8794: 
                   8795:     while (my $configline=<$config>) {
1.483     albertel 8796: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448     albertel 8797: 	chomp($configline);
                   8798: 	my ($short,$plain)=split(/:/,$configline);
                   8799: 	my ($pack,$name)=split(/\&/,$short);
                   8800: 	if ($plain ne '') {
                   8801: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
                   8802: 	    $packagetab{$short}=$plain; 
                   8803: 	}
1.11      www      8804:     }
1.448     albertel 8805:     close($config);
1.329     matthew  8806: }
                   8807: 
                   8808: # ------------- set up temporary directory
                   8809: {
                   8810:     $tmpdir = $perlvar{'lonDaemons'}.'/tmp/';
                   8811: 
1.11      www      8812: }
                   8813: 
1.794     albertel 8814: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
                   8815: 				'compress_threshold'=> 20_000,
                   8816:  			        });
1.185     www      8817: 
1.281     www      8818: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186     www      8819: $dumpcount=0;
1.958     www      8820: $locknum=0;
1.22      www      8821: 
1.163     harris41 8822: &logtouch();
1.672     albertel 8823: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195     www      8824: $readit=1;
1.564     albertel 8825:     {
                   8826: 	use integer;
                   8827: 	my $test=(2**32)+1;
1.568     albertel 8828: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564     albertel 8829: 	&logthis(" Detected 64bit platform ($_64bit)");
                   8830:     }
1.195     www      8831: }
1.1       albertel 8832: }
1.179     www      8833: 
1.1       albertel 8834: 1;
1.191     harris41 8835: __END__
                   8836: 
1.243     albertel 8837: =pod
                   8838: 
1.191     harris41 8839: =head1 NAME
                   8840: 
1.243     albertel 8841: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191     harris41 8842: 
                   8843: =head1 SYNOPSIS
                   8844: 
1.243     albertel 8845: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191     harris41 8846: 
                   8847:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
                   8848: 
1.243     albertel 8849: Common parameters:
                   8850: 
                   8851: =over 4
                   8852: 
                   8853: =item *
                   8854: 
                   8855: $uname : an internal username (if $cname expecting a course Id specifically)
                   8856: 
                   8857: =item *
                   8858: 
                   8859: $udom : a domain (if $cdom expecting a course's domain specifically)
                   8860: 
                   8861: =item *
                   8862: 
                   8863: $symb : a resource instance identifier
                   8864: 
                   8865: =item *
                   8866: 
                   8867: $namespace : the name of a .db file that contains the data needed or
                   8868: being set.
                   8869: 
                   8870: =back
                   8871: 
1.394     bowersj2 8872: =head1 OVERVIEW
1.191     harris41 8873: 
1.394     bowersj2 8874: lonnet provides subroutines which interact with the
                   8875: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
                   8876: about classes, users, and resources.
1.243     albertel 8877: 
                   8878: For many of these objects you can also use this to store data about
                   8879: them or modify them in various ways.
1.191     harris41 8880: 
1.394     bowersj2 8881: =head2 Symbs
1.191     harris41 8882: 
1.394     bowersj2 8883: To identify a specific instance of a resource, LON-CAPA uses symbols
                   8884: or "symbs"X<symb>. These identifiers are built from the URL of the
                   8885: map, the resource number of the resource in the map, and the URL of
                   8886: the resource itself. The latter is somewhat redundant, but might help
                   8887: if maps change.
                   8888: 
                   8889: An example is
                   8890: 
                   8891:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
                   8892: 
                   8893: The respective map entry is
                   8894: 
                   8895:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
                   8896:   title="Problem 2">
                   8897:  </resource>
                   8898: 
                   8899: Symbs are used by the random number generator, as well as to store and
                   8900: restore data specific to a certain instance of for example a problem.
                   8901: 
                   8902: =head2 Storing And Retrieving Data
                   8903: 
                   8904: X<store()>X<cstore()>X<restore()>Three of the most important functions
                   8905: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
                   8906: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
                   8907: is is the non-critical message twin of cstore. These functions are for
                   8908: handlers to store a perl hash to a user's permanent data space in an
                   8909: easy manner, and to retrieve it again on another call. It is expected
                   8910: that a handler would use this once at the beginning to retrieve data,
                   8911: and then again once at the end to send only the new data back.
                   8912: 
                   8913: The data is stored in the user's data directory on the user's
                   8914: homeserver under the ID of the course.
                   8915: 
                   8916: The hash that is returned by restore will have all of the previous
                   8917: value for all of the elements of the hash.
                   8918: 
                   8919: Example:
                   8920: 
                   8921:  #creating a hash
                   8922:  my %hash;
                   8923:  $hash{'foo'}='bar';
                   8924: 
                   8925:  #storing it
                   8926:  &Apache::lonnet::cstore(\%hash);
                   8927: 
                   8928:  #changing a value
                   8929:  $hash{'foo'}='notbar';
                   8930: 
                   8931:  #adding a new value
                   8932:  $hash{'bar'}='foo';
                   8933:  &Apache::lonnet::cstore(\%hash);
                   8934: 
                   8935:  #retrieving the hash
                   8936:  my %history=&Apache::lonnet::restore();
                   8937: 
                   8938:  #print the hash
                   8939:  foreach my $key (sort(keys(%history))) {
                   8940:    print("\%history{$key} = $history{$key}");
                   8941:  }
                   8942: 
                   8943: Will print out:
1.191     harris41 8944: 
1.394     bowersj2 8945:  %history{1:foo} = bar
                   8946:  %history{1:keys} = foo:timestamp
                   8947:  %history{1:timestamp} = 990455579
                   8948:  %history{2:bar} = foo
                   8949:  %history{2:foo} = notbar
                   8950:  %history{2:keys} = foo:bar:timestamp
                   8951:  %history{2:timestamp} = 990455580
                   8952:  %history{bar} = foo
                   8953:  %history{foo} = notbar
                   8954:  %history{timestamp} = 990455580
                   8955:  %history{version} = 2
                   8956: 
                   8957: Note that the special hash entries C<keys>, C<version> and
                   8958: C<timestamp> were added to the hash. C<version> will be equal to the
                   8959: total number of versions of the data that have been stored. The
                   8960: C<timestamp> attribute will be the UNIX time the hash was
                   8961: stored. C<keys> is available in every historical section to list which
                   8962: keys were added or changed at a specific historical revision of a
                   8963: hash.
                   8964: 
                   8965: B<Warning>: do not store the hash that restore returns directly. This
                   8966: will cause a mess since it will restore the historical keys as if the
                   8967: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191     harris41 8968: 
1.394     bowersj2 8969: Calling convention:
1.191     harris41 8970: 
1.394     bowersj2 8971:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname,$home);
                   8972:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$home);
1.191     harris41 8973: 
1.394     bowersj2 8974: For more detailed information, see lonnet specific documentation.
1.191     harris41 8975: 
1.394     bowersj2 8976: =head1 RETURN MESSAGES
1.191     harris41 8977: 
1.394     bowersj2 8978: =over 4
1.191     harris41 8979: 
1.394     bowersj2 8980: =item * B<con_lost>: unable to contact remote host
1.191     harris41 8981: 
1.394     bowersj2 8982: =item * B<con_delayed>: unable to contact remote host, message will be delivered
                   8983: when the connection is brought back up
1.191     harris41 8984: 
1.394     bowersj2 8985: =item * B<con_failed>: unable to contact remote host and unable to save message
                   8986: for later delivery
1.191     harris41 8987: 
1.394     bowersj2 8988: =item * B<error:>: an error a occured, a description of the error follows the :
1.191     harris41 8989: 
1.394     bowersj2 8990: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243     albertel 8991: that was requested
1.191     harris41 8992: 
1.243     albertel 8993: =back
1.191     harris41 8994: 
1.243     albertel 8995: =head1 PUBLIC SUBROUTINES
1.191     harris41 8996: 
1.243     albertel 8997: =head2 Session Environment Functions
1.191     harris41 8998: 
1.243     albertel 8999: =over 4
1.191     harris41 9000: 
1.394     bowersj2 9001: =item * 
                   9002: X<appenv()>
1.949     raeburn  9003: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
1.394     bowersj2 9004: the user envirnoment file, and will be restored for each access this
1.620     albertel 9005: user makes during this session, also modifies the %env for the current
1.949     raeburn  9006: process. Optional rolesarrayref - if defined contains a reference to an array
                   9007: of roles which are exempt from the restriction on modifying user.role entries 
                   9008: in the user's environment.db and in %env.    
1.191     harris41 9009: 
                   9010: =item *
1.394     bowersj2 9011: X<delenv()>
                   9012: B<delenv($regexp)>: removes all items from the session
                   9013: environment file that matches the regular expression in $regexp. The
1.620     albertel 9014: values are also delted from the current processes %env.
1.191     harris41 9015: 
1.795     albertel 9016: =item * get_env_multiple($name) 
                   9017: 
                   9018: gets $name from the %env hash, it seemlessly handles the cases where multiple
                   9019: values may be defined and end up as an array ref.
                   9020: 
                   9021: returns an array of values
                   9022: 
1.243     albertel 9023: =back
                   9024: 
                   9025: =head2 User Information
1.191     harris41 9026: 
1.243     albertel 9027: =over 4
1.191     harris41 9028: 
                   9029: =item *
1.394     bowersj2 9030: X<queryauthenticate()>
                   9031: B<queryauthenticate($uname,$udom)>: try to determine user's current 
1.191     harris41 9032: authentication scheme
                   9033: 
                   9034: =item *
1.394     bowersj2 9035: X<authenticate()>
                   9036: B<authenticate($uname,$upass,$udom)>: try to
                   9037: authenticate user from domain's lib servers (first use the current
                   9038: one). C<$upass> should be the users password.
1.191     harris41 9039: 
                   9040: =item *
1.394     bowersj2 9041: X<homeserver()>
                   9042: B<homeserver($uname,$udom)>: find the server which has
                   9043: the user's directory and files (there must be only one), this caches
                   9044: the answer, and also caches if there is a borken connection.
1.191     harris41 9045: 
                   9046: =item *
1.394     bowersj2 9047: X<idget()>
                   9048: B<idget($udom,@ids)>: find the usernames behind a list of IDs
                   9049: (IDs are a unique resource in a domain, there must be only 1 ID per
                   9050: username, and only 1 username per ID in a specific domain) (returns
                   9051: hash: id=>name,id=>name)
1.191     harris41 9052: 
                   9053: =item *
1.394     bowersj2 9054: X<idrget()>
                   9055: B<idrget($udom,@unames)>: find the IDs behind a list of
                   9056: usernames (returns hash: name=>id,name=>id)
1.191     harris41 9057: 
                   9058: =item *
1.394     bowersj2 9059: X<idput()>
                   9060: B<idput($udom,%ids)>: store away a list of names and associated IDs
1.191     harris41 9061: 
                   9062: =item *
1.394     bowersj2 9063: X<rolesinit()>
                   9064: B<rolesinit($udom,$username,$authhost)>: get user privileges
1.243     albertel 9065: 
                   9066: =item *
1.551     albertel 9067: X<getsection()>
                   9068: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243     albertel 9069: course $cname, return section name/number or '' for "not in course"
                   9070: and '-1' for "no section"
                   9071: 
                   9072: =item *
1.394     bowersj2 9073: X<userenvironment()>
                   9074: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243     albertel 9075: passed in @what from the requested user's environment, returns a hash
                   9076: 
1.858     raeburn  9077: =item * 
                   9078: X<userlog_query()>
1.859     albertel 9079: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
                   9080: activity.log file. %filters defines filters applied when parsing the
                   9081: log file. These can be start or end timestamps, or the type of action
                   9082: - log to look for Login or Logout events, check for Checkin or
                   9083: Checkout, role for role selection. The response is in the form
                   9084: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
                   9085: escaped strings of the action recorded in the activity.log file.
1.858     raeburn  9086: 
1.243     albertel 9087: =back
                   9088: 
                   9089: =head2 User Roles
                   9090: 
                   9091: =over 4
                   9092: 
                   9093: =item *
                   9094: 
1.810     raeburn  9095: allowed($priv,$uri,$symb,$role) : check for a user privilege; returns codes for allowed actions
1.243     albertel 9096:  F: full access
                   9097:  U,I,K: authentication modes (cxx only)
                   9098:  '': forbidden
                   9099:  1: user needs to choose course
                   9100:  2: browse allowed
1.766     albertel 9101:  A: passphrase authentication needed
1.243     albertel 9102: 
                   9103: =item *
                   9104: 
                   9105: definerole($rolename,$sysrole,$domrole,$courole) : define role; define a custom
                   9106: role rolename set privileges in format of lonTabs/roles.tab for system, domain,
                   9107: and course level
                   9108: 
                   9109: =item *
                   9110: 
                   9111: plaintext($short) : return value in %prp hash (rolesplain.tab); plain text
                   9112: explanation of a user role term
                   9113: 
1.832     raeburn  9114: =item *
                   9115: 
1.935     raeburn  9116: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec) :
1.858     raeburn  9117: All arguments are optional. Returns a hash of a roles, either for
                   9118: co-author/assistant author roles for a user's Construction Space
1.906     albertel 9119: (default), or if $context is 'userroles', roles for the user himself,
1.933     raeburn  9120: In the hash, keys are set to colon-separated $uname,$udom,$role, and
                   9121: (optionally) if $withsec is true, a fourth colon-separated item - $section.
                   9122: For each key, value is set to colon-separated start and end times for
                   9123: the role.  If no username and domain are specified, will default to
1.934     raeburn  9124: current user/domain. Types, roles, and roledoms are references to arrays
1.858     raeburn  9125: of role statuses (active, future or previous), roles 
                   9126: (e.g., cc,in, st etc.) and domains of the roles which can be used
                   9127: to restrict the list of roles reported. If no array ref is 
                   9128: provided for types, will default to return only active roles.
1.834     albertel 9129: 
1.243     albertel 9130: =back
                   9131: 
                   9132: =head2 User Modification
                   9133: 
                   9134: =over 4
                   9135: 
                   9136: =item *
                   9137: 
1.957     raeburn  9138: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
1.243     albertel 9139: user for the level given by URL.  Optional start and end dates (leave empty
                   9140: string or zero for "no date")
1.191     harris41 9141: 
                   9142: =item *
                   9143: 
1.243     albertel 9144: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
                   9145: change a users, password, possible return values are: ok,
                   9146: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
                   9147: refused
1.191     harris41 9148: 
                   9149: =item *
                   9150: 
1.243     albertel 9151: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191     harris41 9152: 
                   9153: =item *
                   9154: 
1.963     raeburn  9155: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,
                   9156:            $forceid,$desiredhome,$email,$inststatus) : 
1.243     albertel 9157: modify user
1.191     harris41 9158: 
                   9159: =item *
                   9160: 
1.286     matthew  9161: modifystudent
                   9162: 
1.957     raeburn  9163: modify a student's enrollment and identification information.
1.286     matthew  9164: The course id is resolved based on the current users environment.  
                   9165: This means the envoking user must be a course coordinator or otherwise
                   9166: associated with a course.
                   9167: 
1.297     matthew  9168: This call is essentially a wrapper for lonnet::modifyuser and
                   9169: lonnet::modify_student_enrollment
1.286     matthew  9170: 
                   9171: Inputs: 
                   9172: 
                   9173: =over 4
                   9174: 
1.957     raeburn  9175: =item B<$udom> Student's loncapa domain
1.286     matthew  9176: 
1.957     raeburn  9177: =item B<$uname> Student's loncapa login name
1.286     matthew  9178: 
1.964     bisitz   9179: =item B<$uid> Student/Employee ID
1.286     matthew  9180: 
1.957     raeburn  9181: =item B<$umode> Student's authentication mode
1.286     matthew  9182: 
1.957     raeburn  9183: =item B<$upass> Student's password
1.286     matthew  9184: 
1.957     raeburn  9185: =item B<$first> Student's first name
1.286     matthew  9186: 
1.957     raeburn  9187: =item B<$middle> Student's middle name
1.286     matthew  9188: 
1.957     raeburn  9189: =item B<$last> Student's last name
1.286     matthew  9190: 
1.957     raeburn  9191: =item B<$gene> Student's generation
1.286     matthew  9192: 
1.957     raeburn  9193: =item B<$usec> Student's section in course
1.286     matthew  9194: 
                   9195: =item B<$end> Unix time of the roles expiration
                   9196: 
                   9197: =item B<$start> Unix time of the roles start date
                   9198: 
                   9199: =item B<$forceid> If defined, allow $uid to be changed
                   9200: 
                   9201: =item B<$desiredhome> server to use as home server for student
                   9202: 
1.957     raeburn  9203: =item B<$email> Student's permanent e-mail address
                   9204: 
                   9205: =item B<$type> Type of enrollment (auto or manual)
                   9206: 
1.963     raeburn  9207: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto    
                   9208: 
                   9209: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
1.957     raeburn  9210: 
1.963     raeburn  9211: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
1.957     raeburn  9212: 
1.963     raeburn  9213: =item B<$context> role change context (shown in User Management Logs display in a course)
1.957     raeburn  9214: 
1.963     raeburn  9215: =item B<$inststatus> institutional status of user - : separated string of escaped status types  
1.957     raeburn  9216: 
1.286     matthew  9217: =back
1.297     matthew  9218: 
                   9219: =item *
                   9220: 
                   9221: modify_student_enrollment
                   9222: 
                   9223: Change a students enrollment status in a class.  The environment variable
                   9224: 'role.request.course' must be defined for this function to proceed.
                   9225: 
                   9226: Inputs:
                   9227: 
                   9228: =over 4
                   9229: 
                   9230: =item $udom, students domain
                   9231: 
                   9232: =item $uname, students name
                   9233: 
                   9234: =item $uid, students user id
                   9235: 
                   9236: =item $first, students first name
                   9237: 
                   9238: =item $middle
                   9239: 
                   9240: =item $last
                   9241: 
                   9242: =item $gene
                   9243: 
                   9244: =item $usec
                   9245: 
                   9246: =item $end
                   9247: 
                   9248: =item $start
                   9249: 
1.957     raeburn  9250: =item $type
                   9251: 
                   9252: =item $locktype
                   9253: 
                   9254: =item $cid
                   9255: 
                   9256: =item $selfenroll
                   9257: 
                   9258: =item $context
                   9259: 
1.297     matthew  9260: =back
                   9261: 
1.191     harris41 9262: 
                   9263: =item *
                   9264: 
1.243     albertel 9265: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
                   9266: custom role; give a custom role to a user for the level given by URL.  Specify
                   9267: name and domain of role author, and role name
1.191     harris41 9268: 
                   9269: =item *
                   9270: 
1.243     albertel 9271: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191     harris41 9272: 
                   9273: =item *
                   9274: 
1.243     albertel 9275: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
                   9276: 
                   9277: =back
                   9278: 
                   9279: =head2 Course Infomation
                   9280: 
                   9281: =over 4
1.191     harris41 9282: 
                   9283: =item *
                   9284: 
1.631     albertel 9285: coursedescription($courseid) : returns a hash of information about the
                   9286: specified course id, including all environment settings for the
                   9287: course, the description of the course will be in the hash under the
                   9288: key 'description'
1.191     harris41 9289: 
                   9290: =item *
                   9291: 
1.624     albertel 9292: resdata($name,$domain,$type,@which) : request for current parameter
                   9293: setting for a specific $type, where $type is either 'course' or 'user',
                   9294: @what should be a list of parameters to ask about. This routine caches
                   9295: answers for 5 minutes.
1.243     albertel 9296: 
1.877     foxr     9297: =item *
                   9298: 
                   9299: get_courseresdata($courseid, $domain) : dump the entire course resource
                   9300: data base, returning a hash that is keyed by the resource name and has
                   9301: values that are the resource value.  I believe that the timestamps and
                   9302: versions are also returned.
                   9303: 
                   9304: 
1.243     albertel 9305: =back
                   9306: 
                   9307: =head2 Course Modification
                   9308: 
                   9309: =over 4
1.191     harris41 9310: 
                   9311: =item *
                   9312: 
1.243     albertel 9313: writecoursepref($courseid,%prefs) : write preferences (environment
                   9314: database) for a course
1.191     harris41 9315: 
                   9316: =item *
                   9317: 
1.243     albertel 9318: createcourse($udom,$description,$url) : make/modify course
                   9319: 
                   9320: =back
                   9321: 
                   9322: =head2 Resource Subroutines
                   9323: 
                   9324: =over 4
1.191     harris41 9325: 
                   9326: =item *
                   9327: 
1.243     albertel 9328: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191     harris41 9329: 
                   9330: =item *
                   9331: 
1.243     albertel 9332: repcopy($filename) : subscribes to the requested file, and attempts to
                   9333: replicate from the owning library server, Might return
1.607     raeburn  9334: 'unavailable', 'not_found', 'forbidden', 'ok', or
                   9335: 'bad_request', also attempts to grab the metadata for the
1.243     albertel 9336: resource. Expects the local filesystem pathname
                   9337: (/home/httpd/html/res/....)
                   9338: 
                   9339: =back
                   9340: 
                   9341: =head2 Resource Information
                   9342: 
                   9343: =over 4
1.191     harris41 9344: 
                   9345: =item *
                   9346: 
1.243     albertel 9347: EXT($varname,$symb,$udom,$uname) : evaluates and returns the value of
                   9348: a vairety of different possible values, $varname should be a request
                   9349: string, and the other parameters can be used to specify who and what
                   9350: one is asking about.
                   9351: 
                   9352: Possible values for $varname are environment.lastname (or other item
                   9353: from the envirnment hash), user.name (or someother aspect about the
                   9354: user), resource.0.maxtries (or some other part and parameter of a
                   9355: resource)
1.204     albertel 9356: 
                   9357: =item *
                   9358: 
1.243     albertel 9359: directcondval($number) : get current value of a condition; reads from a state
                   9360: string
1.204     albertel 9361: 
                   9362: =item *
                   9363: 
1.243     albertel 9364: condval($condidx) : value of condition index based on state
1.204     albertel 9365: 
                   9366: =item *
                   9367: 
1.243     albertel 9368: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
                   9369: resource's metadata, $what should be either a specific key, or either
                   9370: 'keys' (to get a list of possible keys) or 'packages' to get a list of
                   9371: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
                   9372: 
                   9373: this function automatically caches all requests
1.191     harris41 9374: 
                   9375: =item *
                   9376: 
1.243     albertel 9377: metadata_query($query,$custom,$customshow) : make a metadata query against the
                   9378: network of library servers; returns file handle of where SQL and regex results
                   9379: will be stored for query
1.191     harris41 9380: 
                   9381: =item *
                   9382: 
1.243     albertel 9383: symbread($filename) : return symbolic list entry (filename argument optional);
                   9384: returns the data handle
1.191     harris41 9385: 
                   9386: =item *
                   9387: 
1.243     albertel 9388: symbverify($symb,$thisfn) : verifies that $symb actually exists and is
1.582     albertel 9389: a possible symb for the URL in $thisfn, and if is an encryypted
                   9390: resource that the user accessed using /enc/ returns a 1 on success, 0
                   9391: on failure, user must be in a course, as it assumes the existance of
1.620     albertel 9392: the course initial hash, and uses $env('request.course.id'}
1.243     albertel 9393: 
1.191     harris41 9394: 
                   9395: =item *
                   9396: 
1.243     albertel 9397: symbclean($symb) : removes versions numbers from a symb, returns the
                   9398: cleaned symb
1.191     harris41 9399: 
                   9400: =item *
                   9401: 
1.243     albertel 9402: is_on_map($uri) : checks if the $uri is somewhere on the current
                   9403: course map, user must be in a course for it to work.
1.191     harris41 9404: 
                   9405: =item *
                   9406: 
1.243     albertel 9407: numval($salt) : return random seed value (addend for rndseed)
1.191     harris41 9408: 
                   9409: =item *
                   9410: 
1.243     albertel 9411: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
                   9412: a random seed, all arguments are optional, if they aren't sent it uses the
                   9413: environment to derive them. Note: if symb isn't sent and it can't get one
                   9414: from &symbread it will use the current time as its return value
1.191     harris41 9415: 
                   9416: =item *
                   9417: 
1.243     albertel 9418: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
                   9419: unfakeable, receipt
1.191     harris41 9420: 
                   9421: =item *
                   9422: 
1.620     albertel 9423: receipt() : API to ireceipt working off of env values; given out to users
1.191     harris41 9424: 
                   9425: =item *
                   9426: 
1.243     albertel 9427: countacc($url) : count the number of accesses to a given URL
1.191     harris41 9428: 
                   9429: =item *
                   9430: 
1.243     albertel 9431: 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 9432: 
                   9433: =item *
                   9434: 
1.243     albertel 9435: 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 9436: 
                   9437: =item *
                   9438: 
1.243     albertel 9439: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191     harris41 9440: 
                   9441: =item *
                   9442: 
1.243     albertel 9443: devalidate($symb) : devalidate temporary spreadsheet calculations,
                   9444: forcing spreadsheet to reevaluate the resource scores next time.
                   9445: 
                   9446: =back
                   9447: 
                   9448: =head2 Storing/Retreiving Data
                   9449: 
                   9450: =over 4
1.191     harris41 9451: 
                   9452: =item *
                   9453: 
1.243     albertel 9454: store($storehash,$symb,$namespace,$udom,$uname) : stores hash permanently
                   9455: for this url; hashref needs to be given and should be a \%hashname; the
                   9456: remaining args aren't required and if they aren't passed or are '' they will
1.620     albertel 9457: be derived from the env
1.191     harris41 9458: 
                   9459: =item *
                   9460: 
1.243     albertel 9461: cstore($storehash,$symb,$namespace,$udom,$uname) : same as store but
                   9462: uses critical subroutine
1.191     harris41 9463: 
                   9464: =item *
                   9465: 
1.243     albertel 9466: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
                   9467: all args are optional
1.191     harris41 9468: 
                   9469: =item *
                   9470: 
1.717     albertel 9471: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
                   9472: dumps the complete (or key matching regexp) namespace into a hash
                   9473: ($udom, $uname, $regexp, $range are optional) for a namespace that is
                   9474: normally &store()ed into
                   9475: 
                   9476: $range should be either an integer '100' (give me the first 100
                   9477:                                            matching records)
                   9478:               or be  two integers sperated by a - with no spaces
                   9479:                  '30-50' (give me the 30th through the 50th matching
                   9480:                           records)
                   9481: 
                   9482: 
                   9483: =item *
                   9484: 
                   9485: putstore($namespace,$symb,$version,$storehash,$udomain,$uname) :
                   9486: replaces a &store() version of data with a replacement set of data
                   9487: for a particular resource in a namespace passed in the $storehash hash 
                   9488: reference
                   9489: 
                   9490: =item *
                   9491: 
1.243     albertel 9492: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
                   9493: works very similar to store/cstore, but all data is stored in a
                   9494: temporary location and can be reset using tmpreset, $storehash should
                   9495: be a hash reference, returns nothing on success
1.191     harris41 9496: 
                   9497: =item *
                   9498: 
1.243     albertel 9499: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
                   9500: similar to restore, but all data is stored in a temporary location and
                   9501: can be reset using tmpreset. Returns a hash of values on success,
                   9502: error string otherwise.
1.191     harris41 9503: 
                   9504: =item *
                   9505: 
1.243     albertel 9506: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
                   9507: deltes all keys for $symb form the temporary storage hash.
1.191     harris41 9508: 
                   9509: =item *
                   9510: 
1.243     albertel 9511: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   9512: reference filled in from namesp ($udom and $uname are optional)
1.191     harris41 9513: 
                   9514: =item *
                   9515: 
1.243     albertel 9516: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
                   9517: namesp ($udom and $uname are optional)
1.191     harris41 9518: 
                   9519: =item *
                   9520: 
1.702     albertel 9521: dump($namespace,$udom,$uname,$regexp,$range) : 
1.243     albertel 9522: dumps the complete (or key matching regexp) namespace into a hash
1.702     albertel 9523: ($udom, $uname, $regexp, $range are optional)
1.449     matthew  9524: 
1.702     albertel 9525: $range should be either an integer '100' (give me the first 100
                   9526:                                            matching records)
                   9527:               or be  two integers sperated by a - with no spaces
                   9528:                  '30-50' (give me the 30th through the 50th matching
                   9529:                           records)
1.449     matthew  9530: =item *
                   9531: 
                   9532: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
                   9533: $store can be a scalar, an array reference, or if the amount to be 
                   9534: incremented is > 1, a hash reference.
                   9535: 
                   9536: ($udom and $uname are optional)
1.191     harris41 9537: 
                   9538: =item *
                   9539: 
1.243     albertel 9540: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
                   9541: ($udom and $uname are optional)
1.191     harris41 9542: 
                   9543: =item *
                   9544: 
1.243     albertel 9545: cput($namespace,$storehash,$udom,$uname) : critical put
                   9546: ($udom and $uname are optional)
1.191     harris41 9547: 
                   9548: =item *
                   9549: 
1.748     albertel 9550: newput($namespace,$storehash,$udom,$uname) :
                   9551: 
                   9552: Attempts to store the items in the $storehash, but only if they don't
                   9553: currently exist, if this succeeds you can be certain that you have 
                   9554: successfully created a new key value pair in the $namespace db.
                   9555: 
                   9556: 
                   9557: Args:
                   9558:  $namespace: name of database to store values to
                   9559:  $storehash: hashref to store to the db
                   9560:  $udom: (optional) domain of user containing the db
                   9561:  $uname: (optional) name of user caontaining the db
                   9562: 
                   9563: Returns:
                   9564:  'ok' -> succeeded in storing all keys of $storehash
                   9565:  'key_exists: <key>' -> failed to anything out of $storehash, as at
                   9566:                         least <key> already existed in the db (other
                   9567:                         requested keys may also already exist)
                   9568:  'error: <msg>' -> unable to tie the DB or other erorr occured
                   9569:  'con_lost' -> unable to contact request server
                   9570:  'refused' -> action was not allowed by remote machine
                   9571: 
                   9572: 
                   9573: =item *
                   9574: 
1.243     albertel 9575: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   9576: reference filled in from namesp (encrypts the return communication)
                   9577: ($udom and $uname are optional)
1.191     harris41 9578: 
                   9579: =item *
                   9580: 
1.243     albertel 9581: log($udom,$name,$home,$message) : write to permanent log for user; use
                   9582: critical subroutine
                   9583: 
1.806     raeburn  9584: =item *
                   9585: 
1.860     raeburn  9586: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
                   9587: array reference filled in from namespace found in domain level on either
                   9588: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806     raeburn  9589: 
                   9590: =item *
                   9591: 
1.860     raeburn  9592: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
                   9593: domain level either on specified domain server ($uhome) or primary domain 
                   9594: server ($udom and $uhome are optional)
1.806     raeburn  9595: 
1.943     raeburn  9596: =item * 
                   9597: 
                   9598: get_domain_defaults($target_domain) : returns hash with defaults for
                   9599: authentication and language in the domain. Keys are: auth_def, auth_arg_def,
                   9600: lang_def; corresponsing values are authentication type (internal, krb4, krb5,
                   9601: or localauth), initial password or a kerberos realm, language (e.g., en-us).
                   9602: Values are retrieved from cache (if current), or from domain's configuration.db
                   9603: (if available), or lastly from values in lonTabs/dns_domain,tab, 
                   9604: or lonTabs/domain.tab. 
                   9605: 
                   9606: %domdefaults = &get_auth_defaults($target_domain);
                   9607: 
1.243     albertel 9608: =back
                   9609: 
                   9610: =head2 Network Status Functions
                   9611: 
                   9612: =over 4
1.191     harris41 9613: 
                   9614: =item *
                   9615: 
                   9616: dirlist($uri) : return directory list based on URI
                   9617: 
                   9618: =item *
                   9619: 
1.243     albertel 9620: spareserver() : find server with least workload from spare.tab
                   9621: 
                   9622: =back
                   9623: 
                   9624: =head2 Apache Request
                   9625: 
                   9626: =over 4
1.191     harris41 9627: 
                   9628: =item *
                   9629: 
1.243     albertel 9630: ssi($url,%hash) : server side include, does a complete request cycle on url to
                   9631: localhost, posts hash
                   9632: 
                   9633: =back
                   9634: 
                   9635: =head2 Data to String to Data
                   9636: 
                   9637: =over 4
1.191     harris41 9638: 
                   9639: =item *
                   9640: 
1.243     albertel 9641: hash2str(%hash) : convert a hash into a string complete with escaping and '='
                   9642: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191     harris41 9643: 
                   9644: =item *
                   9645: 
1.243     albertel 9646: hashref2str($hashref) : convert a hashref into a string complete with
                   9647: escaping and '=' and '&' separators, supports elements that are
                   9648: arrayrefs and hashrefs
1.191     harris41 9649: 
                   9650: =item *
                   9651: 
1.243     albertel 9652: arrayref2str($arrayref) : convert an arrayref into a string complete
                   9653: with escaping and '&' separators, supports elements that are arrayrefs
                   9654: and hashrefs
1.191     harris41 9655: 
                   9656: =item *
                   9657: 
1.243     albertel 9658: str2hash($string) : convert string to hash using unescaping and
                   9659: splitting on '=' and '&', supports elements that are arrayrefs and
                   9660: hashrefs
1.191     harris41 9661: 
                   9662: =item *
                   9663: 
1.243     albertel 9664: str2array($string) : convert string to hash using unescaping and
                   9665: splitting on '&', supports elements that are arrayrefs and hashrefs
                   9666: 
                   9667: =back
                   9668: 
                   9669: =head2 Logging Routines
                   9670: 
                   9671: =over 4
                   9672: 
                   9673: These routines allow one to make log messages in the lonnet.log and
                   9674: lonnet.perm logfiles.
1.191     harris41 9675: 
                   9676: =item *
                   9677: 
1.243     albertel 9678: logtouch() : make sure the logfile, lonnet.log, exists
1.191     harris41 9679: 
                   9680: =item *
                   9681: 
1.243     albertel 9682: logthis() : append message to the normal lonnet.log file, it gets
                   9683: preiodically rolled over and deleted.
1.191     harris41 9684: 
                   9685: =item *
                   9686: 
1.243     albertel 9687: logperm() : append a permanent message to lonnet.perm.log, this log
                   9688: file never gets deleted by any automated portion of the system, only
                   9689: messages of critical importance should go in here.
                   9690: 
                   9691: =back
                   9692: 
                   9693: =head2 General File Helper Routines
                   9694: 
                   9695: =over 4
1.191     harris41 9696: 
                   9697: =item *
                   9698: 
1.481     raeburn  9699: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
                   9700: (a) files in /uploaded
                   9701:   (i) If a local copy of the file exists - 
                   9702:       compares modification date of local copy with last-modified date for 
                   9703:       definitive version stored on home server for course. If local copy is 
                   9704:       stale, requests a new version from the home server and stores it. 
                   9705:       If the original has been removed from the home server, then local copy 
                   9706:       is unlinked.
                   9707:   (ii) If local copy does not exist -
                   9708:       requests the file from the home server and stores it. 
                   9709:   
                   9710:   If $caller is 'uploadrep':  
                   9711:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
                   9712:     for request for files originally uploaded via DOCS. 
                   9713:      - returns 'ok' if fresh local copy now available, -1 otherwise.
                   9714:   
                   9715:   Otherwise:
                   9716:      This indicates a call from the content generation phase of the request.
                   9717:      -  returns the entire contents of the file or -1.
                   9718:      
                   9719: (b) files in /res
                   9720:    - returns the entire contents of a file or -1; 
                   9721:    it properly subscribes to and replicates the file if neccessary.
1.191     harris41 9722: 
1.712     albertel 9723: 
                   9724: =item *
                   9725: 
                   9726: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
                   9727:                   reference
                   9728: 
                   9729: returns either a stat() list of data about the file or an empty list
                   9730: if the file doesn't exist or couldn't find out about it (connection
                   9731: problems or user unknown)
                   9732: 
1.191     harris41 9733: =item *
                   9734: 
1.243     albertel 9735: filelocation($dir,$file) : returns file system location of a file
                   9736: based on URI; meant to be "fairly clean" absolute reference, $dir is a
                   9737: directory that relative $file lookups are to looked in ($dir of /a/dir
                   9738: and a file of ../bob will become /a/bob)
1.191     harris41 9739: 
                   9740: =item *
                   9741: 
                   9742: hreflocation($dir,$file) : returns file system location or a URL; same as
                   9743: filelocation except for hrefs
                   9744: 
                   9745: =item *
                   9746: 
                   9747: declutter() : declutters URLs (remove docroot, beginning slashes, 'res' etc)
                   9748: 
1.243     albertel 9749: =back
                   9750: 
1.608     albertel 9751: =head2 Usererfile file routines (/uploaded*)
                   9752: 
                   9753: =over 4
                   9754: 
                   9755: =item *
                   9756: 
                   9757: userfileupload(): main rotine for putting a file in a user or course's
                   9758:                   filespace, arguments are,
                   9759: 
1.620     albertel 9760:  formname - required - this is the name of the element in $env where the
1.608     albertel 9761:            filename, and the contents of the file to create/modifed exist
1.620     albertel 9762:            the filename is in $env{'form.'.$formname.'.filename'} and the
                   9763:            contents of the file is located in $env{'form.'.$formname}
1.608     albertel 9764:  coursedoc - if true, store the file in the course of the active role
                   9765:              of the current user
                   9766:  subdir - required - subdirectory to put the file in under ../userfiles/
                   9767:          if undefined, it will be placed in "unknown"
                   9768: 
                   9769:  (This routine calls clean_filename() to remove any dangerous
                   9770:  characters from the filename, and then calls finuserfileupload() to
                   9771:  complete the transaction)
                   9772: 
                   9773:  returns either the url of the uploaded file (/uploaded/....) if successful
                   9774:  and /adm/notfound.html if unsuccessful
                   9775: 
                   9776: =item *
                   9777: 
                   9778: clean_filename(): routine for cleaing a filename up for storage in
                   9779:                  userfile space, argument is:
                   9780: 
                   9781:  filename - proposed filename
                   9782: 
                   9783: returns: the new clean filename
                   9784: 
                   9785: =item *
                   9786: 
                   9787: finishuserfileupload(): routine that creaes and sends the file to
                   9788: userspace, probably shouldn't be called directly
                   9789: 
                   9790:   docuname: username or courseid of destination for the file
                   9791:   docudom: domain of user/course of destination for the file
                   9792:   formname: same as for userfileupload()
                   9793:   fname: filename (inculding subdirectories) for the file
                   9794: 
                   9795:  returns either the url of the uploaded file (/uploaded/....) if successful
                   9796:  and /adm/notfound.html if unsuccessful
                   9797: 
                   9798: =item *
                   9799: 
                   9800: renameuserfile(): renames an existing userfile to a new name
                   9801: 
                   9802:   Args:
                   9803:    docuname: username or courseid of destination for the file
                   9804:    docudom: domain of user/course of destination for the file
                   9805:    old: current file name (including any subdirs under userfiles)
                   9806:    new: desired file name (including any subdirs under userfiles)
                   9807: 
                   9808: =item *
                   9809: 
                   9810: mkdiruserfile(): creates a directory is a userfiles dir
                   9811: 
                   9812:   Args:
                   9813:    docuname: username or courseid of destination for the file
                   9814:    docudom: domain of user/course of destination for the file
                   9815:    dir: dir to create (including any subdirs under userfiles)
                   9816: 
                   9817: =item *
                   9818: 
                   9819: removeuserfile(): removes a file that exists in userfiles
                   9820: 
                   9821:   Args:
                   9822:    docuname: username or courseid of destination for the file
                   9823:    docudom: domain of user/course of destination for the file
                   9824:    fname: filname to delete (including any subdirs under userfiles)
                   9825: 
                   9826: =item *
                   9827: 
                   9828: removeuploadedurl(): convience function for removeuserfile()
                   9829: 
                   9830:   Args:
                   9831:    url:  a full /uploaded/... url to delete
                   9832: 
1.747     albertel 9833: =item * 
                   9834: 
                   9835: get_portfile_permissions():
                   9836:   Args:
                   9837:     domain: domain of user or course contain the portfolio files
                   9838:     user: name of user or num of course contain the portfolio files
                   9839:   Returns:
                   9840:     hashref of a dump of the proper file_permissions.db
                   9841:    
                   9842: 
                   9843: =item * 
                   9844: 
                   9845: get_access_controls():
                   9846: 
                   9847: Args:
                   9848:   current_permissions: the hash ref returned from get_portfile_permissions()
                   9849:   group: (optional) the group you want the files associated with
                   9850:   file: (optional) the file you want access info on
                   9851: 
                   9852: Returns:
1.749     raeburn  9853:     a hash (keys are file names) of hashes containing
                   9854:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
                   9855:         values are XML containing access control settings (see below) 
1.747     albertel 9856: 
                   9857: Internal notes:
                   9858: 
1.749     raeburn  9859:  access controls are stored in file_permissions.db as key=value pairs.
                   9860:     key -> path to file/file_name\0uniqueID:scope_end_start
                   9861:         where scope -> public,guest,course,group,domains or users.
                   9862:               end -> UNIX time for end of access (0 -> no end date)
                   9863:               start -> UNIX time for start of access
                   9864: 
                   9865:     value -> XML description of access control
                   9866:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
                   9867:             <start></start>
                   9868:             <end></end>
                   9869: 
                   9870:             <password></password>  for scope type = guest
                   9871: 
                   9872:             <domain></domain>     for scope type = course or group
                   9873:             <number></number>
                   9874:             <roles id="">
                   9875:              <role></role>
                   9876:              <access></access>
                   9877:              <section></section>
                   9878:              <group></group>
                   9879:             </roles>
                   9880: 
                   9881:             <dom></dom>         for scope type = domains
                   9882: 
                   9883:             <users>             for scope type = users
                   9884:              <user>
                   9885:               <uname></uname>
                   9886:               <udom></udom>
                   9887:              </user>
                   9888:             </users>
                   9889:            </scope> 
                   9890:               
                   9891:  Access data is also aggregated for each file in an additional key=value pair:
                   9892:  key -> path to file/file_name\0accesscontrol 
                   9893:  value -> reference to hash
                   9894:           hash contains key = value pairs
                   9895:           where key = uniqueID:scope_end_start
                   9896:                 value = UNIX time record was last updated
                   9897: 
                   9898:           Used to improve speed of look-ups of access controls for each file.  
                   9899:  
                   9900:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
                   9901: 
                   9902: modify_access_controls():
                   9903: 
                   9904: Modifies access controls for a portfolio file
                   9905: Args
                   9906: 1. file name
                   9907: 2. reference to hash of required changes,
                   9908: 3. domain
                   9909: 4. username
                   9910:   where domain,username are the domain of the portfolio owner 
                   9911:   (either a user or a course) 
                   9912: 
                   9913: Returns:
                   9914: 1. result of additions or updates ('ok' or 'error', with error message). 
                   9915: 2. result of deletions ('ok' or 'error', with error message).
                   9916: 3. reference to hash of any new or updated access controls.
                   9917: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
                   9918:    key = integer (inbound ID)
                   9919:    value = uniqueID  
1.747     albertel 9920: 
1.608     albertel 9921: =back
                   9922: 
1.243     albertel 9923: =head2 HTTP Helper Routines
                   9924: 
                   9925: =over 4
                   9926: 
1.191     harris41 9927: =item *
                   9928: 
                   9929: escape() : unpack non-word characters into CGI-compatible hex codes
                   9930: 
                   9931: =item *
                   9932: 
                   9933: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
                   9934: 
1.243     albertel 9935: =back
                   9936: 
                   9937: =head1 PRIVATE SUBROUTINES
                   9938: 
                   9939: =head2 Underlying communication routines (Shouldn't call)
                   9940: 
                   9941: =over 4
                   9942: 
                   9943: =item *
                   9944: 
                   9945: subreply() : tries to pass a message to lonc, returns con_lost if incapable
                   9946: 
                   9947: =item *
                   9948: 
                   9949: reply() : uses subreply to send a message to remote machine, logs all failures
                   9950: 
                   9951: =item *
                   9952: 
                   9953: critical() : passes a critical message to another server; if cannot
                   9954: get through then place message in connection buffer directory and
                   9955: returns con_delayed, if incapable of saving message, returns
                   9956: con_failed
                   9957: 
                   9958: =item *
                   9959: 
                   9960: reconlonc() : tries to reconnect lonc client processes.
                   9961: 
                   9962: =back
                   9963: 
                   9964: =head2 Resource Access Logging
                   9965: 
                   9966: =over 4
                   9967: 
                   9968: =item *
                   9969: 
                   9970: flushcourselogs() : flush (save) buffer logs and access logs
                   9971: 
                   9972: =item *
                   9973: 
                   9974: courselog($what) : save message for course in hash
                   9975: 
                   9976: =item *
                   9977: 
                   9978: courseacclog($what) : save message for course using &courselog().  Perform
                   9979: special processing for specific resource types (problems, exams, quizzes, etc).
                   9980: 
1.191     harris41 9981: =item *
                   9982: 
                   9983: goodbye() : flush course logs and log shutting down; it is called in srm.conf
                   9984: as a PerlChildExitHandler
1.243     albertel 9985: 
                   9986: =back
                   9987: 
                   9988: =head2 Other
                   9989: 
                   9990: =over 4
                   9991: 
                   9992: =item *
                   9993: 
                   9994: symblist($mapname,%newhash) : update symbolic storage links
1.191     harris41 9995: 
                   9996: =back
                   9997: 
                   9998: =cut
1.877     foxr     9999: 

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