Annotation of loncom/lonnet/perl/lonnet.pm, revision 1.1172.2.146.2.24
1.1 albertel 1: # The LearningOnline Network
2: # TCP networking package
1.12 www 3: #
1.1172.2.146. .24(raeb 4:-24): # $Id: lonnet.pm,v 1.1172.2.146.2.23 2024/08/25 22:26:12 raeburn Exp $
1.178 www 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
1.169 harris41 28: ###
29:
1.971 jms 30: =pod
31:
1.972 jms 32: =head1 NAME
33:
34: Apache::lonnet.pm
35:
36: =head1 SYNOPSIS
37:
38: This file is an interface to the lonc processes of
39: the LON-CAPA network as well as set of elaborated functions for handling information
40: necessary for navigating through a given cluster of LON-CAPA machines within a
41: domain. There are over 40 specialized functions in this module which handle the
42: reading and transmission of metadata, user information (ids, names, environments, roles,
43: logs), file information (storage, reading, directories, extensions, replication, embedded
44: styles and descriptors), educational resources (course descriptions, section names and
45: numbers), url hashing (to assign roles on a url basis), and translating abbreviated symbols to
46: and from more descriptive phrases or explanations.
47:
48: This is part of the LearningOnline Network with CAPA project
49: described at http://www.lon-capa.org.
50:
1.971 jms 51: =head1 Package Variables
52:
53: These are largely undocumented, so if you decipher one please note it here.
54:
55: =over 4
56:
57: =item $processmarker
58:
59: Contains the time this process was started and this servers host id.
60:
61: =item $dumpcount
62:
63: Counts the number of times a message log flush has been attempted (regardless
64: of success) by this process. Used as part of the filename when messages are
65: delayed.
66:
67: =back
68:
69: =cut
70:
1.1 albertel 71: package Apache::lonnet;
72:
73: use strict;
1.8 www 74: use LWP::UserAgent();
1.486 www 75: use HTTP::Date;
1.977 amueller 76: use Image::Magick;
1.1172.2.107 raeburn 77: use CGI::Cookie;
1.977 amueller 78:
1.1172.2.104 raeburn 79: use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir $deftex
1.1138 raeburn 80: $_64bit %env %protocol %loncaparevs %serverhomeIDs %needsrelease
1.1172.2.114 raeburn 81: %managerstab $passwdmin);
1.871 albertel 82:
83: my (%badServerCache, $memcache, %courselogs, %accesshash, %domainrolehash,
84: %userrolehash, $processmarker, $dumpcount, %coursedombuf,
85: %coursenumbuf, %coursehombuf, %coursedescrbuf, %courseinstcodebuf,
1.958 www 86: %courseownerbuf, %coursetypebuf,$locknum);
1.403 www 87:
1.1 albertel 88: use IO::Socket;
1.31 www 89: use GDBM_File;
1.208 albertel 90: use HTML::LCParser;
1.88 www 91: use Fcntl qw(:flock);
1.870 albertel 92: use Storable qw(thaw nfreeze);
1.1172.2.79 raeburn 93: use Time::HiRes qw( sleep gettimeofday tv_interval );
1.599 albertel 94: use Cache::Memcached;
1.676 albertel 95: use Digest::MD5;
1.790 albertel 96: use Math::Random;
1.1024 raeburn 97: use File::MMagic;
1.1172.2.142 raeburn 98: use Net::CIDR;
99: use Sys::Hostname::FQDN();
1.807 albertel 100: use LONCAPA qw(:DEFAULT :match);
1.740 www 101: use LONCAPA::Configuration;
1.1160 www 102: use LONCAPA::lonmetadata;
1.1172.2.22 raeburn 103: use LONCAPA::Lond;
1.1172.2.110 raeburn 104: use LONCAPA::transliterate;
1.1117 foxr 105:
1.1090 raeburn 106: use File::Copy;
1.676 albertel 107:
1.195 www 108: my $readit;
1.1172.2.79 raeburn 109: my $max_connection_retries = 20; # Or some such value.
1.1 albertel 110:
1.619 albertel 111: require Exporter;
112:
113: our @ISA = qw (Exporter);
114: our @EXPORT = qw(%env);
115:
1.1172.2.9 raeburn 116: # ------------------------------------ Logging (parameters, docs, slots, roles)
1.729 www 117: {
118: my $logid;
1.1172.2.9 raeburn 119: sub write_log {
120: my ($context,$hash_name,$storehash,$delflag,$uname,$udom,$cnum,$cdom)=@_;
121: if ($context eq 'course') {
122: if (($cnum eq '') || ($cdom eq '')) {
123: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
124: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
125: }
1.957 raeburn 126: }
1.1172.2.13 raeburn 127: $logid ++;
1.957 raeburn 128: my $now = time();
129: my $id=$now.'00000'.$$.'00000'.$logid;
1.1172.2.146. .1(raebu 130:22): my $ip = &get_requestor_ip();
1.1172.2.9 raeburn 131: my $logentry = {
132: $id => {
133: 'exe_uname' => $env{'user.name'},
134: 'exe_udom' => $env{'user.domain'},
135: 'exe_time' => $now,
1.1172.2.134 raeburn 136: 'exe_ip' => $ip,
1.1172.2.9 raeburn 137: 'delflag' => $delflag,
138: 'logentry' => $storehash,
139: 'uname' => $uname,
140: 'udom' => $udom,
141: }
142: };
143: return &put('nohist_'.$hash_name,$logentry,$cdom,$cnum);
1.729 www 144: }
145: }
1.1 albertel 146:
1.163 harris41 147: sub logtouch {
148: my $execdir=$perlvar{'lonDaemons'};
1.448 albertel 149: unless (-e "$execdir/logs/lonnet.log") {
1.1172.2.96 raeburn 150: open(my $fh,">>","$execdir/logs/lonnet.log");
1.163 harris41 151: close $fh;
152: }
153: my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
154: chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
155: }
156:
1.1 albertel 157: sub logthis {
158: my $message=shift;
159: my $execdir=$perlvar{'lonDaemons'};
160: my $now=time;
161: my $local=localtime($now);
1.1172.2.96 raeburn 162: if (open(my $fh,">>","$execdir/logs/lonnet.log")) {
1.986 foxr 163: my $logstring = $local. " ($$): ".$message."\n"; # Keep any \'s in string.
164: print $fh $logstring;
1.448 albertel 165: close($fh);
166: }
1.1 albertel 167: return 1;
168: }
169:
170: sub logperm {
171: my $message=shift;
172: my $execdir=$perlvar{'lonDaemons'};
173: my $now=time;
174: my $local=localtime($now);
1.1172.2.96 raeburn 175: if (open(my $fh,">>","$execdir/logs/lonnet.perm.log")) {
1.448 albertel 176: print $fh "$now:$message:$local\n";
177: close($fh);
178: }
1.1 albertel 179: return 1;
180: }
181:
1.850 albertel 182: sub create_connection {
1.853 albertel 183: my ($hostname,$lonid) = @_;
1.851 albertel 184: my $client=IO::Socket::UNIX->new(Peer => $perlvar{'lonSockCreate'},
1.850 albertel 185: Type => SOCK_STREAM,
186: Timeout => 10);
187: return 0 if (!$client);
1.890 albertel 188: print $client (join(':',$hostname,$lonid,&machine_ids($hostname))."\n");
1.850 albertel 189: my $result = <$client>;
190: chomp($result);
191: return 1 if ($result eq 'done');
192: return 0;
193: }
194:
1.983 raeburn 195: sub get_server_timezone {
196: my ($cnum,$cdom) = @_;
197: my $home=&homeserver($cnum,$cdom);
198: if ($home ne 'no_host') {
199: my $cachetime = 24*3600;
200: my ($timezone,$cached)=&is_cached_new('servertimezone',$home);
201: if (defined($cached)) {
202: return $timezone;
203: } else {
204: my $timezone = &reply('servertimezone',$home);
205: return &do_cache_new('servertimezone',$home,$timezone,$cachetime);
206: }
207: }
208: }
1.850 albertel 209:
1.1106 raeburn 210: sub get_server_distarch {
211: my ($lonhost,$ignore_cache) = @_;
212: if (defined($lonhost)) {
213: if (!defined(&hostname($lonhost))) {
214: return;
215: }
216: my $cachetime = 12*3600;
217: if (!$ignore_cache) {
218: my ($distarch,$cached)=&is_cached_new('serverdistarch',$lonhost);
219: if (defined($cached)) {
220: return $distarch;
221: }
222: }
223: my $rep = &reply('serverdistarch',$lonhost);
224: unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' ||
225: $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
226: $rep eq '') {
227: return &do_cache_new('serverdistarch',$lonhost,$rep,$cachetime);
228: }
229: }
230: return;
231: }
232:
1.993 raeburn 233: sub get_server_loncaparev {
1.1073 raeburn 234: my ($dom,$lonhost,$ignore_cache,$caller) = @_;
1.993 raeburn 235: if (defined($lonhost)) {
236: if (!defined(&hostname($lonhost))) {
237: undef($lonhost);
238: }
239: }
240: if (!defined($lonhost)) {
241: if (defined(&domain($dom,'primary'))) {
242: $lonhost=&domain($dom,'primary');
243: if ($lonhost eq 'no_host') {
244: undef($lonhost);
245: }
246: }
247: }
248: if (defined($lonhost)) {
1.1073 raeburn 249: my $cachetime = 12*3600;
250: if (!$ignore_cache) {
251: my ($loncaparev,$cached)=&is_cached_new('serverloncaparev',$lonhost);
252: if (defined($cached)) {
253: return $loncaparev;
254: }
255: }
256: my ($answer,$loncaparev);
257: my @ids=¤t_machine_ids();
258: if (grep(/^\Q$lonhost\E$/,@ids)) {
259: $answer = $perlvar{'lonVersion'};
1.1081 raeburn 260: if ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
1.1073 raeburn 261: $loncaparev = $1;
262: }
263: } else {
264: $answer = &reply('serverloncaparev',$lonhost);
265: if (($answer eq 'unknown_cmd') || ($answer eq 'con_lost')) {
266: if ($caller eq 'loncron') {
267: my $ua=new LWP::UserAgent;
1.1082 raeburn 268: $ua->timeout(4);
1.1172.2.120 raeburn 269: my $hostname = &hostname($lonhost);
1.1073 raeburn 270: my $protocol = $protocol{$lonhost};
271: $protocol = 'http' if ($protocol ne 'https');
1.1172.2.120 raeburn 272: my $url = $protocol.'://'.$hostname.'/adm/about.html';
1.1073 raeburn 273: my $request=new HTTP::Request('GET',$url);
274: my $response=$ua->request($request);
275: unless ($response->is_error()) {
276: my $content = $response->content;
1.1081 raeburn 277: if ($content =~ /<p>VERSION\:\s*([\w.\-]+)<\/p>/) {
1.1073 raeburn 278: $loncaparev = $1;
279: }
280: }
281: } else {
282: $loncaparev = $loncaparevs{$lonhost};
283: }
1.1081 raeburn 284: } elsif ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
1.1073 raeburn 285: $loncaparev = $1;
286: }
1.993 raeburn 287: }
1.1073 raeburn 288: return &do_cache_new('serverloncaparev',$lonhost,$loncaparev,$cachetime);
1.993 raeburn 289: }
290: }
291:
1.1074 raeburn 292: sub get_server_homeID {
293: my ($hostname,$ignore_cache,$caller) = @_;
294: unless ($ignore_cache) {
295: my ($serverhomeID,$cached)=&is_cached_new('serverhomeID',$hostname);
296: if (defined($cached)) {
297: return $serverhomeID;
298: }
299: }
300: my $cachetime = 12*3600;
301: my $serverhomeID;
302: if ($caller eq 'loncron') {
303: my @machine_ids = &machine_ids($hostname);
304: foreach my $id (@machine_ids) {
305: my $response = &reply('serverhomeID',$id);
306: unless (($response eq 'unknown_cmd') || ($response eq 'con_lost')) {
307: $serverhomeID = $response;
308: last;
309: }
310: }
311: if ($serverhomeID eq '') {
312: $serverhomeID = $machine_ids[-1];
313: }
314: } else {
315: $serverhomeID = $serverhomeIDs{$hostname};
316: }
317: return &do_cache_new('serverhomeID',$hostname,$serverhomeID,$cachetime);
318: }
319:
1.1121 raeburn 320: sub get_remote_globals {
321: my ($lonhost,$whathash,$ignore_cache) = @_;
1.1125 raeburn 322: my ($result,%returnhash,%whatneeded);
323: if (ref($whathash) eq 'HASH') {
1.1121 raeburn 324: foreach my $what (sort(keys(%{$whathash}))) {
325: my $hashid = $lonhost.'-'.$what;
1.1125 raeburn 326: my ($response,$cached);
1.1121 raeburn 327: unless ($ignore_cache) {
1.1125 raeburn 328: ($response,$cached)=&is_cached_new('lonnetglobal',$hashid);
1.1121 raeburn 329: }
330: if (defined($cached)) {
1.1125 raeburn 331: $returnhash{$what} = $response;
1.1121 raeburn 332: } else {
1.1125 raeburn 333: $whatneeded{$what} = 1;
1.1121 raeburn 334: }
335: }
1.1125 raeburn 336: if (keys(%whatneeded) == 0) {
337: $result = 'ok';
338: } else {
1.1121 raeburn 339: my $requested = &freeze_escape(\%whatneeded);
340: my $rep=&reply('readlonnetglobal:'.$requested,$lonhost);
1.1125 raeburn 341: if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
342: ($rep eq 'unknown_cmd')) {
343: $result = $rep;
344: } else {
345: $result = 'ok';
1.1121 raeburn 346: my @pairs=split(/\&/,$rep);
1.1125 raeburn 347: foreach my $item (@pairs) {
348: my ($key,$value)=split(/=/,$item,2);
349: my $what = &unescape($key);
350: my $hashid = $lonhost.'-'.$what;
351: $returnhash{$what}=&thaw_unescape($value);
352: &do_cache_new('lonnetglobal',$hashid,$returnhash{$what},600);
1.1121 raeburn 353: }
354: }
355: }
356: }
1.1125 raeburn 357: return ($result,\%returnhash);
1.1121 raeburn 358: }
359:
1.1124 raeburn 360: sub remote_devalidate_cache {
1.1172.2.35 raeburn 361: my ($lonhost,$cachekeys) = @_;
362: my $items;
363: return unless (ref($cachekeys) eq 'ARRAY');
364: my $cachestr = join('&',@{$cachekeys});
365: return &reply('devalidatecache:'.&escape($cachestr),$lonhost);
1.1124 raeburn 366: }
367:
1.1172.2.146. .13(raeb 368:-23): sub sign_lti {
369:-23): my ($cdom,$cnum,$crsdef,$type,$context,$url,$ltinum,$keynum,$paramsref,$inforef) = @_;
370:-23): my $chome;
371:-23): if (&domain($cdom) ne '') {
372:-23): if ($crsdef) {
373:-23): $chome = &homeserver($cnum,$cdom);
374:-23): } else {
375:-23): $chome = &domain($cdom,'primary');
376:-23): }
377:-23): }
378:-23): if ($cdom && $chome && ($chome ne 'no_host')) {
379:-23): if ((ref($paramsref) eq 'HASH') &&
380:-23): (ref($inforef) eq 'HASH')) {
381:-23): my $rep;
382:-23): if (grep { $_ eq $chome } ¤t_machine_ids()) {
383:-23): # domain information is hosted on this machine
384:-23): $rep =
385:-23): &LONCAPA::Lond::sign_lti_payload($cdom,$cnum,$crsdef,$type,
386:-23): $context,$url,$ltinum,$keynum,
387:-23): $perlvar{'lonVersion'},
388:-23): $paramsref,$inforef);
389:-23): if (ref($rep) eq 'HASH') {
390:-23): return ('ok',$rep);
391:-23): }
392:-23): } else {
393:-23): my ($escurl,$params,$info);
394:-23): $escurl = &escape($url);
395:-23): if (ref($paramsref) eq 'HASH') {
396:-23): $params = &freeze_escape($paramsref);
397:-23): }
398:-23): if (ref($inforef) eq 'HASH') {
399:-23): $info = &freeze_escape($inforef);
400:-23): }
401:-23): $rep=&reply("encrypt:signlti:$cdom:$cnum:$crsdef:$type:$context:$escurl:$ltinum:$keynum:$params:$info",$chome);
402:-23): }
403:-23): if (($rep eq '') || ($rep =~ /^con_lost|error|no_such_host|unknown_cmd/i)) {
404:-23): return ();
405:-23): } elsif (($inforef->{'respfmt'} eq 'to_post_body') ||
406:-23): ($inforef->{'respfmt'} eq 'to_authorization_header')) {
407:-23): return ('ok',$rep);
408:-23): } else {
409:-23): my %returnhash;
410:-23): foreach my $item (split(/\&/,$rep)) {
411:-23): my ($name,$value)=split(/\=/,$item);
412:-23): $returnhash{&unescape($name)}=&thaw_unescape($value);
413:-23): }
414:-23): return('ok',\%returnhash);
415:-23): }
416:-23): } else {
417:-23): return ();
418:-23): }
419:-23): } else {
420:-23): return ();
421:-23): &logthis("sign_lti failed - no homeserver and/or domain ($cdom) ($chome)");
422:-23): }
423:-23): }
424:-23):
1.1 albertel 425: # -------------------------------------------------- Non-critical communication
426: sub subreply {
427: my ($cmd,$server)=@_;
1.838 albertel 428: my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
1.549 foxr 429: #
430: # With loncnew process trimming, there's a timing hole between lonc server
431: # process exit and the master server picking up the listen on the AF_UNIX
432: # socket. In that time interval, a lock file will exist:
433:
434: my $lockfile=$peerfile.".lock";
435: while (-e $lockfile) { # Need to wait for the lockfile to disappear.
1.1172.2.79 raeburn 436: sleep(0.1);
1.549 foxr 437: }
438: # At this point, either a loncnew parent is listening or an old lonc
1.550 foxr 439: # or loncnew child is listening so we can connect or everything's dead.
1.549 foxr 440: #
1.550 foxr 441: # We'll give the connection a few tries before abandoning it. If
442: # connection is not possible, we'll con_lost back to the client.
443: #
444: my $client;
445: for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
446: $client=IO::Socket::UNIX->new(Peer =>"$peerfile",
447: Type => SOCK_STREAM,
448: Timeout => 10);
1.869 albertel 449: if ($client) {
1.550 foxr 450: last; # Connected!
1.850 albertel 451: } else {
1.853 albertel 452: &create_connection(&hostname($server),$server);
1.550 foxr 453: }
1.1172.2.79 raeburn 454: sleep(0.1); # Try again later if failed connection.
1.550 foxr 455: }
456: my $answer;
457: if ($client) {
1.704 albertel 458: print $client "sethost:$server:$cmd\n";
1.550 foxr 459: $answer=<$client>;
460: if (!$answer) { $answer="con_lost"; }
461: chomp($answer);
462: } else {
463: $answer = 'con_lost'; # Failed connection.
464: }
1.1 albertel 465: return $answer;
466: }
467:
468: sub reply {
469: my ($cmd,$server)=@_;
1.838 albertel 470: unless (defined(&hostname($server))) { return 'no_such_host'; }
1.1 albertel 471: my $answer=subreply($cmd,$server);
1.65 www 472: if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
1.1172.2.111 raeburn 473: my $logged = $cmd;
474: if ($cmd =~ /^encrypt:([^:]+):/) {
475: my $subcmd = $1;
476: if (($subcmd eq 'auth') || ($subcmd eq 'passwd') ||
477: ($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
1.1172.2.146. .13(raeb 478:-23): ($subcmd eq 'putdom') || ($subcmd eq 'autoexportgrades') ||
479:-23): ($subcmd eq 'put')) {
1.1172.2.111 raeburn 480: (undef,undef,my @rest) = split(/:/,$cmd);
481: if (($subcmd eq 'auth') || ($subcmd eq 'putdom')) {
482: splice(@rest,2,1,'Hidden');
483: } elsif ($subcmd eq 'passwd') {
484: splice(@rest,2,2,('Hidden','Hidden'));
485: } elsif (($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
1.1172.2.146. .13(raeb 486:-23): ($subcmd eq 'autoexportgrades') || ($subcmd eq 'put')) {
1.1172.2.111 raeburn 487: splice(@rest,3,1,'Hidden');
488: }
489: $logged = join(':',('encrypt:'.$subcmd,@rest));
490: }
491: }
492: &logthis("<font color=\"blue\">WARNING:".
493: " $logged to $server returned $answer</font>");
1.12 www 494: }
1.1 albertel 495: return $answer;
496: }
497:
498: # ----------------------------------------------------------- Send USR1 to lonc
499:
500: sub reconlonc {
1.891 albertel 501: my ($lonid) = @_;
502: if ($lonid) {
1.1172.2.72 raeburn 503: my $hostname = &hostname($lonid);
1.891 albertel 504: my $peerfile="$perlvar{'lonSockDir'}/$hostname";
505: if ($hostname && -e $peerfile) {
506: &logthis("Trying to reconnect lonc for $lonid ($hostname)");
507: my $client=IO::Socket::UNIX->new(Peer => $peerfile,
508: Type => SOCK_STREAM,
509: Timeout => 10);
510: if ($client) {
511: print $client ("reset_retries\n");
512: my $answer=<$client>;
513: #reset just this one.
514: }
515: }
516: return;
517: }
518:
1.836 www 519: &logthis("Trying to reconnect lonc");
1.1 albertel 520: my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
1.1172.2.96 raeburn 521: if (open(my $fh,"<",$loncfile)) {
1.1 albertel 522: my $loncpid=<$fh>;
523: chomp($loncpid);
524: if (kill 0 => $loncpid) {
525: &logthis("lonc at pid $loncpid responding, sending USR1");
526: kill USR1 => $loncpid;
527: sleep 1;
1.836 www 528: } else {
1.12 www 529: &logthis(
1.672 albertel 530: "<font color=\"blue\">WARNING:".
1.12 www 531: " lonc at pid $loncpid not responding, giving up</font>");
1.1 albertel 532: }
533: } else {
1.836 www 534: &logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
1.1 albertel 535: }
536: }
537:
538: # ------------------------------------------------------ Critical communication
1.12 www 539:
1.1 albertel 540: sub critical {
541: my ($cmd,$server)=@_;
1.838 albertel 542: unless (&hostname($server)) {
1.672 albertel 543: &logthis("<font color=\"blue\">WARNING:".
1.89 www 544: " Critical message to unknown server ($server)</font>");
545: return 'no_such_host';
546: }
1.1 albertel 547: my $answer=reply($cmd,$server);
548: if ($answer eq 'con_lost') {
1.1172.2.72 raeburn 549: &reconlonc($server);
1.589 albertel 550: my $answer=reply($cmd,$server);
1.1 albertel 551: if ($answer eq 'con_lost') {
552: my $now=time;
553: my $middlename=$cmd;
1.5 www 554: $middlename=substr($middlename,0,16);
1.1 albertel 555: $middlename=~s/\W//g;
556: my $dfilename=
1.305 www 557: "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
558: $dumpcount++;
1.1 albertel 559: {
1.448 albertel 560: my $dfh;
1.1172.2.96 raeburn 561: if (open($dfh,">",$dfilename)) {
1.448 albertel 562: print $dfh "$cmd\n";
563: close($dfh);
564: }
1.1 albertel 565: }
1.1172.2.79 raeburn 566: sleep 1;
1.1 albertel 567: my $wcmd='';
568: {
1.448 albertel 569: my $dfh;
1.1172.2.96 raeburn 570: if (open($dfh,"<",$dfilename)) {
1.448 albertel 571: $wcmd=<$dfh>;
572: close($dfh);
573: }
1.1 albertel 574: }
575: chomp($wcmd);
1.7 www 576: if ($wcmd eq $cmd) {
1.672 albertel 577: &logthis("<font color=\"blue\">WARNING: ".
1.12 www 578: "Connection buffer $dfilename: $cmd</font>");
1.1 albertel 579: &logperm("D:$server:$cmd");
580: return 'con_delayed';
581: } else {
1.672 albertel 582: &logthis("<font color=\"red\">CRITICAL:"
1.12 www 583: ." Critical connection failed: $server $cmd</font>");
1.1 albertel 584: &logperm("F:$server:$cmd");
585: return 'con_failed';
586: }
587: }
588: }
589: return $answer;
1.405 albertel 590: }
591:
1.755 albertel 592: # ------------------------------------------- check if return value is an error
593:
594: sub error {
595: my ($result) = @_;
1.756 albertel 596: if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
1.755 albertel 597: if ($2 == 2) { return undef; }
598: return $1;
599: }
600: return undef;
601: }
602:
1.783 albertel 603: sub convert_and_load_session_env {
604: my ($lonidsdir,$handle)=@_;
605: my @profile;
606: {
1.917 albertel 607: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
608: if (!$opened) {
1.915 albertel 609: return 0;
610: }
1.783 albertel 611: flock($idf,LOCK_SH);
612: @profile=<$idf>;
613: close($idf);
614: }
615: my %temp_env;
616: foreach my $line (@profile) {
1.786 albertel 617: if ($line !~ m/=/) {
618: return 0;
619: }
1.783 albertel 620: chomp($line);
621: my ($envname,$envvalue)=split(/=/,$line,2);
622: $temp_env{&unescape($envname)} = &unescape($envvalue);
623: }
624: unlink("$lonidsdir/$handle.id");
625: if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
626: 0640)) {
627: %disk_env = %temp_env;
628: @env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
629: untie(%disk_env);
630: }
1.786 albertel 631: return 1;
1.783 albertel 632: }
633:
1.374 www 634: # ------------------------------------------- Transfer profile into environment
1.780 albertel 635: my $env_loaded;
636: sub transfer_profile_to_env {
1.788 albertel 637: my ($lonidsdir,$handle,$force_transfer) = @_;
638: if (!$force_transfer && $env_loaded) { return; }
1.374 www 639:
1.720 albertel 640: if (!defined($lonidsdir)) {
641: $lonidsdir = $perlvar{'lonIDsDir'};
642: }
643: if (!defined($handle)) {
644: ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
645: }
646:
1.786 albertel 647: my $convert;
648: {
1.917 albertel 649: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
650: if (!$opened) {
1.915 albertel 651: return;
652: }
1.786 albertel 653: flock($idf,LOCK_SH);
654: if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
655: &GDBM_READER(),0640)) {
656: @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
657: untie(%disk_env);
658: } else {
659: $convert = 1;
660: }
661: }
662: if ($convert) {
663: if (!&convert_and_load_session_env($lonidsdir,$handle)) {
664: &logthis("Failed to load session, or convert session.");
665: }
1.374 www 666: }
1.783 albertel 667:
1.786 albertel 668: my %remove;
1.783 albertel 669: while ( my $envname = each(%env) ) {
1.433 matthew 670: if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
671: if ($time < time-300) {
1.783 albertel 672: $remove{$key}++;
1.433 matthew 673: }
674: }
675: }
1.783 albertel 676:
1.619 albertel 677: $env{'user.environment'} = "$lonidsdir/$handle.id";
1.780 albertel 678: $env_loaded=1;
1.783 albertel 679: foreach my $expired_key (keys(%remove)) {
1.433 matthew 680: &delenv($expired_key);
1.374 www 681: }
1.1 albertel 682: }
683:
1.916 albertel 684: # ---------------------------------------------------- Check for valid session
685: sub check_for_valid_session {
1.1172.2.96 raeburn 686: my ($r,$name,$userhashref,$domref) = @_;
1.916 albertel 687: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
1.1172.2.108 raeburn 688: my ($lonidsdir,$linkname,$pubname,$secure,$lonid);
1.1155 raeburn 689: if ($name eq 'lonDAV') {
690: $lonidsdir=$r->dir_config('lonDAVsessDir');
691: } else {
692: $lonidsdir=$r->dir_config('lonIDsDir');
1.1172.2.108 raeburn 693: if ($name eq '') {
694: $name = 'lonID';
695: }
696: }
697: if ($name eq 'lonID') {
698: $secure = 'lonSID';
699: $linkname = 'lonLinkID';
700: $pubname = 'lonPubID';
701: if (exists($cookies{$secure})) {
702: $lonid=$cookies{$secure};
703: } elsif (exists($cookies{$name})) {
704: $lonid=$cookies{$name};
705: } elsif ((exists($cookies{$linkname})) && ($ENV{'SERVER_PORT'} != 443)) {
706: $lonid=$cookies{$linkname};
707: } elsif (exists($cookies{$pubname})) {
708: $lonid=$cookies{$pubname};
709: }
710: } else {
711: $lonid=$cookies{$name};
712: }
713: return undef if (!$lonid);
714:
715: my $handle=&LONCAPA::clean_handle($lonid->value);
716: if (-l "$lonidsdir/$handle.id") {
717: my $link = readlink("$lonidsdir/$handle.id");
718: if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
719: $handle = $1;
720: }
1.1155 raeburn 721: }
1.1172.2.96 raeburn 722: if (!-e "$lonidsdir/$handle.id") {
723: if ((ref($domref)) && ($name eq 'lonID') &&
724: ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
725: my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
726: if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
727: $$domref = $possudom;
728: }
729: }
730: return undef;
731: }
1.916 albertel 732:
1.917 albertel 733: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
734: return undef if (!$opened);
1.916 albertel 735:
736: flock($idf,LOCK_SH);
737: my %disk_env;
738: if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
739: &GDBM_READER(),0640)) {
740: return undef;
741: }
742:
743: if (!defined($disk_env{'user.name'})
744: || !defined($disk_env{'user.domain'})) {
1.1172.2.107 raeburn 745: untie(%disk_env);
1.916 albertel 746: return undef;
747: }
1.1172.2.18 raeburn 748:
1.1172.2.36 raeburn 749: if (ref($userhashref) eq 'HASH') {
750: $userhashref->{'name'} = $disk_env{'user.name'};
751: $userhashref->{'domain'} = $disk_env{'user.domain'};
1.1172.2.142 raeburn 752: if ($disk_env{'request.role'}) {
753: $userhashref->{'role'} = $disk_env{'request.role'};
754: }
1.1172.2.146. .13(raeb 755:-23): $userhashref->{'lti'} = $disk_env{'request.lti.login'};
756:-23): if ($userhashref->{'lti'}) {
757:-23): $userhashref->{'ltitarget'} = $disk_env{'request.lti.target'};
758:-23): $userhashref->{'ltiuri'} = $disk_env{'request.lti.uri'};
759:-23): }
1.1172.2.18 raeburn 760: }
1.1172.2.107 raeburn 761: untie(%disk_env);
1.1172.2.18 raeburn 762:
1.916 albertel 763: return $handle;
764: }
765:
1.830 albertel 766: sub timed_flock {
767: my ($file,$lock_type) = @_;
768: my $failed=0;
769: eval {
770: local $SIG{__DIE__}='DEFAULT';
771: local $SIG{ALRM}=sub {
772: $failed=1;
773: die("failed lock");
774: };
775: alarm(13);
776: flock($file,$lock_type);
777: alarm(0);
778: };
779: if ($failed) {
780: return undef;
781: } else {
782: return 1;
783: }
784: }
785:
1.1172.2.107 raeburn 786: sub get_sessionfile_vars {
787: my ($handle,$lonidsdir,$storearr) = @_;
788: my %returnhash;
789: unless (ref($storearr) eq 'ARRAY') {
790: return %returnhash;
791: }
792: if (-l "$lonidsdir/$handle.id") {
793: my $link = readlink("$lonidsdir/$handle.id");
794: if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
795: $handle = $1;
796: }
797: }
798: if ((-e "$lonidsdir/$handle.id") &&
799: ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
800: my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
801: if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
802: if (open(my $idf,'+<',"$lonidsdir/$handle.id")) {
803: flock($idf,LOCK_SH);
804: if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
805: &GDBM_READER(),0640)) {
806: foreach my $item (@{$storearr}) {
807: $returnhash{$item} = $disk_env{$item};
808: }
809: untie(%disk_env);
810: }
811: }
812: }
813: }
814: return %returnhash;
815: }
816:
1.5 www 817: # ---------------------------------------------------------- Append Environment
818:
819: sub appenv {
1.949 raeburn 820: my ($newenv,$roles) = @_;
821: if (ref($newenv) eq 'HASH') {
822: foreach my $key (keys(%{$newenv})) {
823: my $refused = 0;
824: if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
825: $refused = 1;
826: if (ref($roles) eq 'ARRAY') {
1.1172.2.40 raeburn 827: my ($type,$role) = ($key =~ m{^user\.(role|priv)\.(.+?)\./});
1.949 raeburn 828: if (grep(/^\Q$role\E$/,@{$roles})) {
829: $refused = 0;
830: }
831: }
832: }
833: if ($refused) {
834: &logthis("<font color=\"blue\">WARNING: ".
835: "Attempt to modify environment ".$key." to ".$newenv->{$key}
836: .'</font>');
837: delete($newenv->{$key});
838: } else {
839: $env{$key}=$newenv->{$key};
840: }
841: }
1.1172.2.96 raeburn 842: my $lonids = $perlvar{'lonIDsDir'};
843: if ($env{'user.environment'} =~ m{^\Q$lonids/\E$match_username\_\d+\_$match_domain\_[\w\-.]+\.id$}) {
844: my $opened = open(my $env_file,'+<',$env{'user.environment'});
845: if ($opened
846: && &timed_flock($env_file,LOCK_EX)
847: &&
848: tie(my %disk_env,'GDBM_File',$env{'user.environment'},
849: (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
850: while (my ($key,$value) = each(%{$newenv})) {
851: $disk_env{$key} = $value;
852: }
853: untie(%disk_env);
854: }
1.35 www 855: }
1.191 harris41 856: }
1.56 www 857: return 'ok';
858: }
859: # ----------------------------------------------------- Delete from Environment
860:
861: sub delenv {
1.1104 raeburn 862: my ($delthis,$regexp,$roles) = @_;
863: if (($delthis=~/^user\.role/) || ($delthis=~/^user\.priv/)) {
864: my $refused = 1;
865: if (ref($roles) eq 'ARRAY') {
866: my ($type,$role) = ($delthis =~ /^user\.(role|priv)\.([^.]+)\./);
867: if (grep(/^\Q$role\E$/,@{$roles})) {
868: $refused = 0;
869: }
870: }
871: if ($refused) {
872: &logthis("<font color=\"blue\">WARNING: ".
873: "Attempt to delete from environment ".$delthis);
874: return 'error';
875: }
1.56 www 876: }
1.917 albertel 877: my $opened = open(my $env_file,'+<',$env{'user.environment'});
878: if ($opened
1.915 albertel 879: && &timed_flock($env_file,LOCK_EX)
1.830 albertel 880: &&
881: tie(my %disk_env,'GDBM_File',$env{'user.environment'},
882: (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783 albertel 883: foreach my $key (keys(%disk_env)) {
1.987 raeburn 884: if ($regexp) {
885: if ($key=~/^$delthis/) {
886: delete($env{$key});
887: delete($disk_env{$key});
888: }
889: } else {
890: if ($key=~/^\Q$delthis\E/) {
891: delete($env{$key});
892: delete($disk_env{$key});
893: }
894: }
1.448 albertel 895: }
1.783 albertel 896: untie(%disk_env);
1.5 www 897: }
898: return 'ok';
1.369 albertel 899: }
900:
1.790 albertel 901: sub get_env_multiple {
902: my ($name) = @_;
903: my @values;
904: if (defined($env{$name})) {
905: # exists is it an array
906: if (ref($env{$name})) {
907: @values=@{ $env{$name} };
908: } else {
909: $values[0]=$env{$name};
910: }
911: }
912: return(@values);
913: }
914:
1.958 www 915: # ------------------------------------------------------------------- Locking
916:
917: sub set_lock {
918: my ($text)=@_;
919: $locknum++;
920: my $id=$$.'-'.$locknum;
921: &appenv({'session.locks' => $env{'session.locks'}.','.$id,
922: 'session.lock.'.$id => $text});
923: return $id;
924: }
925:
926: sub get_locks {
927: my $num=0;
928: my %texts=();
929: foreach my $lock (split(/\,/,$env{'session.locks'})) {
930: if ($lock=~/\w/) {
931: $num++;
932: $texts{$lock}=$env{'session.lock.'.$lock};
933: }
934: }
935: return ($num,%texts);
936: }
937:
938: sub remove_lock {
939: my ($id)=@_;
940: my $newlocks='';
941: foreach my $lock (split(/\,/,$env{'session.locks'})) {
942: if (($lock=~/\w/) && ($lock ne $id)) {
943: $newlocks.=','.$lock;
944: }
945: }
946: &appenv({'session.locks' => $newlocks});
947: &delenv('session.lock.'.$id);
948: }
949:
950: sub remove_all_locks {
951: my $activelocks=$env{'session.locks'};
952: foreach my $lock (split(/\,/,$env{'session.locks'})) {
953: if ($lock=~/\w/) {
954: &remove_lock($lock);
955: }
956: }
957: }
958:
959:
1.369 albertel 960: # ------------------------------------------ Find out current server userload
961: sub userload {
962: my $numusers=0;
963: {
964: opendir(LONIDS,$perlvar{'lonIDsDir'});
965: my $filename;
966: my $curtime=time;
967: while ($filename=readdir(LONIDS)) {
1.925 albertel 968: next if ($filename eq '.' || $filename eq '..');
969: next if ($filename =~ /publicuser_\d+\.id/);
1.1172.2.112 raeburn 970: next if ($filename =~ /^[a-f0-9]+_linked\.id$/);
1.404 albertel 971: my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
1.437 albertel 972: if ($curtime-$mtime < 1800) { $numusers++; }
1.369 albertel 973: }
974: closedir(LONIDS);
975: }
976: my $userloadpercent=0;
977: my $maxuserload=$perlvar{'lonUserLoadLim'};
978: if ($maxuserload) {
1.371 albertel 979: $userloadpercent=100*$numusers/$maxuserload;
1.369 albertel 980: }
1.372 albertel 981: $userloadpercent=sprintf("%.2f",$userloadpercent);
1.369 albertel 982: return $userloadpercent;
1.283 www 983: }
984:
1.1 albertel 985: # ------------------------------ Find server with least workload from spare.tab
1.11 www 986:
1.1 albertel 987: sub spareserver {
1.1172.2.142 raeburn 988: my ($r,$loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
1.784 albertel 989: my $spare_server;
1.370 albertel 990: if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
1.784 albertel 991: my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent
992: : $userloadpercent;
1.1083 raeburn 993: my ($uint_dom,$remotesessions);
994: if (($udom ne '') && (&domain($udom) ne '')) {
995: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
996: $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
997: my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
998: $remotesessions = $udomdefaults{'remotesessions'};
999: }
1.1123 raeburn 1000: my $spareshash = &this_host_spares($udom);
1001: if (ref($spareshash) eq 'HASH') {
1002: if (ref($spareshash->{'primary'}) eq 'ARRAY') {
1003: foreach my $try_server (@{ $spareshash->{'primary'} }) {
1.1172.2.62 raeburn 1004: next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
1005: $try_server));
1.1123 raeburn 1006: ($spare_server, $lowest_load) =
1007: &compare_server_load($try_server, $spare_server, $lowest_load);
1008: }
1.1083 raeburn 1009: }
1.784 albertel 1010:
1.1123 raeburn 1011: my $found_server = ($spare_server ne '' && $lowest_load < 100);
1012:
1013: if (!$found_server) {
1014: if (ref($spareshash->{'default'}) eq 'ARRAY') {
1015: foreach my $try_server (@{ $spareshash->{'default'} }) {
1.1172.2.62 raeburn 1016: next unless (&spare_can_host($udom,$uint_dom,
1017: $remotesessions,$try_server));
1.1123 raeburn 1018: ($spare_server, $lowest_load) =
1019: &compare_server_load($try_server, $spare_server, $lowest_load);
1020: }
1021: }
1022: }
1.784 albertel 1023: }
1024:
1025: if (!$want_server_name) {
1.1001 raeburn 1026: if (defined($spare_server)) {
1027: my $hostname = &hostname($spare_server);
1.1083 raeburn 1028: if (defined($hostname)) {
1.1172.2.120 raeburn 1029: my $protocol = 'http';
1030: if ($protocol{$spare_server} eq 'https') {
1031: $protocol = $protocol{$spare_server};
1032: }
1.1172.2.142 raeburn 1033: my $alias = &Apache::lonnet::use_proxy_alias($r,$spare_server);
1034: $hostname = $alias if ($alias ne '');
1.1001 raeburn 1035: $spare_server = $protocol.'://'.$hostname;
1036: }
1037: }
1.784 albertel 1038: }
1039: return $spare_server;
1040: }
1041:
1042: sub compare_server_load {
1.1172.2.41 raeburn 1043: my ($try_server, $spare_server, $lowest_load, $required) = @_;
1044:
1045: if ($required) {
1046: my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/);
1047: my $remoterev = &get_server_loncaparev(undef,$try_server);
1048: my ($major,$minor) = ($remoterev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
1049: if (($major eq '' && $minor eq '') ||
1050: (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) {
1051: return ($spare_server,$lowest_load);
1052: }
1053: }
1.784 albertel 1054:
1055: my $loadans = &reply('load', $try_server);
1056: my $userloadans = &reply('userload',$try_server);
1057:
1058: if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
1.1114 raeburn 1059: return ($spare_server, $lowest_load); #didn't get a number from the server
1.784 albertel 1060: }
1061:
1062: my $load;
1063: if ($loadans =~ /\d/) {
1064: if ($userloadans =~ /\d/) {
1065: #both are numbers, pick the bigger one
1066: $load = ($loadans > $userloadans) ? $loadans
1067: : $userloadans;
1.411 albertel 1068: } else {
1.784 albertel 1069: $load = $loadans;
1.411 albertel 1070: }
1.784 albertel 1071: } else {
1072: $load = $userloadans;
1073: }
1074:
1075: if (($load =~ /\d/) && ($load < $lowest_load)) {
1076: $spare_server = $try_server;
1077: $lowest_load = $load;
1.370 albertel 1078: }
1.784 albertel 1079: return ($spare_server,$lowest_load);
1.202 matthew 1080: }
1.914 albertel 1081:
1082: # --------------------------- ask offload servers if user already has a session
1083: sub find_existing_session {
1084: my ($udom,$uname) = @_;
1.1123 raeburn 1085: my $spareshash = &this_host_spares($udom);
1086: if (ref($spareshash) eq 'HASH') {
1087: if (ref($spareshash->{'primary'}) eq 'ARRAY') {
1088: foreach my $try_server (@{ $spareshash->{'primary'} }) {
1089: return $try_server if (&has_user_session($try_server, $udom, $uname));
1090: }
1091: }
1092: if (ref($spareshash->{'default'}) eq 'ARRAY') {
1093: foreach my $try_server (@{ $spareshash->{'default'} }) {
1094: return $try_server if (&has_user_session($try_server, $udom, $uname));
1095: }
1096: }
1.914 albertel 1097: }
1098: return;
1099: }
1100:
1.1172.2.146. .13(raeb 1101:-23): sub delusersession {
1102:-23): my ($lonid,$udom,$uname) = @_;
1103:-23): my $uprimary_id = &domain($udom,'primary');
1104:-23): my $uintdom = &internet_dom($uprimary_id);
1105:-23): my $intdom = &internet_dom($lonid);
1106:-23): my $serverhomedom = &host_domain($lonid);
1107:-23): if (($uintdom ne '') && ($uintdom eq $intdom)) {
1108:-23): return &reply(join(':','delusersession',
1109:-23): map {&escape($_)} ($udom,$uname)),$lonid);
1110:-23): }
1111:-23): return;
1112:-23): }
1113:-23):
1114:-23):
1.1172.2.107 raeburn 1115: # check if user's browser sent load balancer cookie and server still has session
1116: # and is not overloaded.
1117: sub check_for_balancer_cookie {
1118: my ($r,$update_mtime) = @_;
1119: my ($otherserver,$cookie);
1120: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
1121: if (exists($cookies{'balanceID'})) {
1122: my $balid = $cookies{'balanceID'};
1123: $cookie=&LONCAPA::clean_handle($balid->value);
1124: my $balancedir=$r->dir_config('lonBalanceDir');
1125: if ((-d $balancedir) && (-e "$balancedir/$cookie.id")) {
1126: if ($cookie =~ /^($match_domain)_($match_username)_[a-f0-9]+$/) {
1127: my ($possudom,$possuname) = ($1,$2);
1128: my $has_session = 0;
1129: if ((&domain($possudom) ne '') &&
1130: (&homeserver($possuname,$possudom) ne 'no_host')) {
1131: my $try_server;
1132: my $opened = open(my $idf,'+<',"$balancedir/$cookie.id");
1133: if ($opened) {
1134: flock($idf,LOCK_SH);
1135: while (my $line = <$idf>) {
1136: chomp($line);
1137: if (&hostname($line) ne '') {
1138: $try_server = $line;
1139: last;
1140: }
1141: }
1142: close($idf);
1143: if (($try_server) &&
1144: (&has_user_session($try_server,$possudom,$possuname))) {
1145: my $lowest_load = 30000;
1146: ($otherserver,$lowest_load) =
1147: &compare_server_load($try_server,undef,$lowest_load);
1148: if ($otherserver ne '' && $lowest_load < 100) {
1149: $has_session = 1;
1150: } else {
1151: undef($otherserver);
1152: }
1153: }
1154: }
1155: }
1156: if ($has_session) {
1157: if ($update_mtime) {
1158: my $atime = my $mtime = time;
1159: utime($atime,$mtime,"$balancedir/$cookie.id");
1160: }
1161: } else {
1162: unlink("$balancedir/$cookie.id");
1163: }
1164: }
1165: }
1166: }
1167: return ($otherserver,$cookie);
1168: }
1169:
1.1172.2.130 raeburn 1170: sub updatebalcookie {
1171: my ($cookie,$balancer,$lastentry)=@_;
1172: if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
1173: my ($udom,$uname) = ($1,$2);
1174: my $uprimary_id = &domain($udom,'primary');
1175: my $uintdom = &internet_dom($uprimary_id);
1176: my $intdom = &internet_dom($balancer);
1177: my $serverhomedom = &host_domain($balancer);
1178: if (($uintdom ne '') && ($uintdom eq $intdom)) {
1179: return &reply('updatebalcookie:'.&escape($cookie).':'.&escape($lastentry),$balancer);
1180: }
1181: }
1182: return;
1183: }
1184:
1.1172.2.107 raeburn 1185: sub delbalcookie {
1186: my ($cookie,$balancer) =@_;
1187: if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
1188: my ($udom,$uname) = ($1,$2);
1189: my $uprimary_id = &domain($udom,'primary');
1190: my $uintdom = &internet_dom($uprimary_id);
1191: my $intdom = &internet_dom($balancer);
1192: my $serverhomedom = &host_domain($balancer);
1193: if (($uintdom ne '') && ($uintdom eq $intdom)) {
1.1172.2.130 raeburn 1194: return &reply('delbalcookie:'.&escape($cookie),$balancer);
1.1172.2.107 raeburn 1195: }
1196: }
1197: }
1198:
1.914 albertel 1199: # -------------------------------- ask if server already has a session for user
1200: sub has_user_session {
1201: my ($lonid,$udom,$uname) = @_;
1202: my $result = &reply(join(':','userhassession',
1203: map {&escape($_)} ($udom,$uname)),$lonid);
1204: return 1 if ($result eq 'ok');
1205:
1206: return 0;
1207: }
1208:
1.1076 raeburn 1209: # --------- determine least loaded server in a user's domain which allows login
1210:
1211: sub choose_server {
1.1172.2.47 raeburn 1212: my ($udom,$checkloginvia,$required,$skiploadbal) = @_;
1.1076 raeburn 1213: my %domconfhash = &Apache::loncommon::get_domainconf($udom);
1.1077 raeburn 1214: my %servers = &get_servers($udom);
1.1076 raeburn 1215: my $lowest_load = 30000;
1.1172.2.47 raeburn 1216: my ($login_host,$hostname,$portal_path,$isredirect,$balancers);
1217: if ($skiploadbal) {
1218: ($balancers,my $cached)=&is_cached_new('loadbalancing',$udom);
1219: unless (defined($cached)) {
1220: my $cachetime = 60*60*24;
1221: my %domconfig =
1222: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
1223: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1224: $balancers = &do_cache_new('loadbalancing',$udom,$domconfig{'loadbalancing'},
1225: $cachetime);
1226: }
1227: }
1228: }
1.1076 raeburn 1229: foreach my $lonhost (keys(%servers)) {
1.1115 raeburn 1230: my $loginvia;
1.1172.2.47 raeburn 1231: if ($skiploadbal) {
1232: if (ref($balancers) eq 'HASH') {
1233: next if (exists($balancers->{$lonhost}));
1234: }
1235: }
1.1115 raeburn 1236: if ($checkloginvia) {
1237: $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
1.1116 raeburn 1238: if ($loginvia) {
1239: my ($server,$path) = split(/:/,$loginvia);
1240: ($login_host, $lowest_load) =
1.1172.2.41 raeburn 1241: &compare_server_load($server, $login_host, $lowest_load, $required);
1.1116 raeburn 1242: if ($login_host eq $server) {
1243: $portal_path = $path;
1.1151 raeburn 1244: $isredirect = 1;
1.1116 raeburn 1245: }
1246: } else {
1247: ($login_host, $lowest_load) =
1.1172.2.41 raeburn 1248: &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1.1116 raeburn 1249: if ($login_host eq $lonhost) {
1250: $portal_path = '';
1.1151 raeburn 1251: $isredirect = '';
1.1116 raeburn 1252: }
1253: }
1254: } else {
1.1076 raeburn 1255: ($login_host, $lowest_load) =
1.1172.2.41 raeburn 1256: &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1.1076 raeburn 1257: }
1258: }
1259: if ($login_host ne '') {
1.1116 raeburn 1260: $hostname = &hostname($login_host);
1.1076 raeburn 1261: }
1.1172.2.88 raeburn 1262: return ($login_host,$hostname,$portal_path,$isredirect,$lowest_load);
1.1076 raeburn 1263: }
1264:
1.1172.2.123 raeburn 1265: sub get_course_sessions {
1266: my ($cnum,$cdom,$lastactivity) = @_;
1267: my %servers = &internet_dom_servers($cdom);
1268: my %returnhash;
1269: foreach my $server (sort(keys(%servers))) {
1270: my $rep = &reply("coursesessions:$cdom:$cnum:$lastactivity",$server);
1271: my @pairs=split(/\&/,$rep);
1272: unless (($rep eq 'unknown_cmd') || ($rep =~ /^error/)) {
1273: foreach my $item (@pairs) {
1274: my ($key,$value)=split(/=/,$item,2);
1275: $key = &unescape($key);
1276: next if ($key =~ /^error: 2 /);
1277: if (exists($returnhash{$key})) {
1278: next if ($value < $returnhash{$key});
1279: }
1280: $returnhash{$key}=$value;
1281: }
1282: }
1283: }
1284: return %returnhash;
1285: }
1286:
1.202 matthew 1287: # --------------------------------------------- Try to change a user's password
1288:
1289: sub changepass {
1.799 raeburn 1290: my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
1.202 matthew 1291: $currentpass = &escape($currentpass);
1292: $newpass = &escape($newpass);
1.1030 raeburn 1293: my $lonhost = $perlvar{'lonHostID'};
1294: my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
1.202 matthew 1295: $server);
1296: if (! $answer) {
1297: &logthis("No reply on password change request to $server ".
1298: "by $uname in domain $udom.");
1299: } elsif ($answer =~ "^ok") {
1300: &logthis("$uname in $udom successfully changed their password ".
1301: "on $server.");
1302: } elsif ($answer =~ "^pwchange_failure") {
1303: &logthis("$uname in $udom was unable to change their password ".
1304: "on $server. The action was blocked by either lcpasswd ".
1305: "or pwchange");
1306: } elsif ($answer =~ "^non_authorized") {
1307: &logthis("$uname in $udom did not get their password correct when ".
1308: "attempting to change it on $server.");
1309: } elsif ($answer =~ "^auth_mode_error") {
1310: &logthis("$uname in $udom attempted to change their password despite ".
1311: "not being locally or internally authenticated on $server.");
1312: } elsif ($answer =~ "^unknown_user") {
1313: &logthis("$uname in $udom attempted to change their password ".
1314: "on $server but were unable to because $server is not ".
1315: "their home server.");
1316: } elsif ($answer =~ "^refused") {
1317: &logthis("$server refused to change $uname in $udom password because ".
1318: "it was sent an unencrypted request to change the password.");
1.1030 raeburn 1319: } elsif ($answer =~ "invalid_client") {
1320: &logthis("$server refused to change $uname in $udom password because ".
1321: "it was a reset by e-mail originating from an invalid server.");
1.1172.2.113 raeburn 1322: } elsif ($answer =~ "^prioruse") {
1323: &logthis("$server refused to change $uname in $udom password because ".
1324: "the password had been used before");
1.202 matthew 1325: }
1326: return $answer;
1.1 albertel 1327: }
1328:
1.169 harris41 1329: # ----------------------- Try to determine user's current authentication scheme
1330:
1331: sub queryauthenticate {
1332: my ($uname,$udom)=@_;
1.456 albertel 1333: my $uhome=&homeserver($uname,$udom);
1.1172.2.146. .5(raebu 1334:22): if ((!$uhome) || ($uhome eq 'no_host')) {
1.456 albertel 1335: &logthis("User $uname at $udom is unknown when looking for authentication mechanism");
1336: return 'no_host';
1337: }
1338: my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
1339: if ($answer =~ /^(unknown_user|refused|con_lost)/) {
1340: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.169 harris41 1341: }
1.456 albertel 1342: return $answer;
1.169 harris41 1343: }
1344:
1.1 albertel 1345: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11 www 1346:
1.1 albertel 1347: sub authenticate {
1.1073 raeburn 1348: my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
1.807 albertel 1349: $upass=&escape($upass);
1350: $uname= &LONCAPA::clean_username($uname);
1.836 www 1351: my $uhome=&homeserver($uname,$udom,1);
1.952 raeburn 1352: my $newhome;
1.836 www 1353: if ((!$uhome) || ($uhome eq 'no_host')) {
1354: # Maybe the machine was offline and only re-appeared again recently?
1355: &reconlonc();
1356: # One more
1.952 raeburn 1357: $uhome=&homeserver($uname,$udom,1);
1358: if (($uhome eq 'no_host') && $checkdefauth) {
1359: if (defined(&domain($udom,'primary'))) {
1360: $newhome=&domain($udom,'primary');
1361: }
1362: if ($newhome ne '') {
1363: $uhome = $newhome;
1364: }
1365: }
1.836 www 1366: if ((!$uhome) || ($uhome eq 'no_host')) {
1367: &logthis("User $uname at $udom is unknown in authenticate");
1.952 raeburn 1368: return 'no_host';
1369: }
1.1 albertel 1370: }
1.1073 raeburn 1371: my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
1.471 albertel 1372: if ($answer eq 'authorized') {
1.952 raeburn 1373: if ($newhome) {
1374: &logthis("User $uname at $udom authorized by $uhome, but needs account");
1375: return 'no_account_on_host';
1376: } else {
1377: &logthis("User $uname at $udom authorized by $uhome");
1378: return $uhome;
1379: }
1.471 albertel 1380: }
1381: if ($answer eq 'non_authorized') {
1382: &logthis("User $uname at $udom rejected by $uhome");
1.1172.2.146. .5(raebu 1383:22): return 'no_host';
1.9 www 1384: }
1.471 albertel 1385: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.1 albertel 1386: return 'no_host';
1387: }
1388:
1.1172.2.146. .6(raebu 1389:22): sub can_switchserver {
1390:22): my ($udom,$home) = @_;
1391:22): my ($canswitch,@intdoms);
1392:22): my $internet_names = &get_internet_names($home);
1393:22): if (ref($internet_names) eq 'ARRAY') {
1394:22): @intdoms = @{$internet_names};
1395:22): }
1396:22): my $uint_dom = &internet_dom(&domain($udom,'primary'));
1397:22): if ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
1398:22): $canswitch = 1;
1399:22): } else {
1400:22): my $serverhomeID = &get_server_homeID(&hostname($home));
1401:22): my $serverhomedom = &host_domain($serverhomeID);
1402:22): my %defdomdefaults = &get_domain_defaults($serverhomedom);
1403:22): my %udomdefaults = &get_domain_defaults($udom);
1404:22): my $remoterev = &get_server_loncaparev('',$home);
1405:22): $canswitch = &can_host_session($udom,$home,$remoterev,
1406:22): $udomdefaults{'remotesessions'},
1407:22): $defdomdefaults{'hostedsessions'});
1408:22): }
1409:22): return $canswitch;
1410:22): }
1411:22):
1.1073 raeburn 1412: sub can_host_session {
1.1074 raeburn 1413: my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
1.1073 raeburn 1414: my $canhost = 1;
1.1074 raeburn 1415: my $host_idn = &Apache::lonnet::internet_dom($lonhost);
1.1073 raeburn 1416: if (ref($remotesessions) eq 'HASH') {
1417: if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
1.1074 raeburn 1418: if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
1.1073 raeburn 1419: $canhost = 0;
1420: } else {
1421: $canhost = 1;
1422: }
1423: }
1424: if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
1.1074 raeburn 1425: if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
1.1073 raeburn 1426: $canhost = 1;
1427: } else {
1428: $canhost = 0;
1429: }
1430: }
1431: if ($canhost) {
1432: if ($remotesessions->{'version'} ne '') {
1433: my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
1434: if ($reqmajor ne '' && $reqminor ne '') {
1435: if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
1436: my $major = $1;
1437: my $minor = $2;
1438: if (($major < $reqmajor ) ||
1439: (($major == $reqmajor) && ($minor < $reqminor))) {
1440: $canhost = 0;
1441: }
1442: } else {
1443: $canhost = 0;
1444: }
1445: }
1446: }
1447: }
1448: }
1449: if ($canhost) {
1450: if (ref($hostedsessions) eq 'HASH') {
1.1120 raeburn 1451: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
1452: my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
1.1073 raeburn 1453: if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
1.1120 raeburn 1454: if (($uint_dom ne '') &&
1455: (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
1.1073 raeburn 1456: $canhost = 0;
1457: } else {
1458: $canhost = 1;
1459: }
1460: }
1461: if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
1.1120 raeburn 1462: if (($uint_dom ne '') &&
1463: (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
1.1073 raeburn 1464: $canhost = 1;
1465: } else {
1466: $canhost = 0;
1467: }
1468: }
1469: }
1470: }
1471: return $canhost;
1472: }
1473:
1.1083 raeburn 1474: sub spare_can_host {
1475: my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
1476: my $canhost=1;
1.1172.2.62 raeburn 1477: my $try_server_hostname = &hostname($try_server);
1478: my $serverhomeID = &get_server_homeID($try_server_hostname);
1479: my $serverhomedom = &host_domain($serverhomeID);
1480: my %defdomdefaults = &get_domain_defaults($serverhomedom);
1481: if (ref($defdomdefaults{'offloadnow'}) eq 'HASH') {
1482: if ($defdomdefaults{'offloadnow'}{$try_server}) {
1483: $canhost = 0;
1484: }
1485: }
1.1172.2.136 raeburn 1486: if ($canhost) {
1487: if (ref($defdomdefaults{'offloadoth'}) eq 'HASH') {
1488: if ($defdomdefaults{'offloadoth'}{$try_server}) {
1489: unless (&shared_institution($udom,$try_server)) {
1490: $canhost = 0;
1491: }
1492: }
1493: }
1494: }
1.1172.2.62 raeburn 1495: if (($canhost) && ($uint_dom)) {
1496: my @intdoms;
1497: my $internet_names = &get_internet_names($try_server);
1498: if (ref($internet_names) eq 'ARRAY') {
1499: @intdoms = @{$internet_names};
1500: }
1501: unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
1502: my $remoterev = &get_server_loncaparev(undef,$try_server);
1503: $canhost = &can_host_session($udom,$try_server,$remoterev,
1504: $remotesessions,
1505: $defdomdefaults{'hostedsessions'});
1506: }
1.1083 raeburn 1507: }
1508: return $canhost;
1509: }
1510:
1.1123 raeburn 1511: sub this_host_spares {
1512: my ($dom) = @_;
1.1126 raeburn 1513: my ($dom_in_use,$lonhost_in_use,$result);
1.1123 raeburn 1514: my @hosts = ¤t_machine_ids();
1515: foreach my $lonhost (@hosts) {
1516: if (&host_domain($lonhost) eq $dom) {
1.1126 raeburn 1517: $dom_in_use = $dom;
1518: $lonhost_in_use = $lonhost;
1.1123 raeburn 1519: last;
1520: }
1521: }
1.1126 raeburn 1522: if ($dom_in_use ne '') {
1523: $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
1524: }
1525: if (ref($result) ne 'HASH') {
1526: $lonhost_in_use = $perlvar{'lonHostID'};
1527: $dom_in_use = &host_domain($lonhost_in_use);
1528: $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
1529: if (ref($result) ne 'HASH') {
1530: $result = \%spareid;
1531: }
1532: }
1533: return $result;
1534: }
1535:
1536: sub spares_for_offload {
1537: my ($dom_in_use,$lonhost_in_use) = @_;
1538: my ($result,$cached)=&is_cached_new('spares',$dom_in_use);
1.1123 raeburn 1539: if (defined($cached)) {
1540: return $result;
1541: } else {
1.1126 raeburn 1542: my $cachetime = 60*60*24;
1543: my %domconfig =
1544: &Apache::lonnet::get_dom('configuration',['usersessions'],$dom_in_use);
1545: if (ref($domconfig{'usersessions'}) eq 'HASH') {
1546: if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
1547: if (ref($domconfig{'usersessions'}{'spares'}{$lonhost_in_use}) eq 'HASH') {
1548: return &do_cache_new('spares',$dom_in_use,$domconfig{'usersessions'}{'spares'}{$lonhost_in_use},$cachetime);
1.1123 raeburn 1549: }
1550: }
1551: }
1552: }
1.1126 raeburn 1553: return;
1.1123 raeburn 1554: }
1555:
1.1129 raeburn 1556: sub get_lonbalancer_config {
1557: my ($servers) = @_;
1558: my ($currbalancer,$currtargets);
1559: if (ref($servers) eq 'HASH') {
1560: foreach my $server (keys(%{$servers})) {
1561: my %what = (
1562: spareid => 1,
1563: perlvar => 1,
1564: );
1565: my ($result,$returnhash) = &get_remote_globals($server,\%what);
1566: if ($result eq 'ok') {
1567: if (ref($returnhash) eq 'HASH') {
1568: if (ref($returnhash->{'perlvar'}) eq 'HASH') {
1569: if ($returnhash->{'perlvar'}->{'lonBalancer'} eq 'yes') {
1570: $currbalancer = $server;
1571: $currtargets = {};
1572: if (ref($returnhash->{'spareid'}) eq 'HASH') {
1573: if (ref($returnhash->{'spareid'}->{'primary'}) eq 'ARRAY') {
1574: $currtargets->{'primary'} = $returnhash->{'spareid'}->{'primary'};
1575: }
1576: if (ref($returnhash->{'spareid'}->{'default'}) eq 'ARRAY') {
1577: $currtargets->{'default'} = $returnhash->{'spareid'}->{'default'};
1578: }
1579: }
1580: last;
1581: }
1582: }
1583: }
1584: }
1585: }
1586: }
1587: return ($currbalancer,$currtargets);
1588: }
1589:
1590: sub check_loadbalancing {
1.1172.2.88 raeburn 1591: my ($uname,$udom,$caller) = @_;
1.1172.2.12 raeburn 1592: my ($is_balancer,$currtargets,$currrules,$dom_in_use,$homeintdom,
1.1172.2.107 raeburn 1593: $rule_in_effect,$offloadto,$otherserver,$setcookie);
1.1129 raeburn 1594: my $lonhost = $perlvar{'lonHostID'};
1.1172.2.4 raeburn 1595: my @hosts = ¤t_machine_ids();
1.1129 raeburn 1596: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
1597: my $uintdom = &Apache::lonnet::internet_dom($uprimary_id);
1598: my $intdom = &Apache::lonnet::internet_dom($lonhost);
1599: my $serverhomedom = &host_domain($lonhost);
1.1172.2.75 raeburn 1600: my $domneedscache;
1.1129 raeburn 1601: my $cachetime = 60*60*24;
1602:
1603: if (($uintdom ne '') && ($uintdom eq $intdom)) {
1604: $dom_in_use = $udom;
1605: $homeintdom = 1;
1606: } else {
1607: $dom_in_use = $serverhomedom;
1608: }
1609: my ($result,$cached)=&is_cached_new('loadbalancing',$dom_in_use);
1610: unless (defined($cached)) {
1611: my %domconfig =
1612: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
1613: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.1130 raeburn 1614: $result = &do_cache_new('loadbalancing',$dom_in_use,$domconfig{'loadbalancing'},$cachetime);
1.1172.2.75 raeburn 1615: } else {
1616: $domneedscache = $dom_in_use;
1.1129 raeburn 1617: }
1618: }
1619: if (ref($result) eq 'HASH') {
1.1172.2.107 raeburn 1620: ($is_balancer,$currtargets,$currrules,$setcookie) =
1.1172.2.12 raeburn 1621: &check_balancer_result($result,@hosts);
1.1129 raeburn 1622: if ($is_balancer) {
1623: if (ref($currrules) eq 'HASH') {
1624: if ($homeintdom) {
1625: if ($uname ne '') {
1626: if (($currrules->{'_LC_adv'} ne '') || ($currrules->{'_LC_author'} ne '')) {
1627: my ($is_adv,$is_author) = &is_advanced_user($udom,$uname);
1628: if (($currrules->{'_LC_author'} ne '') && ($is_author)) {
1629: $rule_in_effect = $currrules->{'_LC_author'};
1630: } elsif (($currrules->{'_LC_adv'} ne '') && ($is_adv)) {
1631: $rule_in_effect = $currrules->{'_LC_adv'}
1632: }
1633: }
1634: if ($rule_in_effect eq '') {
1635: my %userenv = &userenvironment($udom,$uname,'inststatus');
1636: if ($userenv{'inststatus'} ne '') {
1637: my @statuses = map { &unescape($_); } split(/:/,$userenv{'inststatus'});
1638: my ($othertitle,$usertypes,$types) =
1639: &Apache::loncommon::sorted_inst_types($udom);
1640: if (ref($types) eq 'ARRAY') {
1641: foreach my $type (@{$types}) {
1642: if (grep(/^\Q$type\E$/,@statuses)) {
1643: if (exists($currrules->{$type})) {
1644: $rule_in_effect = $currrules->{$type};
1645: }
1646: }
1647: }
1648: }
1649: } else {
1650: if (exists($currrules->{'default'})) {
1651: $rule_in_effect = $currrules->{'default'};
1652: }
1653: }
1654: }
1655: } else {
1656: if (exists($currrules->{'default'})) {
1657: $rule_in_effect = $currrules->{'default'};
1658: }
1659: }
1660: } else {
1661: if ($currrules->{'_LC_external'} ne '') {
1662: $rule_in_effect = $currrules->{'_LC_external'};
1663: }
1664: }
1665: $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
1666: $uname,$udom);
1667: }
1668: }
1669: } elsif (($homeintdom) && ($udom ne $serverhomedom)) {
1.1172.2.35 raeburn 1670: ($result,$cached)=&is_cached_new('loadbalancing',$serverhomedom);
1.1129 raeburn 1671: unless (defined($cached)) {
1672: my %domconfig =
1673: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$serverhomedom);
1674: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.1172.2.75 raeburn 1675: $result = &do_cache_new('loadbalancing',$serverhomedom,$domconfig{'loadbalancing'},$cachetime);
1676: } else {
1677: $domneedscache = $serverhomedom;
1.1129 raeburn 1678: }
1679: }
1680: if (ref($result) eq 'HASH') {
1.1172.2.107 raeburn 1681: ($is_balancer,$currtargets,$currrules,$setcookie) =
1.1172.2.12 raeburn 1682: &check_balancer_result($result,@hosts);
1683: if ($is_balancer) {
1.1129 raeburn 1684: if (ref($currrules) eq 'HASH') {
1685: if ($currrules->{'_LC_internetdom'} ne '') {
1686: $rule_in_effect = $currrules->{'_LC_internetdom'};
1687: }
1688: }
1689: $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
1690: $uname,$udom);
1691: }
1692: } else {
1693: if ($perlvar{'lonBalancer'} eq 'yes') {
1694: $is_balancer = 1;
1695: $offloadto = &this_host_spares($dom_in_use);
1696: }
1.1172.2.75 raeburn 1697: unless (defined($cached)) {
1698: $domneedscache = $serverhomedom;
1699: }
1.1129 raeburn 1700: }
1701: } else {
1702: if ($perlvar{'lonBalancer'} eq 'yes') {
1703: $is_balancer = 1;
1704: $offloadto = &this_host_spares($dom_in_use);
1705: }
1.1172.2.75 raeburn 1706: unless (defined($cached)) {
1707: $domneedscache = $serverhomedom;
1708: }
1709: }
1710: if ($domneedscache) {
1711: &do_cache_new('loadbalancing',$domneedscache,$is_balancer,$cachetime);
1.1129 raeburn 1712: }
1.1172.2.130 raeburn 1713: if (($is_balancer) && ($caller ne 'switchserver')) {
1.1172.2.5 raeburn 1714: my $lowest_load = 30000;
1715: if (ref($offloadto) eq 'HASH') {
1716: if (ref($offloadto->{'primary'}) eq 'ARRAY') {
1717: foreach my $try_server (@{$offloadto->{'primary'}}) {
1718: ($otherserver,$lowest_load) =
1719: &compare_server_load($try_server,$otherserver,$lowest_load);
1720: }
1.1129 raeburn 1721: }
1.1172.2.5 raeburn 1722: my $found_server = ($otherserver ne '' && $lowest_load < 100);
1.1129 raeburn 1723:
1.1172.2.5 raeburn 1724: if (!$found_server) {
1725: if (ref($offloadto->{'default'}) eq 'ARRAY') {
1726: foreach my $try_server (@{$offloadto->{'default'}}) {
1727: ($otherserver,$lowest_load) =
1728: &compare_server_load($try_server,$otherserver,$lowest_load);
1729: }
1730: }
1731: }
1732: } elsif (ref($offloadto) eq 'ARRAY') {
1733: if (@{$offloadto} == 1) {
1734: $otherserver = $offloadto->[0];
1735: } elsif (@{$offloadto} > 1) {
1736: foreach my $try_server (@{$offloadto}) {
1.1129 raeburn 1737: ($otherserver,$lowest_load) =
1738: &compare_server_load($try_server,$otherserver,$lowest_load);
1739: }
1740: }
1741: }
1.1172.2.88 raeburn 1742: unless ($caller eq 'login') {
1743: if (($otherserver ne '') && (grep(/^\Q$otherserver\E$/,@hosts))) {
1744: $is_balancer = 0;
1745: if ($uname ne '' && $udom ne '') {
1746: if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
1747: &appenv({'user.loadbalexempt' => $lonhost,
1748: 'user.loadbalcheck.time' => time});
1749: }
1.1172.2.5 raeburn 1750: }
1.1129 raeburn 1751: }
1752: }
1.1172.2.130 raeburn 1753: }
1754: if (($is_balancer) && (!$homeintdom)) {
1755: undef($setcookie);
1.1129 raeburn 1756: }
1.1172.2.107 raeburn 1757: return ($is_balancer,$otherserver,$setcookie);
1.1129 raeburn 1758: }
1759:
1.1172.2.12 raeburn 1760: sub check_balancer_result {
1761: my ($result,@hosts) = @_;
1.1172.2.107 raeburn 1762: my ($is_balancer,$currtargets,$currrules,$setcookie);
1.1172.2.12 raeburn 1763: if (ref($result) eq 'HASH') {
1764: if ($result->{'lonhost'} ne '') {
1765: my $currbalancer = $result->{'lonhost'};
1766: if (grep(/^\Q$currbalancer\E$/,@hosts)) {
1767: $is_balancer = 1;
1768: $currtargets = $result->{'targets'};
1769: $currrules = $result->{'rules'};
1770: }
1771: } else {
1772: foreach my $key (keys(%{$result})) {
1773: if (($key ne '') && (grep(/^\Q$key\E$/,@hosts)) &&
1774: (ref($result->{$key}) eq 'HASH')) {
1775: $is_balancer = 1;
1776: $currrules = $result->{$key}{'rules'};
1777: $currtargets = $result->{$key}{'targets'};
1.1172.2.107 raeburn 1778: $setcookie = $result->{$key}{'cookie'};
1.1172.2.12 raeburn 1779: last;
1780: }
1781: }
1782: }
1783: }
1.1172.2.107 raeburn 1784: return ($is_balancer,$currtargets,$currrules,$setcookie);
1.1172.2.12 raeburn 1785: }
1786:
1.1129 raeburn 1787: sub get_loadbalancer_targets {
1788: my ($rule_in_effect,$currtargets,$uname,$udom) = @_;
1789: my $offloadto;
1.1172.2.4 raeburn 1790: if ($rule_in_effect eq 'none') {
1791: return [$perlvar{'lonHostID'}];
1792: } elsif ($rule_in_effect eq '') {
1.1129 raeburn 1793: $offloadto = $currtargets;
1794: } else {
1795: if ($rule_in_effect eq 'homeserver') {
1796: my $homeserver = &homeserver($uname,$udom);
1797: if ($homeserver ne 'no_host') {
1798: $offloadto = [$homeserver];
1799: }
1800: } elsif ($rule_in_effect eq 'externalbalancer') {
1801: my %domconfig =
1802: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
1803: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1804: if ($domconfig{'loadbalancing'}{'lonhost'} ne '') {
1805: if (&hostname($domconfig{'loadbalancing'}{'lonhost'}) ne '') {
1806: $offloadto = [$domconfig{'loadbalancing'}{'lonhost'}];
1807: }
1808: }
1809: } else {
1.1172.2.7 raeburn 1810: my %servers = &internet_dom_servers($udom);
1.1129 raeburn 1811: my ($remotebalancer,$remotetargets) = &get_lonbalancer_config(\%servers);
1812: if (&hostname($remotebalancer) ne '') {
1813: $offloadto = [$remotebalancer];
1814: }
1815: }
1816: } elsif (&hostname($rule_in_effect) ne '') {
1817: $offloadto = [$rule_in_effect];
1818: }
1819: }
1820: return $offloadto;
1821: }
1822:
1.1127 raeburn 1823: sub internet_dom_servers {
1824: my ($dom) = @_;
1825: my (%uniqservers,%servers);
1826: my $primaryserver = &hostname(&domain($dom,'primary'));
1827: my @machinedoms = &machine_domains($primaryserver);
1828: foreach my $mdom (@machinedoms) {
1829: my %currservers = %servers;
1830: my %server = &get_servers($mdom);
1831: %servers = (%currservers,%server);
1832: }
1833: my %by_hostname;
1834: foreach my $id (keys(%servers)) {
1835: push(@{$by_hostname{$servers{$id}}},$id);
1836: }
1837: foreach my $hostname (sort(keys(%by_hostname))) {
1838: if (@{$by_hostname{$hostname}} > 1) {
1839: my $match = 0;
1840: foreach my $id (@{$by_hostname{$hostname}}) {
1841: if (&host_domain($id) eq $dom) {
1842: $uniqservers{$id} = $hostname;
1843: $match = 1;
1844: }
1845: }
1846: unless ($match) {
1847: $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
1848: }
1849: } else {
1850: $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
1851: }
1852: }
1853: return %uniqservers;
1854: }
1855:
1.1 albertel 1856: # ---------------------- Find the homebase for a user from domain's lib servers
1.11 www 1857:
1.599 albertel 1858: my %homecache;
1.1 albertel 1859: sub homeserver {
1.230 stredwic 1860: my ($uname,$udom,$ignoreBadCache)=@_;
1.1 albertel 1861: my $index="$uname:$udom";
1.426 albertel 1862:
1.599 albertel 1863: if (exists($homecache{$index})) { return $homecache{$index}; }
1.841 albertel 1864:
1865: my %servers = &get_servers($udom,'library');
1866: foreach my $tryserver (keys(%servers)) {
1.230 stredwic 1867: next if ($ignoreBadCache ne 'true' &&
1.231 stredwic 1868: exists($badServerCache{$tryserver}));
1.841 albertel 1869:
1870: my $answer=reply("home:$udom:$uname",$tryserver);
1871: if ($answer eq 'found') {
1872: delete($badServerCache{$tryserver});
1873: return $homecache{$index}=$tryserver;
1874: } elsif ($answer eq 'no_host') {
1875: $badServerCache{$tryserver}=1;
1876: }
1.1 albertel 1877: }
1878: return 'no_host';
1.70 www 1879: }
1880:
1881: # ------------------------------------- Find the usernames behind a list of IDs
1882:
1883: sub idget {
1884: my ($udom,@ids)=@_;
1885: my %returnhash=();
1886:
1.841 albertel 1887: my %servers = &get_servers($udom,'library');
1888: foreach my $tryserver (keys(%servers)) {
1.1172.2.73 raeburn 1889: my $idlist=join('&', map { &escape($_); } @ids);
1.841 albertel 1890: $idlist=~tr/A-Z/a-z/;
1891: my $reply=&reply("idget:$udom:".$idlist,$tryserver);
1892: my @answer=();
1893: if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
1894: @answer=split(/\&/,$reply);
1895: } ;
1896: my $i;
1897: for ($i=0;$i<=$#ids;$i++) {
1898: if ($answer[$i]) {
1.1172.2.73 raeburn 1899: $returnhash{$ids[$i]}=&unescape($answer[$i]);
1.841 albertel 1900: }
1901: }
1902: }
1.70 www 1903: return %returnhash;
1904: }
1905:
1906: # ------------------------------------- Find the IDs behind a list of usernames
1907:
1908: sub idrget {
1909: my ($udom,@unames)=@_;
1910: my %returnhash=();
1.800 albertel 1911: foreach my $uname (@unames) {
1912: $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
1.191 harris41 1913: }
1.70 www 1914: return %returnhash;
1915: }
1916:
1917: # ------------------------------- Store away a list of names and associated IDs
1918:
1919: sub idput {
1920: my ($udom,%ids)=@_;
1921: my %servers=();
1.800 albertel 1922: foreach my $uname (keys(%ids)) {
1923: &cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
1924: my $uhom=&homeserver($uname,$udom);
1.70 www 1925: if ($uhom ne 'no_host') {
1.800 albertel 1926: my $id=&escape($ids{$uname});
1.70 www 1927: $id=~tr/A-Z/a-z/;
1.800 albertel 1928: my $esc_unam=&escape($uname);
1.70 www 1929: if ($servers{$uhom}) {
1.800 albertel 1930: $servers{$uhom}.='&'.$id.'='.$esc_unam;
1.70 www 1931: } else {
1.800 albertel 1932: $servers{$uhom}=$id.'='.$esc_unam;
1.70 www 1933: }
1934: }
1.191 harris41 1935: }
1.800 albertel 1936: foreach my $server (keys(%servers)) {
1937: &critical('idput:'.$udom.':'.$servers{$server},$server);
1.191 harris41 1938: }
1.344 www 1939: }
1940:
1.1172.2.30 raeburn 1941: # ---------------------------------------- Delete unwanted IDs from ids.db file
1942:
1943: sub iddel {
1944: my ($udom,$idshashref,$uhome)=@_;
1945: my %result=();
1946: unless (ref($idshashref) eq 'HASH') {
1947: return %result;
1948: }
1949: my %servers=();
1950: while (my ($id,$uname) = each(%{$idshashref})) {
1951: my $uhom;
1952: if ($uhome) {
1953: $uhom = $uhome;
1954: } else {
1955: $uhom=&homeserver($uname,$udom);
1956: }
1957: if ($uhom ne 'no_host') {
1958: if ($servers{$uhom}) {
1959: $servers{$uhom}.='&'.&escape($id);
1960: } else {
1961: $servers{$uhom}=&escape($id);
1962: }
1963: }
1964: }
1965: foreach my $server (keys(%servers)) {
1966: $result{$server} = &critical('iddel:'.$udom.':'.$servers{$server},$uhome);
1967: }
1968: return %result;
1969: }
1970:
1.1023 raeburn 1971: # ------------------------------dump from db file owned by domainconfig user
1.1012 raeburn 1972: sub dump_dom {
1.1165 droeschl 1973: my ($namespace, $udom, $regexp) = @_;
1974:
1975: $udom ||= $env{'user.domain'};
1976:
1977: return () unless $udom;
1978:
1979: return &dump($namespace, $udom, &get_domainconfiguser($udom), $regexp);
1.1012 raeburn 1980: }
1981:
1.1023 raeburn 1982: # ------------------------------------------ get items from domain db files
1.806 raeburn 1983:
1984: sub get_dom {
1.1172.2.146. .1(raebu 1985:22): my ($namespace,$storearr,$udom,$uhome,$encrypt)=@_;
1.1172.2.57 raeburn 1986: return if ($udom eq 'public');
1.806 raeburn 1987: my $items='';
1988: foreach my $item (@$storearr) {
1989: $items.=&escape($item).'&';
1990: }
1991: $items=~s/\&$//;
1.860 raeburn 1992: if (!$udom) {
1993: $udom=$env{'user.domain'};
1.1172.2.57 raeburn 1994: return if ($udom eq 'public');
1.860 raeburn 1995: if (defined(&domain($udom,'primary'))) {
1996: $uhome=&domain($udom,'primary');
1997: } else {
1.874 albertel 1998: undef($uhome);
1.860 raeburn 1999: }
2000: } else {
2001: if (!$uhome) {
2002: if (defined(&domain($udom,'primary'))) {
2003: $uhome=&domain($udom,'primary');
2004: }
2005: }
2006: }
2007: if ($udom && $uhome && ($uhome ne 'no_host')) {
1.1172.2.139 raeburn 2008: my $rep;
2009: if (grep { $_ eq $uhome } ¤t_machine_ids()) {
2010: # domain information is hosted on this machine
2011: $rep = &LONCAPA::Lond::get_dom("getdom:$udom:$namespace:$items");
1.1172.2.146. .1(raebu 2012:22): } else {
2013:22): if ($encrypt) {
2014:22): $rep=&reply("encrypt:egetdom:$udom:$namespace:$items",$uhome);
2015:22): } else {
2016:22): $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
2017:22): }
1.1172.2.139 raeburn 2018: }
1.866 raeburn 2019: my %returnhash;
1.875 albertel 2020: if ($rep eq '' || $rep =~ /^error: 2 /) {
1.866 raeburn 2021: return %returnhash;
2022: }
1.806 raeburn 2023: my @pairs=split(/\&/,$rep);
2024: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
2025: return @pairs;
2026: }
2027: my $i=0;
2028: foreach my $item (@$storearr) {
2029: $returnhash{$item}=&thaw_unescape($pairs[$i]);
2030: $i++;
2031: }
2032: return %returnhash;
2033: } else {
1.880 banghart 2034: &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
1.806 raeburn 2035: }
2036: }
2037:
2038: # -------------------------------------------- put items in domain db files
2039:
2040: sub put_dom {
1.1172.2.146. .1(raebu 2041:22): my ($namespace,$storehash,$udom,$uhome,$encrypt)=@_;
1.860 raeburn 2042: if (!$udom) {
2043: $udom=$env{'user.domain'};
2044: if (defined(&domain($udom,'primary'))) {
2045: $uhome=&domain($udom,'primary');
2046: } else {
1.874 albertel 2047: undef($uhome);
1.860 raeburn 2048: }
2049: } else {
2050: if (!$uhome) {
2051: if (defined(&domain($udom,'primary'))) {
2052: $uhome=&domain($udom,'primary');
2053: }
2054: }
2055: }
2056: if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806 raeburn 2057: my $items='';
2058: foreach my $item (keys(%$storehash)) {
2059: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
2060: }
2061: $items=~s/\&$//;
1.1172.2.146. .1(raebu 2062:22): if ($encrypt) {
2063:22): return &reply("encrypt:putdom:$udom:$namespace:$items",$uhome);
2064:22): } else {
2065:22): return &reply("putdom:$udom:$namespace:$items",$uhome);
2066:22): }
1.806 raeburn 2067: } else {
1.860 raeburn 2068: &logthis("put_dom failed - no homeserver and/or domain");
1.806 raeburn 2069: }
2070: }
2071:
1.1023 raeburn 2072: # --------------------- newput for items in db file owned by domainconfig user
1.1012 raeburn 2073: sub newput_dom {
1.1023 raeburn 2074: my ($namespace,$storehash,$udom) = @_;
1.1012 raeburn 2075: my $result;
2076: if (!$udom) {
2077: $udom=$env{'user.domain'};
2078: }
1.1023 raeburn 2079: if ($udom) {
2080: my $uname = &get_domainconfiguser($udom);
2081: $result = &newput($namespace,$storehash,$udom,$uname);
1.1012 raeburn 2082: }
2083: return $result;
2084: }
2085:
1.1023 raeburn 2086: # --------------------- delete for items in db file owned by domainconfig user
1.1012 raeburn 2087: sub del_dom {
1.1023 raeburn 2088: my ($namespace,$storearr,$udom)=@_;
1.1012 raeburn 2089: if (ref($storearr) eq 'ARRAY') {
2090: if (!$udom) {
2091: $udom=$env{'user.domain'};
2092: }
1.1023 raeburn 2093: if ($udom) {
2094: my $uname = &get_domainconfiguser($udom);
2095: return &del($namespace,$storearr,$udom,$uname);
1.1012 raeburn 2096: }
2097: }
2098: }
2099:
1.1172.2.146. .4(raebu 2100:22): sub store_dom {
2101:22): my ($storehash,$id,$namespace,$dom,$home,$encrypt) = @_;
2102:22): $$storehash{'ip'}=&get_requestor_ip();
2103:22): $$storehash{'host'}=$perlvar{'lonHostID'};
2104:22): my $namevalue='';
2105:22): foreach my $key (keys(%{$storehash})) {
2106:22): $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
2107:22): }
2108:22): $namevalue=~s/\&$//;
2109:22): if (grep { $_ eq $home } current_machine_ids()) {
2110:22): return LONCAPA::Lond::store_dom("storedom:$dom:$namespace:$id:$namevalue");
2111:22): } else {
2112:22): if ($namespace eq 'private') {
2113:22): return 'refused';
2114:22): } elsif ($encrypt) {
2115:22): return reply("encrypt:storedom:$dom:$namespace:$id:$namevalue",$home);
2116:22): } else {
2117:22): return reply("storedom:$dom:$namespace:$id:$namevalue",$home);
2118:22): }
2119:22): }
2120:22): }
2121:22):
2122:22): sub restore_dom {
2123:22): my ($id,$namespace,$dom,$home,$encrypt) = @_;
2124:22): my $answer;
2125:22): if (grep { $_ eq $home } current_machine_ids()) {
2126:22): $answer = LONCAPA::Lond::restore_dom("restoredom:$dom:$namespace:$id");
2127:22): } elsif ($namespace ne 'private') {
2128:22): if ($encrypt) {
2129:22): $answer=&reply("encrypt:restoredom:$dom:$namespace:$id",$home);
2130:22): } else {
2131:22): $answer=&reply("restoredom:$dom:$namespace:$id",$home);
2132:22): }
2133:22): }
2134:22): my %returnhash=();
2135:22): unless (($answer eq '') || ($answer eq 'con_lost') || ($answer eq 'refused') ||
2136:22): ($answer eq 'unknown_cmd') || ($answer eq 'rejected')) {
2137:22): foreach my $line (split(/\&/,$answer)) {
2138:22): my ($name,$value)=split(/\=/,$line);
2139:22): $returnhash{&unescape($name)}=&thaw_unescape($value);
2140:22): }
2141:22): my $version;
2142:22): for ($version=1;$version<=$returnhash{'version'};$version++) {
2143:22): foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
2144:22): $returnhash{$item}=$returnhash{$version.':'.$item};
2145:22): }
2146:22): }
2147:22): }
2148:22): return %returnhash;
2149:22): }
2150:22):
1.1023 raeburn 2151: # ----------------------------------construct domainconfig user for a domain
2152: sub get_domainconfiguser {
2153: my ($udom) = @_;
2154: return $udom.'-domainconfig';
2155: }
2156:
1.837 raeburn 2157: sub retrieve_inst_usertypes {
2158: my ($udom) = @_;
2159: my (%returnhash,@order);
1.989 raeburn 2160: my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
2161: if ((ref($domdefs{'inststatustypes'}) eq 'HASH') &&
2162: (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
1.1172.2.44 raeburn 2163: return ($domdefs{'inststatustypes'},$domdefs{'inststatusorder'});
1.989 raeburn 2164: } else {
2165: if (defined(&domain($udom,'primary'))) {
2166: my $uhome=&domain($udom,'primary');
2167: my $rep=&reply("inst_usertypes:$udom",$uhome);
2168: if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
1.1172.2.44 raeburn 2169: &logthis("retrieve_inst_usertypes failed - $rep returned from $uhome in domain: $udom");
1.989 raeburn 2170: return (\%returnhash,\@order);
2171: }
2172: my ($hashitems,$orderitems) = split(/:/,$rep);
2173: my @pairs=split(/\&/,$hashitems);
2174: foreach my $item (@pairs) {
2175: my ($key,$value)=split(/=/,$item,2);
2176: $key = &unescape($key);
2177: next if ($key =~ /^error: 2 /);
2178: $returnhash{$key}=&thaw_unescape($value);
2179: }
2180: my @esc_order = split(/\&/,$orderitems);
2181: foreach my $item (@esc_order) {
2182: push(@order,&unescape($item));
2183: }
2184: } else {
1.1172.2.44 raeburn 2185: &logthis("retrieve_inst_usertypes failed - no primary domain server for $udom");
1.837 raeburn 2186: }
1.1172.2.44 raeburn 2187: return (\%returnhash,\@order);
1.837 raeburn 2188: }
2189: }
2190:
1.868 raeburn 2191: sub is_domainimage {
2192: my ($url) = @_;
1.1172.2.142 raeburn 2193: if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo|login)/+[^/]-) {
1.868 raeburn 2194: if (&domain($1) ne '') {
2195: return '1';
2196: }
2197: }
2198: return;
2199: }
2200:
1.899 raeburn 2201: sub inst_directory_query {
2202: my ($srch) = @_;
2203: my $udom = $srch->{'srchdomain'};
2204: my %results;
2205: my $homeserver = &domain($udom,'primary');
1.909 raeburn 2206: my $outcome;
1.899 raeburn 2207: if ($homeserver ne '') {
1.1172.2.116 raeburn 2208: unless ($homeserver eq $perlvar{'lonHostID'}) {
2209: if ($srch->{'srchby'} eq 'email') {
1.1172.2.117 raeburn 2210: my $lcrev = &get_server_loncaparev($udom,$homeserver);
1.1172.2.116 raeburn 2211: my ($major,$minor,$subver) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.(\d+)[\w.\-]+\'?$/);
2212: if (($major eq '' && $minor eq '') || ($major < 2) ||
2213: (($major == 2) && ($minor < 11)) ||
2214: (($major == 2) && ($minor == 11) && ($subver < 3))) {
2215: return;
2216: }
2217: }
2218: }
1.904 albertel 2219: my $queryid=&reply("querysend:instdirsearch:".
2220: &escape($srch->{'srchby'}).':'.
2221: &escape($srch->{'srchterm'}).':'.
2222: &escape($srch->{'srchtype'}),$homeserver);
2223: my $host=&hostname($homeserver);
2224: if ($queryid !~/^\Q$host\E\_/) {
1.1172.2.96 raeburn 2225: &logthis('institutional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.' in domain '.$udom);
1.904 albertel 2226: return;
2227: }
2228: my $response = &get_query_reply($queryid);
2229: my $maxtries = 5;
2230: my $tries = 1;
2231: while (($response=~/^timeout/) && ($tries < $maxtries)) {
2232: $response = &get_query_reply($queryid);
2233: $tries ++;
2234: }
2235:
2236: if (!&error($response) && $response ne 'refused') {
1.909 raeburn 2237: if ($response eq 'unavailable') {
2238: $outcome = $response;
2239: } else {
2240: $outcome = 'ok';
2241: my @matches = split(/\n/,$response);
2242: foreach my $match (@matches) {
2243: my ($key,$value) = split(/=/,$match);
2244: $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
2245: }
1.899 raeburn 2246: }
2247: }
2248: }
1.909 raeburn 2249: return ($outcome,%results);
1.899 raeburn 2250: }
2251:
2252: sub usersearch {
2253: my ($srch) = @_;
2254: my $dom = $srch->{'srchdomain'};
2255: my %results;
2256: my %libserv = &all_library();
2257: my $query = 'usersearch';
2258: foreach my $tryserver (keys(%libserv)) {
2259: if (&host_domain($tryserver) eq $dom) {
1.1172.2.116 raeburn 2260: unless ($tryserver eq $perlvar{'lonHostID'}) {
2261: if ($srch->{'srchby'} eq 'email') {
1.1172.2.117 raeburn 2262: my $lcrev = &get_server_loncaparev($dom,$tryserver);
1.1172.2.119 raeburn 2263: my ($major,$minor,$subver) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.(\d+)[\w.\-]+\'?$/);
1.1172.2.116 raeburn 2264: next if (($major eq '' && $minor eq '') || ($major < 2) ||
2265: (($major == 2) && ($minor < 11)) ||
2266: (($major == 2) && ($minor == 11) && ($subver < 3)));
2267: }
2268: }
1.899 raeburn 2269: my $host=&hostname($tryserver);
2270: my $queryid=
1.911 raeburn 2271: &reply("querysend:".&escape($query).':'.
2272: &escape($srch->{'srchby'}).':'.
1.899 raeburn 2273: &escape($srch->{'srchtype'}).':'.
2274: &escape($srch->{'srchterm'}),$tryserver);
2275: if ($queryid !~/^\Q$host\E\_/) {
2276: &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
1.902 raeburn 2277: next;
1.899 raeburn 2278: }
2279: my $reply = &get_query_reply($queryid);
2280: my $maxtries = 1;
2281: my $tries = 1;
2282: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
2283: $reply = &get_query_reply($queryid);
2284: $tries ++;
2285: }
2286: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
2287: &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') - maxtries: '.$maxtries.' tries: '.$tries);
2288: } else {
1.911 raeburn 2289: my @matches;
2290: if ($reply =~ /\n/) {
2291: @matches = split(/\n/,$reply);
2292: } else {
2293: @matches = split(/\&/,$reply);
2294: }
1.899 raeburn 2295: foreach my $match (@matches) {
2296: my ($uname,$udom,%userhash);
1.911 raeburn 2297: foreach my $entry (split(/:/,$match)) {
2298: my ($key,$value) =
2299: map {&unescape($_);} split(/=/,$entry);
1.899 raeburn 2300: $userhash{$key} = $value;
2301: if ($key eq 'username') {
2302: $uname = $value;
2303: } elsif ($key eq 'domain') {
2304: $udom = $value;
1.911 raeburn 2305: }
1.899 raeburn 2306: }
2307: $results{$uname.':'.$udom} = \%userhash;
2308: }
2309: }
2310: }
2311: }
2312: return %results;
2313: }
2314:
1.912 raeburn 2315: sub get_instuser {
2316: my ($udom,$uname,$id) = @_;
2317: my $homeserver = &domain($udom,'primary');
2318: my ($outcome,%results);
2319: if ($homeserver ne '') {
2320: my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
2321: &escape($id).':'.&escape($udom),$homeserver);
2322: my $host=&hostname($homeserver);
2323: if ($queryid !~/^\Q$host\E\_/) {
2324: &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
2325: return;
2326: }
2327: my $response = &get_query_reply($queryid);
2328: my $maxtries = 5;
2329: my $tries = 1;
2330: while (($response=~/^timeout/) && ($tries < $maxtries)) {
2331: $response = &get_query_reply($queryid);
2332: $tries ++;
2333: }
2334: if (!&error($response) && $response ne 'refused') {
2335: if ($response eq 'unavailable') {
2336: $outcome = $response;
2337: } else {
2338: $outcome = 'ok';
2339: my @matches = split(/\n/,$response);
2340: foreach my $match (@matches) {
2341: my ($key,$value) = split(/=/,$match);
2342: $results{&unescape($key)} = &thaw_unescape($value);
2343: }
2344: }
2345: }
2346: }
2347: my %userinfo;
2348: if (ref($results{$uname}) eq 'HASH') {
2349: %userinfo = %{$results{$uname}};
2350: }
2351: return ($outcome,%userinfo);
2352: }
2353:
1.1172.2.69 raeburn 2354: sub get_multiple_instusers {
2355: my ($udom,$users,$caller) = @_;
2356: my ($outcome,$results);
2357: if (ref($users) eq 'HASH') {
2358: my $count = keys(%{$users});
2359: my $requested = &freeze_escape($users);
2360: my $homeserver = &domain($udom,'primary');
2361: if ($homeserver ne '') {
2362: my $queryid=&reply('querysend:getmultinstusers:::'.$caller.'='.$requested,$homeserver);
2363: my $host=&hostname($homeserver);
2364: if ($queryid !~/^\Q$host\E\_/) {
2365: &logthis('get_multiple_instusers invalid queryid: '.$queryid.
2366: ' for host: '.$homeserver.'in domain '.$udom);
2367: return ($outcome,$results);
2368: }
2369: my $response = &get_query_reply($queryid);
2370: my $maxtries = 5;
2371: if ($count > 100) {
2372: $maxtries = 1+int($count/20);
2373: }
2374: my $tries = 1;
2375: while (($response=~/^timeout/) && ($tries <= $maxtries)) {
2376: $response = &get_query_reply($queryid);
2377: $tries ++;
2378: }
2379: if ($response eq '') {
2380: $results = {};
2381: foreach my $key (keys(%{$users})) {
2382: my ($uname,$id);
2383: if ($caller eq 'id') {
2384: $id = $key;
2385: } else {
2386: $uname = $key;
2387: }
2388: my ($resp,%info) = &get_instuser($udom,$uname,$id);
2389: $outcome = $resp;
2390: if ($resp eq 'ok') {
2391: %{$results} = (%{$results}, %info);
2392: } else {
2393: last;
2394: }
2395: }
2396: } elsif(!&error($response) && ($response ne 'refused')) {
2397: if (($response eq 'unavailable') || ($response eq 'invalid') || ($response eq 'timeout')) {
2398: $outcome = $response;
2399: } else {
2400: ($outcome,my $userdata) = split(/=/,$response,2);
2401: if ($outcome eq 'ok') {
2402: $results = &thaw_unescape($userdata);
2403: }
2404: }
2405: }
2406: }
2407: }
2408: return ($outcome,$results);
2409: }
2410:
1.912 raeburn 2411: sub inst_rulecheck {
1.923 raeburn 2412: my ($udom,$uname,$id,$item,$rules) = @_;
1.912 raeburn 2413: my %returnhash;
2414: if ($udom ne '') {
2415: if (ref($rules) eq 'ARRAY') {
2416: @{$rules} = map {&escape($_);} (@{$rules});
2417: my $rulestr = join(':',@{$rules});
2418: my $homeserver=&domain($udom,'primary');
2419: if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923 raeburn 2420: my $response;
2421: if ($item eq 'username') {
2422: $response=&unescape(&reply('instrulecheck:'.&escape($udom).
2423: ':'.&escape($uname).':'.$rulestr,
1.912 raeburn 2424: $homeserver));
1.923 raeburn 2425: } elsif ($item eq 'id') {
2426: $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
2427: ':'.&escape($id).':'.$rulestr,
2428: $homeserver));
1.1172.2.146. .5(raebu 2429:22): } elsif ($item eq 'unamemap') {
2430:22): $response=&unescape(&reply('instunamemapcheck:'.
2431:22): &escape($udom).':'.&escape($uname).
2432:22): ':'.$rulestr,$homeserver));
1.945 raeburn 2433: } elsif ($item eq 'selfcreate') {
2434: $response=&unescape(&reply('instselfcreatecheck:'.
1.943 raeburn 2435: &escape($udom).':'.&escape($uname).
2436: ':'.$rulestr,$homeserver));
1.923 raeburn 2437: }
1.912 raeburn 2438: if ($response ne 'refused') {
2439: my @pairs=split(/\&/,$response);
2440: foreach my $item (@pairs) {
2441: my ($key,$value)=split(/=/,$item,2);
2442: $key = &unescape($key);
2443: next if ($key =~ /^error: 2 /);
2444: $returnhash{$key}=&thaw_unescape($value);
2445: }
2446: }
2447: }
2448: }
2449: }
2450: return %returnhash;
2451: }
2452:
2453: sub inst_userrules {
1.923 raeburn 2454: my ($udom,$check) = @_;
1.912 raeburn 2455: my (%ruleshash,@ruleorder);
2456: if ($udom ne '') {
2457: my $homeserver=&domain($udom,'primary');
2458: if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923 raeburn 2459: my $response;
2460: if ($check eq 'id') {
2461: $response=&reply('instidrules:'.&escape($udom),
1.912 raeburn 2462: $homeserver);
1.943 raeburn 2463: } elsif ($check eq 'email') {
2464: $response=&reply('instemailrules:'.&escape($udom),
2465: $homeserver);
1.1172.2.146. .5(raebu 2466:22): } elsif ($check eq 'unamemap') {
2467:22): $response=&reply('unamemaprules:'.&escape($udom),
2468:22): $homeserver);
1.923 raeburn 2469: } else {
2470: $response=&reply('instuserrules:'.&escape($udom),
2471: $homeserver);
2472: }
1.912 raeburn 2473: if (($response ne 'refused') && ($response ne 'error') &&
1.923 raeburn 2474: ($response ne 'unknown_cmd') &&
1.912 raeburn 2475: ($response ne 'no_such_host')) {
2476: my ($hashitems,$orderitems) = split(/:/,$response);
2477: my @pairs=split(/\&/,$hashitems);
2478: foreach my $item (@pairs) {
2479: my ($key,$value)=split(/=/,$item,2);
2480: $key = &unescape($key);
2481: next if ($key =~ /^error: 2 /);
2482: $ruleshash{$key}=&thaw_unescape($value);
2483: }
2484: my @esc_order = split(/\&/,$orderitems);
2485: foreach my $item (@esc_order) {
2486: push(@ruleorder,&unescape($item));
2487: }
2488: }
2489: }
2490: }
2491: return (\%ruleshash,\@ruleorder);
2492: }
2493:
1.976 raeburn 2494: # ------------- Get Authentication, Language and User Tools Defaults for Domain
1.943 raeburn 2495:
2496: sub get_domain_defaults {
1.1172.2.35 raeburn 2497: my ($domain,$ignore_cache) = @_;
2498: return if (($domain eq '') || ($domain eq 'public'));
1.943 raeburn 2499: my $cachetime = 60*60*24;
1.1172.2.35 raeburn 2500: unless ($ignore_cache) {
2501: my ($result,$cached)=&is_cached_new('domdefaults',$domain);
2502: if (defined($cached)) {
2503: if (ref($result) eq 'HASH') {
2504: return %{$result};
2505: }
1.943 raeburn 2506: }
2507: }
2508: my %domdefaults;
2509: my %domconfig =
1.989 raeburn 2510: &Apache::lonnet::get_dom('configuration',['defaults','quotas',
1.1047 raeburn 2511: 'requestcourses','inststatus',
1.1172.2.9 raeburn 2512: 'coursedefaults','usersessions',
1.1172.2.146. .18(raeb 2513:-24): 'requestauthor','authordefaults',
2514:-24): 'selfenrollment','coursecategories',
2515:-24): 'autoenroll','helpsettings',
2516:-24): 'wafproxy','ltisec','toolsec',
2517:-24): 'domexttool','exttool'],$domain);
1.1172.2.41 raeburn 2518: my @coursetypes = ('official','unofficial','community','textbook');
1.943 raeburn 2519: if (ref($domconfig{'defaults'}) eq 'HASH') {
2520: $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'};
2521: $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
2522: $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
1.982 raeburn 2523: $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
1.985 raeburn 2524: $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
1.1147 raeburn 2525: $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
1.1172.2.146. .12(raeb 2526:-23): $domdefaults{'portal_def_email'} = $domconfig{'defaults'}{'portal_def_email'};
2527:-23): $domdefaults{'portal_def_web'} = $domconfig{'defaults'}{'portal_def_web'};
1.1172.2.91 raeburn 2528: $domdefaults{'intauth_cost'} = $domconfig{'defaults'}{'intauth_cost'};
2529: $domdefaults{'intauth_switch'} = $domconfig{'defaults'}{'intauth_switch'};
2530: $domdefaults{'intauth_check'} = $domconfig{'defaults'}{'intauth_check'};
1.1172.2.146. .5(raebu 2531:22): $domdefaults{'unamemap_rule'} = $domconfig{'defaults'}{'unamemap_rule'};
1.943 raeburn 2532: } else {
2533: $domdefaults{'lang_def'} = &domain($domain,'lang_def');
2534: $domdefaults{'auth_def'} = &domain($domain,'auth_def');
2535: $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
2536: }
1.976 raeburn 2537: if (ref($domconfig{'quotas'}) eq 'HASH') {
2538: if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
2539: $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
2540: } else {
2541: $domdefaults{'defaultquota'} = $domconfig{'quotas'};
1.1172.2.29 raeburn 2542: }
1.1172.2.146. .23(raeb 2543:-24): my @usertools = ('aboutme','blog','webdav','portfolio','portaccess');
1.976 raeburn 2544: foreach my $item (@usertools) {
2545: if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
2546: $domdefaults{$item} = $domconfig{'quotas'}{$item};
2547: }
2548: }
1.1172.2.29 raeburn 2549: if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
2550: $domdefaults{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
2551: }
1.976 raeburn 2552: }
1.985 raeburn 2553: if (ref($domconfig{'requestcourses'}) eq 'HASH') {
1.1172.2.37 raeburn 2554: foreach my $item ('official','unofficial','community','textbook') {
1.985 raeburn 2555: $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
2556: }
2557: }
1.1172.2.146. .18(raeb 2558:-24): if (ref($domconfig{'authordefaults'}) eq 'HASH') {
.23(raeb 2559:-24): foreach my $item ('nocodemirror','copyright','sourceavail','domcoordacc','editors','archive') {
.18(raeb 2560:-24): if ($item eq 'editors') {
2561:-24): if (ref($domconfig{'authordefaults'}{'editors'}) eq 'ARRAY') {
2562:-24): $domdefaults{$item} = join(',',@{$domconfig{'authordefaults'}{'editors'}});
2563:-24): }
2564:-24): } else {
2565:-24): $domdefaults{$item} = $domconfig{'authordefaults'}{$item};
2566:-24): }
2567:-24): }
2568:-24): }
1.1172.2.9 raeburn 2569: if (ref($domconfig{'requestauthor'}) eq 'HASH') {
2570: $domdefaults{'requestauthor'} = $domconfig{'requestauthor'};
2571: }
1.989 raeburn 2572: if (ref($domconfig{'inststatus'}) eq 'HASH') {
1.1172.2.44 raeburn 2573: foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
1.989 raeburn 2574: $domdefaults{$item} = $domconfig{'inststatus'}{$item};
2575: }
2576: }
1.1047 raeburn 2577: if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
1.1172.2.60 raeburn 2578: $domdefaults{'usejsme'} = $domconfig{'coursedefaults'}{'usejsme'};
1.1172.2.144 raeburn 2579: $domdefaults{'inline_chem'} = $domconfig{'coursedefaults'}{'inline_chem'};
1.1172.2.60 raeburn 2580: $domdefaults{'uselcmath'} = $domconfig{'coursedefaults'}{'uselcmath'};
2581: if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
2582: $domdefaults{'postsubmit'} = $domconfig{'coursedefaults'}{'postsubmit'}{'client'};
2583: }
1.1172.2.41 raeburn 2584: foreach my $type (@coursetypes) {
2585: if (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
2586: unless ($type eq 'community') {
2587: $domdefaults{$type.'credits'} = $domconfig{'coursedefaults'}{'coursecredits'}{$type};
2588: }
2589: }
2590: if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
2591: $domdefaults{$type.'quota'} = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
2592: }
1.1172.2.146. .14(raeb 2593:-23): if (ref($domconfig{'coursedefaults'}{'coursequota'}) eq 'HASH') {
2594:-23): $domdefaults{$type.'coursequota'} = $domconfig{'coursedefaults'}{'coursequota'}{$type};
2595:-23): }
1.1172.2.60 raeburn 2596: if ($domdefaults{'postsubmit'} eq 'on') {
2597: if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
2598: $domdefaults{$type.'postsubtimeout'} =
2599: $domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
2600: }
2601: }
1.1172.2.146. .13(raeb 2602:-23): if (ref($domconfig{'coursedefaults'}{'domexttool'}) eq 'HASH') {
2603:-23): $domdefaults{$type.'domexttool'} = $domconfig{'coursedefaults'}{'domexttool'}{$type};
2604:-23): } else {
2605:-23): $domdefaults{$type.'domexttool'} = 1;
2606:-23): }
2607:-23): if (ref($domconfig{'coursedefaults'}{'exttool'}) eq 'HASH') {
2608:-23): $domdefaults{$type.'exttool'} = $domconfig{'coursedefaults'}{'exttool'}{$type};
2609:-23): } else {
2610:-23): $domdefaults{$type.'exttool'} = 0;
2611:-23): }
1.1172.2.30 raeburn 2612: }
1.1172.2.67 raeburn 2613: if (ref($domconfig{'coursedefaults'}{'canclone'}) eq 'HASH') {
2614: if (ref($domconfig{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
2615: my @clonecodes = @{$domconfig{'coursedefaults'}{'canclone'}{'instcode'}};
2616: if (@clonecodes) {
2617: $domdefaults{'canclone'} = join('+',@clonecodes);
2618: }
2619: }
2620: } elsif ($domconfig{'coursedefaults'}{'canclone'}) {
2621: $domdefaults{'canclone'}=$domconfig{'coursedefaults'}{'canclone'};
2622: }
1.1172.2.103 raeburn 2623: if ($domconfig{'coursedefaults'}{'texengine'}) {
2624: $domdefaults{'texengine'} = $domconfig{'coursedefaults'}{'texengine'};
2625: }
1.1172.2.146. .3(raebu 2626:22): if (exists($domconfig{'coursedefaults'}{'ltiauth'})) {
2627:22): $domdefaults{'crsltiauth'} = $domconfig{'coursedefaults'}{'ltiauth'};
2628:22): }
1.1047 raeburn 2629: }
1.1073 raeburn 2630: if (ref($domconfig{'usersessions'}) eq 'HASH') {
2631: if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
2632: $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
2633: }
2634: if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
2635: $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
2636: }
1.1172.2.62 raeburn 2637: if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
2638: $domdefaults{'offloadnow'} = $domconfig{'usersessions'}{'offloadnow'};
2639: }
1.1172.2.137 raeburn 2640: if (ref($domconfig{'usersessions'}{'offloadoth'}) eq 'HASH') {
1.1172.2.136 raeburn 2641: $domdefaults{'offloadoth'} = $domconfig{'usersessions'}{'offloadoth'};
2642: }
1.1073 raeburn 2643: }
1.1172.2.41 raeburn 2644: if (ref($domconfig{'selfenrollment'}) eq 'HASH') {
2645: if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
2646: my @settings = ('types','registered','enroll_dates','access_dates','section',
2647: 'approval','limit');
2648: foreach my $type (@coursetypes) {
2649: if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
2650: my @mgrdc = ();
2651: foreach my $item (@settings) {
2652: if ($domconfig{'selfenrollment'}{'admin'}{$type}{$item} eq '0') {
2653: push(@mgrdc,$item);
2654: }
2655: }
2656: if (@mgrdc) {
2657: $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
2658: }
2659: }
2660: }
2661: }
2662: if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
2663: foreach my $type (@coursetypes) {
2664: if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
2665: foreach my $item (keys(%{$domconfig{'selfenrollment'}{'default'}{$type}})) {
2666: $domdefaults{$type.'selfenroll'.$item} = $domconfig{'selfenrollment'}{'default'}{$type}{$item};
2667: }
2668: }
2669: }
2670: }
2671: }
1.1172.2.46 raeburn 2672: if (ref($domconfig{'coursecategories'}) eq 'HASH') {
2673: $domdefaults{'catauth'} = 'std';
2674: $domdefaults{'catunauth'} = 'std';
2675: if ($domconfig{'coursecategories'}{'auth'}) {
2676: $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
2677: }
2678: if ($domconfig{'coursecategories'}{'unauth'}) {
2679: $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
2680: }
2681: }
1.1172.2.76 raeburn 2682: if (ref($domconfig{'autoenroll'}) eq 'HASH') {
2683: $domdefaults{'autofailsafe'} = $domconfig{'autoenroll'}{'autofailsafe'};
1.1172.2.146 raeburn 2684: $domdefaults{'failsafe'} = $domconfig{'autoenroll'}{'failsafe'};
1.1172.2.76 raeburn 2685: }
1.1172.2.89 raeburn 2686: if (ref($domconfig{'helpsettings'}) eq 'HASH') {
2687: $domdefaults{'submitbugs'} = $domconfig{'helpsettings'}{'submitbugs'};
2688: if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
2689: $domdefaults{'adhocroles'} = $domconfig{'helpsettings'}{'adhoc'};
2690: }
2691: }
1.1172.2.142 raeburn 2692: if (ref($domconfig{'wafproxy'}) eq 'HASH') {
2693: foreach my $item ('ipheader','trusted','vpnint','vpnext','sslopt') {
2694: if ($domconfig{'wafproxy'}{$item}) {
2695: $domdefaults{'waf_'.$item} = $domconfig{'wafproxy'}{$item};
2696: }
2697: }
2698: }
1.1172.2.146. .4(raebu 2699:22): if (ref($domconfig{'ltisec'}) eq 'HASH') {
2700:22): if (ref($domconfig{'ltisec'}{'encrypt'}) eq 'HASH') {
2701:22): $domdefaults{'linkprotenc_crs'} = $domconfig{'ltisec'}{'encrypt'}{'crs'};
2702:22): $domdefaults{'linkprotenc_dom'} = $domconfig{'ltisec'}{'encrypt'}{'dom'};
2703:22): $domdefaults{'ltienc_consumers'} = $domconfig{'ltisec'}{'encrypt'}{'consumers'};
2704:22): }
2705:22): if (ref($domconfig{'ltisec'}{'private'}) eq 'HASH') {
2706:22): if (ref($domconfig{'ltisec'}{'private'}{'keys'}) eq 'ARRAY') {
.13(raeb 2707:-23): $domdefaults{'ltiprivhosts'} = $domconfig{'ltisec'}{'private'}{'keys'};
2708:-23): }
2709:-23): }
.17(raeb 2710:-24): if (ref($domconfig{'ltisec'}{'suggested'}) eq 'HASH') {
2711:-24): my %suggestions = %{$domconfig{'ltisec'}{'suggested'}};
2712:-24): foreach my $item (keys(%{$domconfig{'ltisec'}{'suggested'}})) {
2713:-24): unless (ref($domconfig{'ltisec'}{'suggested'}{$item}) eq 'HASH') {
2714:-24): delete($suggestions{$item});
2715:-24): }
2716:-24): }
2717:-24): if (keys(%suggestions)) {
2718:-24): $domdefaults{'linkprotsuggested'} = \%suggestions;
2719:-24): }
2720:-24): }
.13(raeb 2721:-23): }
2722:-23): if (ref($domconfig{'toolsec'}) eq 'HASH') {
2723:-23): if (ref($domconfig{'toolsec'}{'encrypt'}) eq 'HASH') {
2724:-23): $domdefaults{'toolenc_crs'} = $domconfig{'toolsec'}{'encrypt'}{'crs'};
2725:-23): $domdefaults{'toolenc_dom'} = $domconfig{'toolsec'}{'encrypt'}{'dom'};
2726:-23): }
2727:-23): if (ref($domconfig{'toolsec'}{'private'}) eq 'HASH') {
2728:-23): if (ref($domconfig{'toolsec'}{'private'}{'keys'}) eq 'ARRAY') {
2729:-23): $domdefaults{'toolprivhosts'} = $domconfig{'toolsec'}{'private'}{'keys'};
.4(raebu 2730:22): }
2731:22): }
2732:22): }
1.1172.2.23 raeburn 2733: &do_cache_new('domdefaults',$domain,\%domdefaults,$cachetime);
1.943 raeburn 2734: return %domdefaults;
2735: }
2736:
1.1172.2.106 raeburn 2737: sub get_dom_cats {
2738: my ($dom) = @_;
2739: return unless (&domain($dom));
2740: my ($cats,$cached)=&is_cached_new('cats',$dom);
2741: unless (defined($cached)) {
2742: my %domconfig = &get_dom('configuration',['coursecategories'],$dom);
2743: if (ref($domconfig{'coursecategories'}) eq 'HASH') {
2744: if (ref($domconfig{'coursecategories'}{'cats'}) eq 'HASH') {
2745: %{$cats} = %{$domconfig{'coursecategories'}{'cats'}};
2746: } else {
2747: $cats = {};
2748: }
2749: } else {
2750: $cats = {};
2751: }
2752: &Apache::lonnet::do_cache_new('cats',$dom,$cats,3600);
2753: }
2754: return $cats;
2755: }
2756:
2757: sub get_dom_instcats {
2758: my ($dom) = @_;
2759: return unless (&domain($dom));
2760: my ($instcats,$cached)=&is_cached_new('instcats',$dom);
2761: unless (defined($cached)) {
2762: my (%coursecodes,%codes,@codetitles,%cat_titles,%cat_order);
2763: my $totcodes = &retrieve_instcodes(\%coursecodes,$dom);
2764: if ($totcodes > 0) {
2765: my $caller = 'global';
2766: if (&auto_instcode_format($caller,$dom,\%coursecodes,\%codes,
2767: \@codetitles,\%cat_titles,\%cat_order) eq 'ok') {
2768: $instcats = {
1.1172.2.146. .10(raeb 2769:-23): totcodes => $totcodes,
1.1172.2.106 raeburn 2770: codes => \%codes,
2771: codetitles => \@codetitles,
2772: cat_titles => \%cat_titles,
2773: cat_order => \%cat_order,
2774: };
2775: &do_cache_new('instcats',$dom,$instcats,3600);
2776: }
2777: }
2778: }
2779: return $instcats;
2780: }
2781:
2782: sub retrieve_instcodes {
2783: my ($coursecodes,$dom) = @_;
2784: my $totcodes;
2785: my %courses = &courseiddump($dom,'.',1,'.','.','.',undef,undef,'Course');
2786: foreach my $course (keys(%courses)) {
2787: if (ref($courses{$course}) eq 'HASH') {
2788: if ($courses{$course}{'inst_code'} ne '') {
2789: $$coursecodes{$course} = $courses{$course}{'inst_code'};
2790: $totcodes ++;
2791: }
2792: }
2793: }
2794: return $totcodes;
2795: }
2796:
1.1172.2.113 raeburn 2797: # --------------------------------------------- Get domain config for passwords
2798:
2799: sub get_passwdconf {
2800: my ($dom) = @_;
2801: my (%passwdconf,$gotconf,$lookup);
2802: my ($result,$cached)=&is_cached_new('passwdconf',$dom);
2803: if (defined($cached)) {
2804: if (ref($result) eq 'HASH') {
2805: %passwdconf = %{$result};
2806: $gotconf = 1;
2807: }
2808: }
2809: unless ($gotconf) {
2810: my %domconfig = &get_dom('configuration',['passwords'],$dom);
2811: if (ref($domconfig{'passwords'}) eq 'HASH') {
2812: %passwdconf = %{$domconfig{'passwords'}};
2813: }
2814: my $cachetime = 24*60*60;
2815: &do_cache_new('passwdconf',$dom,\%passwdconf,$cachetime);
2816: }
2817: return %passwdconf;
2818: }
2819:
1.1172.2.146. .1(raebu 2820:22): sub course_portal_url {
2821:22): my ($cnum,$cdom,$r) = @_;
2822:22): my $chome = &homeserver($cnum,$cdom);
2823:22): my $hostname = &hostname($chome);
2824:22): my $protocol = $protocol{$chome};
2825:22): $protocol = 'http' if ($protocol ne 'https');
2826:22): my %domdefaults = &get_domain_defaults($cdom);
2827:22): my $firsturl;
2828:22): if ($domdefaults{'portal_def'}) {
2829:22): $firsturl = $domdefaults{'portal_def'};
2830:22): } else {
2831:22): my $alias = &Apache::lonnet::use_proxy_alias($r,$chome);
2832:22): $hostname = $alias if ($alias ne '');
2833:22): $firsturl = $protocol.'://'.$hostname;
2834:22): }
2835:22): return $firsturl;
2836:22): }
2837:22):
.12(raeb 2838:-23): sub url_prefix {
2839:-23): my ($r,$dom,$home,$context) = @_;
2840:-23): my $prefix;
2841:-23): my %domdefs = &get_domain_defaults($dom);
2842:-23): if ($domdefs{'portal_def'} && $domdefs{'portal_def_'.$context}) {
2843:-23): if ($domdefs{'portal_def'} =~ m{^(https?://[^/]+)}) {
2844:-23): $prefix = $1;
2845:-23): }
2846:-23): }
2847:-23): if ($prefix eq '') {
2848:-23): my $hostname = &hostname($home);
2849:-23): my $protocol = $protocol{$home};
2850:-23): $protocol = 'http' if ($protocol{$home} ne 'https');
2851:-23): my $alias = &use_proxy_alias($r,$home);
2852:-23): $hostname = $alias if ($alias ne '');
2853:-23): $prefix = $protocol.'://'.$hostname;
2854:-23): }
2855:-23): return $prefix;
2856:-23): }
2857:-23):
1.344 www 2858: # --------------------------------------------------- Assign a key to a student
2859:
2860: sub assign_access_key {
1.364 www 2861: #
2862: # a valid key looks like uname:udom#comments
2863: # comments are being appended
2864: #
1.498 www 2865: my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
2866: $kdom=
1.620 albertel 2867: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
1.498 www 2868: $knum=
1.620 albertel 2869: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
1.344 www 2870: $cdom=
1.620 albertel 2871: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 2872: $cnum=
1.620 albertel 2873: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
2874: $udom=$env{'user.name'} unless (defined($udom));
2875: $uname=$env{'user.domain'} unless (defined($uname));
1.498 www 2876: my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
1.364 www 2877: if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
1.479 albertel 2878: ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) {
1.364 www 2879: # assigned to this person
2880: # - this should not happen,
1.345 www 2881: # unless something went wrong
2882: # the first time around
2883: # ready to assign
1.364 www 2884: $logentry=$1.'; '.$logentry;
1.496 www 2885: if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
1.498 www 2886: $kdom,$knum) eq 'ok') {
1.345 www 2887: # key now belongs to user
1.346 www 2888: my $envkey='key.'.$cdom.'_'.$cnum;
1.345 www 2889: if (&put('environment',{$envkey => $ckey}) eq 'ok') {
1.949 raeburn 2890: &appenv({'environment.'.$envkey => $ckey});
1.345 www 2891: return 'ok';
2892: } else {
2893: return
2894: 'error: Count not permanently assign key, will need to be re-entered later.';
2895: }
2896: } else {
2897: return 'error: Could not assign key, try again later.';
2898: }
1.364 www 2899: } elsif (!$existing{$ckey}) {
1.345 www 2900: # the key does not exist
2901: return 'error: The key does not exist';
2902: } else {
2903: # the key is somebody else's
2904: return 'error: The key is already in use';
2905: }
1.344 www 2906: }
2907:
1.364 www 2908: # ------------------------------------------ put an additional comment on a key
2909:
2910: sub comment_access_key {
2911: #
2912: # a valid key looks like uname:udom#comments
2913: # comments are being appended
2914: #
2915: my ($ckey,$cdom,$cnum,$logentry)=@_;
2916: $cdom=
1.620 albertel 2917: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.364 www 2918: $cnum=
1.620 albertel 2919: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.364 www 2920: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
2921: if ($existing{$ckey}) {
2922: $existing{$ckey}.='; '.$logentry;
2923: # ready to assign
1.367 www 2924: if (&put('accesskeys',{$ckey=>$existing{$ckey}},
1.364 www 2925: $cdom,$cnum) eq 'ok') {
2926: return 'ok';
2927: } else {
2928: return 'error: Count not store comment.';
2929: }
2930: } else {
2931: # the key does not exist
2932: return 'error: The key does not exist';
2933: }
2934: }
2935:
1.344 www 2936: # ------------------------------------------------------ Generate a set of keys
2937:
2938: sub generate_access_keys {
1.364 www 2939: my ($number,$cdom,$cnum,$logentry)=@_;
1.344 www 2940: $cdom=
1.620 albertel 2941: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 2942: $cnum=
1.620 albertel 2943: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.361 www 2944: unless (&allowed('mky',$cdom)) { return 0; }
1.344 www 2945: unless (($cdom) && ($cnum)) { return 0; }
2946: if ($number>10000) { return 0; }
2947: sleep(2); # make sure don't get same seed twice
2948: srand(time()^($$+($$<<15))); # from "Programming Perl"
2949: my $total=0;
2950: for (my $i=1;$i<=$number;$i++) {
2951: my $newkey=sprintf("%lx",int(100000*rand)).'-'.
2952: sprintf("%lx",int(100000*rand)).'-'.
2953: sprintf("%lx",int(100000*rand));
2954: $newkey=~s/1/g/g; # folks mix up 1 and l
2955: $newkey=~s/0/h/g; # and also 0 and O
2956: my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
2957: if ($existing{$newkey}) {
2958: $i--;
2959: } else {
1.364 www 2960: if (&put('accesskeys',
2961: { $newkey => '# generated '.localtime().
1.620 albertel 2962: ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
1.364 www 2963: '; '.$logentry },
2964: $cdom,$cnum) eq 'ok') {
1.344 www 2965: $total++;
2966: }
2967: }
2968: }
1.620 albertel 2969: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.344 www 2970: 'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
2971: return $total;
2972: }
2973:
2974: # ------------------------------------------------------- Validate an accesskey
2975:
2976: sub validate_access_key {
2977: my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
2978: $cdom=
1.620 albertel 2979: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 2980: $cnum=
1.620 albertel 2981: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
2982: $udom=$env{'user.domain'} unless (defined($udom));
2983: $uname=$env{'user.name'} unless (defined($uname));
1.345 www 2984: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1.479 albertel 2985: return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
1.70 www 2986: }
2987:
2988: # ------------------------------------- Find the section of student in a course
1.652 albertel 2989: sub devalidate_getsection_cache {
2990: my ($udom,$unam,$courseid)=@_;
2991: my $hashid="$udom:$unam:$courseid";
2992: &devalidate_cache_new('getsection',$hashid);
2993: }
1.298 matthew 2994:
1.815 albertel 2995: sub courseid_to_courseurl {
2996: my ($courseid) = @_;
2997: #already url style courseid
2998: return $courseid if ($courseid =~ m{^/});
2999:
3000: if (exists($env{'course.'.$courseid.'.num'})) {
3001: my $cnum = $env{'course.'.$courseid.'.num'};
3002: my $cdom = $env{'course.'.$courseid.'.domain'};
3003: return "/$cdom/$cnum";
3004: }
3005:
3006: my %courseinfo=&Apache::lonnet::coursedescription($courseid);
3007: if (exists($courseinfo{'num'})) {
3008: return "/$courseinfo{'domain'}/$courseinfo{'num'}";
3009: }
3010:
3011: return undef;
3012: }
3013:
1.298 matthew 3014: sub getsection {
3015: my ($udom,$unam,$courseid)=@_;
1.599 albertel 3016: my $cachetime=1800;
1.551 albertel 3017:
3018: my $hashid="$udom:$unam:$courseid";
1.599 albertel 3019: my ($result,$cached)=&is_cached_new('getsection',$hashid);
1.551 albertel 3020: if (defined($cached)) { return $result; }
3021:
1.298 matthew 3022: my %Pending;
3023: my %Expired;
3024: #
3025: # Each role can either have not started yet (pending), be active,
3026: # or have expired.
3027: #
3028: # If there is an active role, we are done.
3029: #
3030: # If there is more than one role which has not started yet,
3031: # choose the one which will start sooner
3032: # If there is one role which has not started yet, return it.
3033: #
3034: # If there is more than one expired role, choose the one which ended last.
3035: # If there is a role which has expired, return it.
3036: #
1.815 albertel 3037: $courseid = &courseid_to_courseurl($courseid);
1.1166 raeburn 3038: my %roleshash = &dump('roles',$udom,$unam,$courseid);
1.817 raeburn 3039: foreach my $key (keys(%roleshash)) {
1.479 albertel 3040: next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1.298 matthew 3041: my $section=$1;
3042: if ($key eq $courseid.'_st') { $section=''; }
1.817 raeburn 3043: my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
1.298 matthew 3044: my $now=time;
1.548 albertel 3045: if (defined($end) && $end && ($now > $end)) {
1.298 matthew 3046: $Expired{$end}=$section;
3047: next;
3048: }
1.548 albertel 3049: if (defined($start) && $start && ($now < $start)) {
1.298 matthew 3050: $Pending{$start}=$section;
3051: next;
3052: }
1.599 albertel 3053: return &do_cache_new('getsection',$hashid,$section,$cachetime);
1.298 matthew 3054: }
3055: #
3056: # Presumedly there will be few matching roles from the above
3057: # loop and the sorting time will be negligible.
3058: if (scalar(keys(%Pending))) {
3059: my ($time) = sort {$a <=> $b} keys(%Pending);
1.599 albertel 3060: return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
1.298 matthew 3061: }
3062: if (scalar(keys(%Expired))) {
3063: my @sorted = sort {$a <=> $b} keys(%Expired);
3064: my $time = pop(@sorted);
1.599 albertel 3065: return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
1.298 matthew 3066: }
1.599 albertel 3067: return &do_cache_new('getsection',$hashid,'-1',$cachetime);
1.298 matthew 3068: }
1.70 www 3069:
1.599 albertel 3070: sub save_cache {
3071: &purge_remembered();
1.722 albertel 3072: #&Apache::loncommon::validate_page();
1.620 albertel 3073: undef(%env);
1.780 albertel 3074: undef($env_loaded);
1.599 albertel 3075: }
1.452 albertel 3076:
1.599 albertel 3077: my $to_remember=-1;
3078: my %remembered;
3079: my %accessed;
3080: my $kicks=0;
3081: my $hits=0;
1.849 albertel 3082: sub make_key {
3083: my ($name,$id) = @_;
1.872 albertel 3084: if (length($id) > 65
3085: && length(&escape($id)) > 200) {
3086: $id=length($id).':'.&Digest::MD5::md5_hex($id);
3087: }
1.849 albertel 3088: return &escape($name.':'.$id);
3089: }
3090:
1.599 albertel 3091: sub devalidate_cache_new {
3092: my ($name,$id,$debug) = @_;
3093: if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
1.1172.2.81 raeburn 3094: my $remembered_id=$name.':'.$id;
1.849 albertel 3095: $id=&make_key($name,$id);
1.599 albertel 3096: $memcache->delete($id);
1.1172.2.81 raeburn 3097: delete($remembered{$remembered_id});
3098: delete($accessed{$remembered_id});
1.599 albertel 3099: }
3100:
3101: sub is_cached_new {
3102: my ($name,$id,$debug) = @_;
1.1172.2.81 raeburn 3103: my $remembered_id=$name.':'.$id; # this is to avoid make_key (which is slow) for
3104: # keys in %remembered hash, which persists for
3105: # duration of request (no restriction on key length).
3106: if (exists($remembered{$remembered_id})) {
3107: if ($debug) { &Apache::lonnet::logthis("Early return $remembered_id of $remembered{$remembered_id} "); }
3108: $accessed{$remembered_id}=[&gettimeofday()];
1.599 albertel 3109: $hits++;
1.1172.2.81 raeburn 3110: return ($remembered{$remembered_id},1);
1.599 albertel 3111: }
1.1172.2.81 raeburn 3112: $id=&make_key($name,$id);
1.599 albertel 3113: my $value = $memcache->get($id);
3114: if (!(defined($value))) {
3115: if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
1.417 albertel 3116: return (undef,undef);
1.416 albertel 3117: }
1.599 albertel 3118: if ($value eq '__undef__') {
3119: if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
3120: $value=undef;
3121: }
1.1172.2.81 raeburn 3122: &make_room($remembered_id,$value,$debug);
1.599 albertel 3123: if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
3124: return ($value,1);
3125: }
3126:
3127: sub do_cache_new {
3128: my ($name,$id,$value,$time,$debug) = @_;
1.1172.2.81 raeburn 3129: my $remembered_id=$name.':'.$id;
1.849 albertel 3130: $id=&make_key($name,$id);
1.599 albertel 3131: my $setvalue=$value;
3132: if (!defined($setvalue)) {
3133: $setvalue='__undef__';
3134: }
1.623 albertel 3135: if (!defined($time) ) {
3136: $time=600;
3137: }
1.599 albertel 3138: if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
1.910 albertel 3139: my $result = $memcache->set($id,$setvalue,$time);
3140: if (! $result) {
1.872 albertel 3141: &logthis("caching of id -> $id failed");
1.910 albertel 3142: $memcache->disconnect_all();
1.872 albertel 3143: }
1.600 albertel 3144: # need to make a copy of $value
1.1172.2.81 raeburn 3145: &make_room($remembered_id,$value,$debug);
1.599 albertel 3146: return $value;
3147: }
3148:
3149: sub make_room {
1.1172.2.81 raeburn 3150: my ($remembered_id,$value,$debug)=@_;
1.919 albertel 3151:
1.1172.2.81 raeburn 3152: $remembered{$remembered_id}= (ref($value)) ? &Storable::dclone($value)
1.919 albertel 3153: : $value;
1.599 albertel 3154: if ($to_remember<0) { return; }
1.1172.2.81 raeburn 3155: $accessed{$remembered_id}=[&gettimeofday()];
1.599 albertel 3156: if (scalar(keys(%remembered)) <= $to_remember) { return; }
3157: my $to_kick;
3158: my $max_time=0;
3159: foreach my $other (keys(%accessed)) {
3160: if (&tv_interval($accessed{$other}) > $max_time) {
3161: $to_kick=$other;
3162: $max_time=&tv_interval($accessed{$other});
3163: }
3164: }
3165: delete($remembered{$to_kick});
3166: delete($accessed{$to_kick});
3167: $kicks++;
3168: if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
1.541 albertel 3169: return;
3170: }
3171:
1.599 albertel 3172: sub purge_remembered {
1.604 albertel 3173: #&logthis("Tossing ".scalar(keys(%remembered)));
3174: #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
1.599 albertel 3175: undef(%remembered);
3176: undef(%accessed);
1.428 albertel 3177: }
1.70 www 3178: # ------------------------------------- Read an entry from a user's environment
3179:
3180: sub userenvironment {
3181: my ($udom,$unam,@what)=@_;
1.976 raeburn 3182: my $items;
3183: foreach my $item (@what) {
3184: $items.=&escape($item).'&';
3185: }
3186: $items=~s/\&$//;
1.70 www 3187: my %returnhash=();
1.1009 raeburn 3188: my $uhome = &homeserver($unam,$udom);
3189: unless ($uhome eq 'no_host') {
3190: my @answer=split(/\&/,
3191: &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
1.1048 raeburn 3192: if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
3193: return %returnhash;
3194: }
1.1009 raeburn 3195: my $i;
3196: for ($i=0;$i<=$#what;$i++) {
3197: $returnhash{$what[$i]}=&unescape($answer[$i]);
3198: }
1.70 www 3199: }
3200: return %returnhash;
1.1 albertel 3201: }
3202:
1.617 albertel 3203: # ---------------------------------------------------------- Get a studentphoto
3204: sub studentphoto {
3205: my ($udom,$unam,$ext) = @_;
3206: my $home=&Apache::lonnet::homeserver($unam,$udom);
1.706 raeburn 3207: if (defined($env{'request.course.id'})) {
1.708 raeburn 3208: if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.706 raeburn 3209: if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
3210: return(&retrievestudentphoto($udom,$unam,$ext));
3211: } else {
3212: my ($result,$perm_reqd)=
1.707 albertel 3213: &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706 raeburn 3214: if ($result eq 'ok') {
3215: if (!($perm_reqd eq 'yes')) {
3216: return(&retrievestudentphoto($udom,$unam,$ext));
3217: }
3218: }
3219: }
3220: }
3221: } else {
3222: my ($result,$perm_reqd) =
1.707 albertel 3223: &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706 raeburn 3224: if ($result eq 'ok') {
3225: if (!($perm_reqd eq 'yes')) {
3226: return(&retrievestudentphoto($udom,$unam,$ext));
3227: }
3228: }
3229: }
3230: return '/adm/lonKaputt/lonlogo_broken.gif';
3231: }
3232:
3233: sub retrievestudentphoto {
3234: my ($udom,$unam,$ext,$type) = @_;
3235: my $home=&Apache::lonnet::homeserver($unam,$udom);
3236: my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
3237: if ($ret eq 'ok') {
3238: my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
3239: if ($type eq 'thumbnail') {
3240: $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext";
3241: }
3242: my $tokenurl=&Apache::lonnet::tokenwrapper($url);
3243: return $tokenurl;
3244: } else {
3245: if ($type eq 'thumbnail') {
3246: return '/adm/lonKaputt/genericstudent_tn.gif';
3247: } else {
3248: return '/adm/lonKaputt/lonlogo_broken.gif';
3249: }
1.617 albertel 3250: }
3251: }
3252:
1.263 www 3253: # -------------------------------------------------------------------- New chat
3254:
3255: sub chatsend {
1.724 raeburn 3256: my ($newentry,$anon,$group)=@_;
1.620 albertel 3257: my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
3258: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
3259: my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.263 www 3260: &reply('chatsend:'.$cdom.':'.$cnum.':'.
1.620 albertel 3261: &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
1.724 raeburn 3262: &escape($newentry)).':'.$group,$chome);
1.292 www 3263: }
3264:
3265: # ------------------------------------------ Find current version of a resource
3266:
3267: sub getversion {
3268: my $fname=&clutter(shift);
1.1172.2.10 raeburn 3269: unless ($fname=~m{^(/adm/wrapper|)/res/}) { return -1; }
1.292 www 3270: return ¤tversion(&filelocation('',$fname));
3271: }
3272:
3273: sub currentversion {
3274: my $fname=shift;
3275: my $author=$fname;
3276: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
3277: my ($udom,$uname)=split(/\//,$author);
1.1112 www 3278: my $home=&homeserver($uname,$udom);
1.292 www 3279: if ($home eq 'no_host') {
3280: return -1;
3281: }
1.1112 www 3282: my $answer=&reply("currentversion:$fname",$home);
1.292 www 3283: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
3284: return -1;
3285: }
1.1112 www 3286: return $answer;
1.263 www 3287: }
3288:
1.1111 www 3289: #
3290: # Return special version number of resource if set by override, empty otherwise
3291: #
3292: sub usedversion {
3293: my $fname=shift;
3294: unless ($fname) { $fname=$env{'request.uri'}; }
3295: my ($urlversion)=($fname=~/\.(\d+)\.\w+$/);
3296: if ($urlversion) { return $urlversion; }
3297: return '';
3298: }
3299:
1.1 albertel 3300: # ----------------------------- Subscribe to a resource, return URL if possible
1.11 www 3301:
1.1 albertel 3302: sub subscribe {
3303: my $fname=shift;
1.761 raeburn 3304: if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1.532 albertel 3305: $fname=~s/[\n\r]//g;
1.1 albertel 3306: my $author=$fname;
3307: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
3308: my ($udom,$uname)=split(/\//,$author);
3309: my $home=homeserver($uname,$udom);
1.335 albertel 3310: if ($home eq 'no_host') {
3311: return 'not_found';
1.1 albertel 3312: }
3313: my $answer=reply("sub:$fname",$home);
1.64 www 3314: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
3315: $answer.=' by '.$home;
3316: }
1.1 albertel 3317: return $answer;
3318: }
3319:
1.8 www 3320: # -------------------------------------------------------------- Replicate file
3321:
3322: sub repcopy {
3323: my $filename=shift;
1.23 www 3324: $filename=~s/\/+/\//g;
1.1142 raeburn 3325: my $londocroot = $perlvar{'lonDocRoot'};
3326: if ($filename=~m{^\Q$londocroot/adm/\E}) { return 'ok'; }
1.1164 raeburn 3327: if ($filename=~m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
1.1142 raeburn 3328: if ($filename=~m{^\Q$londocroot/userfiles/\E} or
3329: $filename=~m{^/*(uploaded|editupload)/}) {
1.538 albertel 3330: return &repcopy_userfile($filename);
3331: }
1.532 albertel 3332: $filename=~s/[\n\r]//g;
1.8 www 3333: my $transname="$filename.in.transfer";
1.828 www 3334: # FIXME: this should flock
1.607 raeburn 3335: if ((-e $filename) || (-e $transname)) { return 'ok'; }
1.8 www 3336: my $remoteurl=subscribe($filename);
1.64 www 3337: if ($remoteurl =~ /^con_lost by/) {
3338: &logthis("Subscribe returned $remoteurl: $filename");
1.607 raeburn 3339: return 'unavailable';
1.8 www 3340: } elsif ($remoteurl eq 'not_found') {
1.441 albertel 3341: #&logthis("Subscribe returned not_found: $filename");
1.607 raeburn 3342: return 'not_found';
1.64 www 3343: } elsif ($remoteurl =~ /^rejected by/) {
3344: &logthis("Subscribe returned $remoteurl: $filename");
1.607 raeburn 3345: return 'forbidden';
1.20 www 3346: } elsif ($remoteurl eq 'directory') {
1.607 raeburn 3347: return 'ok';
1.8 www 3348: } else {
1.290 www 3349: my $author=$filename;
3350: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
3351: my ($udom,$uname)=split(/\//,$author);
3352: my $home=homeserver($uname,$udom);
3353: unless ($home eq $perlvar{'lonHostID'}) {
1.8 www 3354: my @parts=split(/\//,$filename);
3355: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
1.1142 raeburn 3356: if ($path ne "$londocroot/res") {
1.8 www 3357: &logthis("Malconfiguration for replication: $filename");
1.607 raeburn 3358: return 'bad_request';
1.8 www 3359: }
3360: my $count;
3361: for ($count=5;$count<$#parts;$count++) {
3362: $path.="/$parts[$count]";
3363: if ((-e $path)!=1) {
3364: mkdir($path,0777);
3365: }
3366: }
3367: my $ua=new LWP::UserAgent;
3368: my $request=new HTTP::Request('GET',"$remoteurl");
3369: my $response=$ua->request($request,$transname);
3370: if ($response->is_error()) {
3371: unlink($transname);
3372: my $message=$response->status_line;
1.672 albertel 3373: &logthis("<font color=\"blue\">WARNING:"
1.12 www 3374: ." LWP get: $message: $filename</font>");
1.607 raeburn 3375: return 'unavailable';
1.8 www 3376: } else {
1.16 www 3377: if ($remoteurl!~/\.meta$/) {
3378: my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
3379: my $mresponse=$ua->request($mrequest,$filename.'.meta');
3380: if ($mresponse->is_error()) {
3381: unlink($filename.'.meta');
3382: &logthis(
1.672 albertel 3383: "<font color=\"yellow\">INFO: No metadata: $filename</font>");
1.16 www 3384: }
3385: }
1.8 www 3386: rename($transname,$filename);
1.607 raeburn 3387: return 'ok';
1.8 www 3388: }
1.290 www 3389: }
1.8 www 3390: }
1.330 www 3391: }
3392:
1.1172.2.124 raeburn 3393: # ------------------------------------------------- Unsubscribe from a resource
3394:
3395: sub unsubscribe {
3396: my ($fname) = @_;
3397: my $answer;
3398: if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return $answer; }
3399: $fname=~s/[\n\r]//g;
3400: my $author=$fname;
3401: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
3402: my ($udom,$uname)=split(/\//,$author);
3403: my $home=homeserver($uname,$udom);
3404: if ($home eq 'no_host') {
3405: $answer = 'no_host';
3406: } elsif (grep { $_ eq $home } ¤t_machine_ids()) {
3407: $answer = 'home';
3408: } else {
1.1172.2.125 raeburn 3409: $answer = reply("unsub:$fname",$home);
1.1172.2.124 raeburn 3410: }
3411: return $answer;
3412: }
3413:
1.330 www 3414: # ------------------------------------------------ Get server side include body
3415: sub ssi_body {
1.381 albertel 3416: my ($filelink,%form)=@_;
1.606 matthew 3417: if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
3418: $form{'LONCAPA_INTERNAL_no_discussion'}='true';
3419: }
1.953 www 3420: my $output='';
3421: my $response;
1.980 raeburn 3422: if ($filelink=~/^https?\:/) {
1.954 raeburn 3423: ($output,$response)=&externalssi($filelink);
1.953 www 3424: } else {
1.1004 droeschl 3425: $filelink .= $filelink=~/\?/ ? '&' : '?';
3426: $filelink .= 'inhibitmenu=yes';
1.953 www 3427: ($output,$response)=&ssi($filelink,%form);
3428: }
1.778 albertel 3429: $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
1.451 albertel 3430: $output=~s/^.*?\<body[^\>]*\>//si;
1.930 albertel 3431: $output=~s/\<\/body\s*\>.*?$//si;
1.953 www 3432: if (wantarray) {
3433: return ($output, $response);
3434: } else {
3435: return $output;
3436: }
1.8 www 3437: }
3438:
1.15 www 3439: # --------------------------------------------------------- Server Side Include
3440:
1.782 albertel 3441: sub absolute_url {
1.1172.2.145 raeburn 3442: my ($host_name,$unalias,$keep_proto) = @_;
1.782 albertel 3443: my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
3444: if ($host_name eq '') {
3445: $host_name = $ENV{'SERVER_NAME'};
3446: }
1.1172.2.145 raeburn 3447: if ($unalias) {
3448: my $alias = &get_proxy_alias();
3449: if ($alias eq $host_name) {
3450: my $lonhost = $perlvar{'lonHostID'};
3451: my $hostname = &hostname($lonhost);
3452: my $lcproto;
3453: if (($keep_proto) || ($hostname eq '')) {
3454: $lcproto = $protocol;
3455: } else {
3456: $lcproto = $protocol{$lonhost};
3457: $lcproto = 'http' if ($lcproto ne 'https');
3458: $lcproto .= '://';
3459: }
3460: unless ($hostname eq '') {
3461: return $lcproto.$hostname;
3462: }
3463: }
3464: }
1.782 albertel 3465: return $protocol.$host_name;
3466: }
3467:
1.942 foxr 3468: #
3469: # Server side include.
3470: # Parameters:
3471: # fn Possibly encrypted resource name/id.
3472: # form Hash that describes how the rendering should be done
3473: # and other things.
1.944 foxr 3474: # Returns:
1.950 raeburn 3475: # Scalar context: The content of the response.
3476: # Array context: 2 element list of the content and the full response object.
1.942 foxr 3477: #
1.15 www 3478: sub ssi {
3479:
1.944 foxr 3480: my ($fn,%form)=@_;
1.1172.2.145 raeburn 3481: my ($host,$request,$response);
3482: $host = &absolute_url('',1);
1.711 albertel 3483:
3484: $form{'no_update_last_known'}=1;
1.895 albertel 3485: &Apache::lonenc::check_encrypt(\$fn);
1.23 www 3486: if (%form) {
1.1172.2.145 raeburn 3487: $request=new HTTP::Request('POST',$host.$fn);
1.1172.2.58 raeburn 3488: $request->content(join('&',map {
3489: my $name = escape($_);
3490: "$name=" . ( ref($form{$_}) eq 'ARRAY'
3491: ? join("&$name=", map {escape($_) } @{$form{$_}})
3492: : &escape($form{$_}) );
3493: } keys(%form)));
1.23 www 3494: } else {
1.1172.2.145 raeburn 3495: $request=new HTTP::Request('GET',$host.$fn);
1.23 www 3496: }
3497:
1.15 www 3498: $request->header(Cookie => $ENV{'HTTP_COOKIE'});
1.1172.2.100 raeburn 3499:
1.1172.2.101 raeburn 3500: if (($env{'request.course.id'}) &&
3501: ($form{'grade_courseid'} eq $env{'request.course.id'}) &&
3502: ($form{'grade_username'} ne '') && ($form{'grade_domain'} ne '') &&
3503: ($form{'grade_symb'} ne '') &&
3504: (&Apache::lonnet::allowed('mgr',$env{'request.course.id'}.
3505: ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:'')))) {
1.1172.2.100 raeburn 3506: if (LWP::UserAgent->VERSION >= 5.834) {
3507: my $ua=new LWP::UserAgent;
3508: $ua->local_address('127.0.0.1');
3509: $response = $ua->request($request);
3510: } else {
3511: {
3512: require LWP::Protocol::http;
3513: local @LWP::Protocol::http::EXTRA_SOCK_OPTS = (LocalAddr => '127.0.0.1');
3514: my $ua=new LWP::UserAgent;
3515: $response = $ua->request($request);
3516: @LWP::Protocol::http::EXTRA_SOCK_OPTS = ();
3517: }
3518: }
3519: } else {
3520: my $ua=new LWP::UserAgent;
3521: $response = $ua->request($request);
3522: }
1.944 foxr 3523: if (wantarray) {
1.1172.2.3 raeburn 3524: return ($response->content, $response);
1.944 foxr 3525: } else {
1.1172.2.3 raeburn 3526: return $response->content;
1.942 foxr 3527: }
1.324 www 3528: }
3529:
3530: sub externalssi {
3531: my ($url)=@_;
3532: my $ua=new LWP::UserAgent;
3533: my $request=new HTTP::Request('GET',$url);
3534: my $response=$ua->request($request);
1.954 raeburn 3535: if (wantarray) {
3536: return ($response->content, $response);
3537: } else {
3538: return $response->content;
3539: }
1.15 www 3540: }
1.254 www 3541:
1.1172.2.98 raeburn 3542: # If the local copy of a replicated resource is outdated, trigger a
3543: # connection from the homeserver to flush the delayed queue. If no update
3544: # happens, remove local copies of outdated resource (and corresponding
3545: # metadata file).
3546:
3547: sub remove_stale_resfile {
3548: my ($url) = @_;
3549: my $removed;
3550: if ($url=~m{^/res/($match_domain)/($match_username)/}) {
3551: my $audom = $1;
3552: my $auname = $2;
3553: unless (($url =~ /\.\d+\.\w+$/) || ($url =~ m{^/res/lib/templates/})) {
3554: my $homeserver = &homeserver($auname,$audom);
3555: unless (($homeserver eq 'no_host') ||
3556: (grep { $_ eq $homeserver } ¤t_machine_ids())) {
3557: my $fname = &filelocation('',$url);
3558: if (-e $fname) {
3559: my $hostname = &hostname($homeserver);
3560: if ($hostname) {
1.1172.2.120 raeburn 3561: my $protocol = $protocol{$homeserver};
3562: $protocol = 'http' if ($protocol ne 'https');
1.1172.2.98 raeburn 3563: my $uri = $protocol.'://'.$hostname.'/raw/'.&declutter($url);
1.1172.2.120 raeburn 3564: my $ua=new LWP::UserAgent;
3565: $ua->timeout(5);
1.1172.2.98 raeburn 3566: my $request=new HTTP::Request('HEAD',$uri);
3567: my $response=$ua->request($request);
3568: if ($response->is_success()) {
3569: my $remmodtime = &HTTP::Date::str2time( $response->header('Last-modified') );
3570: my $locmodtime = (stat($fname))[9];
3571: if ($locmodtime < $remmodtime) {
3572: my $stale;
3573: my $answer = &reply('pong',$homeserver);
3574: if ($answer eq $homeserver.':'.$perlvar{'lonHostID'}) {
3575: sleep(0.2);
3576: $locmodtime = (stat($fname))[9];
3577: if ($locmodtime < $remmodtime) {
3578: my $posstransfer = $fname.'.in.transfer';
3579: if ((-e $posstransfer) && ($remmodtime < (stat($posstransfer))[9])) {
3580: $removed = 1;
3581: } else {
3582: $stale = 1;
3583: }
3584: } else {
3585: $removed = 1;
3586: }
3587: } else {
3588: $stale = 1;
3589: }
3590: if ($stale) {
1.1172.2.124 raeburn 3591: if (unlink($fname)) {
3592: if ($uri!~/\.meta$/) {
3593: if (-e $fname.'.meta') {
3594: unlink($fname.'.meta');
3595: }
3596: }
3597: my $unsubresult = &unsubscribe($fname);
3598: unless ($unsubresult eq 'ok') {
3599: &logthis("no unsub of $fname from $homeserver, reason: $unsubresult");
3600: }
3601: $removed = 1;
1.1172.2.98 raeburn 3602: }
3603: }
3604: }
3605: }
3606: }
3607: }
3608: }
3609: }
3610: }
3611: return $removed;
3612: }
3613:
1.492 albertel 3614: # -------------------------------- Allow a /uploaded/ URI to be vouched for
3615:
3616: sub allowuploaded {
3617: my ($srcurl,$url)=@_;
3618: $url=&clutter(&declutter($url));
3619: my $dir=$url;
3620: $dir=~s/\/[^\/]+$//;
3621: my %httpref=();
3622: my $httpurl=&hreflocation('',$url);
3623: $httpref{'httpref.'.$httpurl}=$srcurl;
1.949 raeburn 3624: &Apache::lonnet::appenv(\%httpref);
1.254 www 3625: }
1.477 raeburn 3626:
1.1172.2.13 raeburn 3627: #
3628: # Determine if the current user should be able to edit a particular resource,
3629: # when viewing in course context.
3630: # (a) When viewing resource used to determine if "Edit" item is included in
3631: # Functions.
3632: # (b) When displaying folder contents in course editor, used to determine if
3633: # "Edit" link will be displayed alongside resource.
3634: #
3635: # input: six args -- filename (decluttered), course number, course domain,
3636: # url, symb (if registered) and group (if this is a group
3637: # item -- e.g., bulletin board, group page etc.).
3638: # output: array of five scalars --
3639: # $cfile -- url for file editing if editable on current server
3640: # $home -- homeserver of resource (i.e., for author if published,
3641: # or course if uploaded.).
3642: # $switchserver -- 1 if server switch will be needed.
3643: # $forceedit -- 1 if icon/link should be to go to edit mode
3644: # $forceview -- 1 if icon/link should be to go to view mode
3645: #
3646:
3647: sub can_edit_resource {
3648: my ($file,$cnum,$cdom,$resurl,$symb,$group) = @_;
3649: my ($cfile,$home,$switchserver,$forceedit,$forceview,$uploaded,$incourse);
3650: #
3651: # For aboutme pages user can only edit his/her own.
3652: #
3653: if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
3654: my ($sdom,$sname) = ($1,$2);
3655: if (($sdom eq $env{'user.domain'}) && ($sname eq $env{'user.name'})) {
3656: $home = $env{'user.home'};
3657: $cfile = $resurl;
3658: if ($env{'form.forceedit'}) {
3659: $forceview = 1;
3660: } else {
3661: $forceedit = 1;
3662: }
3663: return ($cfile,$home,$switchserver,$forceedit,$forceview);
3664: } else {
3665: return;
3666: }
3667: }
3668:
1.1172.2.146. .22(raeb 3669:-24): #
3670:-24): # For /adm/viewcoauthors can only edit if author or co-author who is manager.
3671:-24): #
3672:-24):
3673:-24): if (($resurl eq '/adm/viewcoauthors') && ($cnum ne '') && ($cdom ne '')) {
3674:-24): if (((&allowed('cca',"$cdom/$cnum")) ||
3675:-24): (&allowed('caa',"$cdom/$cnum"))) ||
3676:-24): ((&allowed('vca',"$cdom/$cnum") ||
3677:-24): &allowed('vaa',"$cdom/$cnum")) &&
3678:-24): ($env{"environment.internal.manager./$cdom/$cnum"}))) {
3679:-24): $home = $env{'user.home'};
3680:-24): $cfile = $resurl;
3681:-24): if ($env{'form.forceedit'}) {
3682:-24): $forceview = 1;
3683:-24): } else {
3684:-24): $forceedit = 1;
3685:-24): }
3686:-24): return ($cfile,$home,$switchserver,$forceedit,$forceview);
3687:-24): } else {
3688:-24): return;
3689:-24): }
3690:-24): }
3691:-24):
1.1172.2.13 raeburn 3692: if ($env{'request.course.id'}) {
3693: my $crsedit = &Apache::lonnet::allowed('mdc',$env{'request.course.id'});
3694: if ($group ne '') {
3695: # if this is a group homepage or group bulletin board, check group privs
3696: my $allowed = 0;
3697: if ($resurl =~ m{^/?adm/$cdom/$cnum/$group/smppg$}) {
3698: if ((&allowed('mdg',$env{'request.course.id'}.
3699: ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
3700: (&allowed('mgh',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
3701: $allowed = 1;
3702: }
3703: } elsif ($resurl =~ m{^/?adm/$cdom/$cnum/\d+/bulletinboard$}) {
3704: if ((&allowed('mdg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
3705: (&allowed('cgb',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
3706: $allowed = 1;
3707: }
3708: }
3709: if ($allowed) {
3710: $home=&homeserver($cnum,$cdom);
3711: if ($env{'form.forceedit'}) {
3712: $forceview = 1;
3713: } else {
3714: $forceedit = 1;
3715: }
3716: $cfile = $resurl;
3717: } else {
3718: return;
3719: }
3720: } else {
1.1172.2.15 raeburn 3721: if ($resurl =~ m{^/?adm/viewclasslist$}) {
3722: unless (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
3723: return;
3724: }
3725: } elsif (!$crsedit) {
1.1172.2.146. .21(raeb 3726:-24): if ($env{'request.role'} =~ m{^st\./$cdom/$cnum}) {
1.1172.2.13 raeburn 3727: #
3728: # No edit allowed where CC has switched to student role.
3729: #
1.1172.2.146. .21(raeb 3730:-24): return;
3731:-24): } elsif (($resurl !~ m{^/res/$match_domain/$match_username/}) ||
3732:-24): ($resurl =~ m{^/res/lib/templates/})) {
3733:-24): return;
3734:-24): }
1.1172.2.13 raeburn 3735: }
3736: }
3737: }
3738:
3739: if ($file ne '') {
3740: if (($cnum =~ /$match_courseid/) && ($cdom =~ /$match_domain/)) {
3741: if (&is_course_upload($file,$cnum,$cdom)) {
3742: $uploaded = 1;
3743: $incourse = 1;
3744: if ($file =~/\.(htm|html|css|js|txt)$/) {
3745: $cfile = &hreflocation('',$file);
3746: if ($env{'form.forceedit'}) {
3747: $forceview = 1;
3748: } else {
3749: $forceedit = 1;
3750: }
3751: }
3752: } elsif ($resurl =~ m{^/public/$cdom/$cnum/syllabus}) {
3753: $incourse = 1;
3754: if ($env{'form.forceedit'}) {
3755: $forceview = 1;
3756: } else {
3757: $forceedit = 1;
3758: }
3759: $cfile = $resurl;
3760: } elsif (($resurl ne '') && (&is_on_map($resurl))) {
3761: if ($resurl =~ m{^/adm/$match_domain/$match_username/\d+/smppg|bulletinboard$}) {
3762: $incourse = 1;
3763: if ($env{'form.forceedit'}) {
3764: $forceview = 1;
3765: } else {
3766: $forceedit = 1;
3767: }
3768: $cfile = $resurl;
3769: } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem') {
3770: $incourse = 1;
3771: $cfile = $resurl.'/smpedit';
3772: } elsif ($resurl =~ m{^/adm/wrapper/ext/}) {
3773: $incourse = 1;
3774: if ($env{'form.forceedit'}) {
3775: $forceview = 1;
3776: } else {
3777: $forceedit = 1;
3778: }
3779: $cfile = $resurl;
1.1172.2.122 raeburn 3780: } elsif (($resurl =~ m{^/ext/}) && ($symb ne '')) {
3781: my ($map,$id,$res) = &decode_symb($symb);
3782: if ($map =~ /\.page$/) {
3783: $incourse = 1;
3784: if ($env{'form.forceedit'}) {
3785: $forceview = 1;
3786: $cfile = $map;
3787: } else {
3788: $forceedit = 1;
3789: $cfile = '/adm/wrapper'.$resurl;
3790: }
3791: }
1.1172.2.146. .1(raebu 3792:22): } elsif ($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) {
3793:22): $incourse = 1;
3794:22): if ($env{'form.forceedit'}) {
3795:22): $forceview = 1;
3796:22): } else {
3797:22): $forceedit = 1;
3798:22): }
3799:22): $cfile = $resurl;
1.1172.2.15 raeburn 3800: } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
3801: $incourse = 1;
3802: if ($env{'form.forceedit'}) {
3803: $forceview = 1;
3804: } else {
3805: $forceedit = 1;
3806: }
3807: $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
1.1172.2.13 raeburn 3808: }
3809: } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
3810: my $template = '/res/lib/templates/simpleproblem.problem';
3811: if (&is_on_map($template)) {
3812: $incourse = 1;
3813: $forceview = 1;
3814: $cfile = $template;
3815: }
3816: } elsif (($resurl =~ m{^/adm/wrapper/ext/}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
1.1172.2.118 raeburn 3817: $incourse = 1;
3818: if ($env{'form.forceedit'}) {
3819: $forceview = 1;
3820: } else {
3821: $forceedit = 1;
3822: }
3823: $cfile = $resurl;
1.1172.2.146. .1(raebu 3824:22): } elsif (($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
3825:22): $incourse = 1;
3826:22): if ($env{'form.forceedit'}) {
3827:22): $forceview = 1;
3828:22): } else {
3829:22): $forceedit = 1;
3830:22): }
3831:22): $cfile = $resurl;
1.1172.2.13 raeburn 3832: } elsif (($resurl eq '/adm/extresedit') && ($symb || $env{'form.folderpath'})) {
3833: $incourse = 1;
3834: $forceview = 1;
3835: if ($symb) {
3836: my ($map,$id,$res)=&decode_symb($symb);
3837: $env{'request.symb'} = $symb;
3838: $cfile = &clutter($res);
3839: } else {
3840: $cfile = $env{'form.suppurl'};
1.1172.2.146. .1(raebu 3841:22): my $escfile = &unescape($cfile);
3842:22): if ($escfile =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
3843:22): $cfile = '/adm/wrapper'.$escfile;
3844:22): } else {
3845:22): $escfile =~ s{^http://}{};
3846:22): $cfile = &escape("/adm/wrapper/ext/$escfile");
3847:22): }
1.1172.2.13 raeburn 3848: }
1.1172.2.31 raeburn 3849: } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
3850: if ($env{'form.forceedit'}) {
3851: $forceview = 1;
3852: } else {
3853: $forceedit = 1;
3854: }
3855: $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
1.1172.2.13 raeburn 3856: }
3857: }
3858: if ($uploaded || $incourse) {
3859: $home=&homeserver($cnum,$cdom);
1.1172.2.14 raeburn 3860: } elsif ($file !~ m{/$}) {
1.1172.2.13 raeburn 3861: $file=~s{^(priv/$match_domain/$match_username)}{/$1};
3862: $file=~s{^($match_domain/$match_username)}{/priv/$1};
3863: # Check that the user has permission to edit this resource
3864: my $setpriv = 1;
3865: my ($cfuname,$cfudom)=&constructaccess($file,$setpriv);
3866: if (defined($cfudom)) {
3867: $home=&homeserver($cfuname,$cfudom);
3868: $cfile=$file;
3869: }
3870: }
3871: if (($cfile ne '') && (!$incourse || $uploaded) &&
3872: (($home ne '') && ($home ne 'no_host'))) {
3873: my @ids=¤t_machine_ids();
3874: unless (grep(/^\Q$home\E$/,@ids)) {
3875: $switchserver=1;
3876: }
3877: }
3878: }
3879: return ($cfile,$home,$switchserver,$forceedit,$forceview);
3880: }
3881:
3882: sub is_course_upload {
3883: my ($file,$cnum,$cdom) = @_;
3884: my $uploadpath = &LONCAPA::propath($cdom,$cnum);
3885: $uploadpath =~ s{^\/}{};
3886: if (($file =~ m{^\Q$uploadpath\E/userfiles/(docs|supplemental)/}) ||
3887: ($file =~ m{^userfiles/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/})) {
3888: return 1;
3889: }
3890: return;
3891: }
3892:
3893: sub in_course {
3894: my ($udom,$uname,$cdom,$cnum,$type,$hideprivileged) = @_;
3895: if ($hideprivileged) {
3896: my $skipuser;
1.1172.2.23 raeburn 3897: my %coursehash = &coursedescription($cdom.'_'.$cnum);
3898: my @possdoms = ($cdom);
3899: if ($coursehash{'checkforpriv'}) {
3900: push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
3901: }
3902: if (&privileged($uname,$udom,\@possdoms)) {
1.1172.2.13 raeburn 3903: $skipuser = 1;
3904: if ($coursehash{'nothideprivileged'}) {
3905: foreach my $item (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
3906: my $user;
3907: if ($item =~ /:/) {
3908: $user = $item;
3909: } else {
3910: $user = join(':',split(/[\@]/,$item));
3911: }
3912: if ($user eq $uname.':'.$udom) {
3913: undef($skipuser);
3914: last;
3915: }
3916: }
3917: }
3918: if ($skipuser) {
3919: return 0;
3920: }
3921: }
3922: }
3923: $type ||= 'any';
3924: if (!defined($cdom) || !defined($cnum)) {
3925: my $cid = $env{'request.course.id'};
3926: $cdom = $env{'course.'.$cid.'.domain'};
3927: $cnum = $env{'course.'.$cid.'.num'};
3928: }
3929: my $typesref;
3930: if (($type eq 'any') || ($type eq 'all')) {
3931: $typesref = ['active','previous','future'];
3932: } elsif ($type eq 'previous' || $type eq 'future') {
3933: $typesref = [$type];
3934: }
3935: my %roles = &get_my_roles($uname,$udom,'userroles',
3936: $typesref,undef,[$cdom]);
3937: my ($tmp) = keys(%roles);
3938: return 0 if ($tmp =~ /^(con_lost|error|no_such_host)/i);
3939: my @course_roles = grep(/^\Q$cnum\E:\Q$cdom\E:/, keys(%roles));
3940: if (@course_roles > 0) {
3941: return 1;
3942: }
3943: return 0;
3944: }
3945:
1.478 albertel 3946: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1.638 albertel 3947: # input: action, courseID, current domain, intended
1.637 raeburn 3948: # path to file, source of file, instruction to parse file for objects,
3949: # ref to hash for embedded objects,
3950: # ref to hash for codebase of java objects.
1.1095 raeburn 3951: # reference to scalar to accommodate mime type determined
3952: # from File::MMagic if $parser = parse.
1.637 raeburn 3953: #
1.485 raeburn 3954: # output: url to file (if action was uploaddoc),
3955: # ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1.477 raeburn 3956: #
1.478 albertel 3957: # Allows directory structure to be used within lonUsers/../userfiles/ for a
3958: # course.
1.477 raeburn 3959: #
1.478 albertel 3960: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
3961: # will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
3962: # course's home server.
1.477 raeburn 3963: #
1.478 albertel 3964: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
3965: # be copied from $source (current location) to
3966: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
3967: # and will then be copied to
3968: # /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
3969: # course's home server.
1.485 raeburn 3970: #
1.481 raeburn 3971: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1.620 albertel 3972: # will be retrived from $env{form.uploaddoc} (from DOCS interface) to
1.481 raeburn 3973: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
3974: # and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
3975: # in course's home server.
1.637 raeburn 3976: #
1.477 raeburn 3977:
3978: sub process_coursefile {
1.1095 raeburn 3979: my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
3980: $mimetype)=@_;
1.477 raeburn 3981: my $fetchresult;
1.638 albertel 3982: my $home=&homeserver($docuname,$docudom);
1.477 raeburn 3983: if ($action eq 'propagate') {
1.638 albertel 3984: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
3985: $home);
1.481 raeburn 3986: } else {
1.477 raeburn 3987: my $fpath = '';
3988: my $fname = $file;
1.478 albertel 3989: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1.477 raeburn 3990: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1.637 raeburn 3991: my $filepath = &build_filepath($fpath);
1.481 raeburn 3992: if ($action eq 'copy') {
3993: if ($source eq '') {
3994: $fetchresult = 'no source file';
3995: return $fetchresult;
3996: } else {
3997: my $destination = $filepath.'/'.$fname;
3998: rename($source,$destination);
3999: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 4000: $home);
1.481 raeburn 4001: }
4002: } elsif ($action eq 'uploaddoc') {
1.1172.2.96 raeburn 4003: open(my $fh,'>',$filepath.'/'.$fname);
1.620 albertel 4004: print $fh $env{'form.'.$source};
1.481 raeburn 4005: close($fh);
1.637 raeburn 4006: if ($parser eq 'parse') {
1.1024 raeburn 4007: my $mm = new File::MMagic;
1.1095 raeburn 4008: my $type = $mm->checktype_filename($filepath.'/'.$fname);
4009: if ($type eq 'text/html') {
1.1024 raeburn 4010: my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
4011: unless ($parse_result eq 'ok') {
4012: &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
4013: }
1.637 raeburn 4014: }
1.1095 raeburn 4015: if (ref($mimetype)) {
4016: $$mimetype = $type;
4017: }
1.637 raeburn 4018: }
1.477 raeburn 4019: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 4020: $home);
1.481 raeburn 4021: if ($fetchresult eq 'ok') {
4022: return '/uploaded/'.$fpath.'/'.$fname;
4023: } else {
4024: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638 albertel 4025: ' to host '.$home.': '.$fetchresult);
1.481 raeburn 4026: return '/adm/notfound.html';
4027: }
1.477 raeburn 4028: }
4029: }
1.485 raeburn 4030: unless ( $fetchresult eq 'ok') {
1.477 raeburn 4031: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638 albertel 4032: ' to host '.$home.': '.$fetchresult);
1.477 raeburn 4033: }
4034: return $fetchresult;
4035: }
4036:
1.637 raeburn 4037: sub build_filepath {
4038: my ($fpath) = @_;
4039: my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
4040: unless ($fpath eq '') {
4041: my @parts=split('/',$fpath);
4042: foreach my $part (@parts) {
4043: $filepath.= '/'.$part;
4044: if ((-e $filepath)!=1) {
4045: mkdir($filepath,0777);
4046: }
4047: }
4048: }
4049: return $filepath;
4050: }
4051:
4052: sub store_edited_file {
1.638 albertel 4053: my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
1.637 raeburn 4054: my $file = $primary_url;
4055: $file =~ s#^/uploaded/$docudom/$docuname/##;
4056: my $fpath = '';
4057: my $fname = $file;
4058: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
4059: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
4060: my $filepath = &build_filepath($fpath);
1.1172.2.96 raeburn 4061: open(my $fh,'>',$filepath.'/'.$fname);
1.637 raeburn 4062: print $fh $content;
4063: close($fh);
1.638 albertel 4064: my $home=&homeserver($docuname,$docudom);
1.637 raeburn 4065: $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 4066: $home);
1.637 raeburn 4067: if ($$fetchresult eq 'ok') {
4068: return '/uploaded/'.$fpath.'/'.$fname;
4069: } else {
1.638 albertel 4070: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
4071: ' to host '.$home.': '.$$fetchresult);
1.637 raeburn 4072: return '/adm/notfound.html';
4073: }
4074: }
4075:
1.531 albertel 4076: sub clean_filename {
1.831 albertel 4077: my ($fname,$args)=@_;
1.315 www 4078: # Replace Windows backslashes by forward slashes
1.257 www 4079: $fname=~s/\\/\//g;
1.831 albertel 4080: if (!$args->{'keep_path'}) {
4081: # Get rid of everything but the actual filename
4082: $fname=~s/^.*\/([^\/]+)$/$1/;
4083: }
1.315 www 4084: # Replace spaces by underscores
4085: $fname=~s/\s+/\_/g;
1.1172.2.110 raeburn 4086: # Transliterate non-ascii text to ascii
4087: my $lang = &Apache::lonlocal::current_language();
4088: $fname = &LONCAPA::transliterate::fname_to_ascii($fname,$lang);
1.315 www 4089: # Replace all other weird characters by nothing
1.831 albertel 4090: $fname=~s{[^/\w\.\-]}{}g;
1.540 albertel 4091: # Replace all .\d. sequences with _\d. so they no longer look like version
4092: # numbers
4093: $fname=~s/\.(\d+)(?=\.)/_$1/g;
1.1172.2.140 raeburn 4094: # Replace three or more adjacent underscores with one for consistency
4095: # with loncfile::filename_check() so complete url can be extracted by
4096: # lonnet::decode_symb()
4097: $fname=~s/_{3,}/_/g;
1.531 albertel 4098: return $fname;
4099: }
1.1172.2.110 raeburn 4100:
1.1051 raeburn 4101: # This Function checks if an Image's dimensions exceed either $resizewidth (width)
4102: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an
4103: # image with the same aspect ratio as the original, but with dimensions which do
4104: # not exceed $resizewidth and $resizeheight.
4105:
1.984 neumanie 4106: sub resizeImage {
1.1051 raeburn 4107: my ($img_path,$resizewidth,$resizeheight) = @_;
4108: my $ima = Image::Magick->new;
4109: my $resized;
4110: if (-e $img_path) {
4111: $ima->Read($img_path);
4112: if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
4113: my $width = $ima->Get('width');
4114: my $height = $ima->Get('height');
4115: if ($width > $resizewidth) {
4116: my $factor = $width/$resizewidth;
4117: my $newheight = $height/$factor;
4118: $ima->Scale(width=>$resizewidth,height=>$newheight);
4119: $resized = 1;
4120: }
4121: }
4122: if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
4123: my $width = $ima->Get('width');
4124: my $height = $ima->Get('height');
4125: if ($height > $resizeheight) {
4126: my $factor = $height/$resizeheight;
4127: my $newwidth = $width/$factor;
4128: $ima->Scale(width=>$newwidth,height=>$resizeheight);
4129: $resized = 1;
4130: }
4131: }
4132: if ($resized) {
4133: $ima->Write($img_path);
4134: }
4135: }
4136: return;
1.977 amueller 4137: }
4138:
1.608 albertel 4139: # --------------- Take an uploaded file and put it into the userfiles directory
1.686 albertel 4140: # input: $formname - the contents of the file are in $env{"form.$formname"}
1.1093 raeburn 4141: # the desired filename is in $env{"form.$formname.filename"}
1.1090 raeburn 4142: # $context - possible values: coursedoc, existingfile, overwrite,
1.1172.2.146. .13(raeb 4143:-23): # canceloverwrite, scantron, toollogo or ''.
1.1090 raeburn 4144: # if 'coursedoc': upload to the current course
4145: # if 'existingfile': write file to tmp/overwrites directory
4146: # if 'canceloverwrite': delete file written to tmp/overwrites directory
4147: # $context is passed as argument to &finishuserfileupload
1.686 albertel 4148: # $subdir - directory in userfile to store the file into
1.1172.2.109 raeburn 4149: # $parser - instruction to parse file for objects ($parser = parse) or
4150: # if context is 'scantron', $parser is hashref of csv column mapping
4151: # (e.g.,{ PaperID => 0, LastName => 1, FirstName => 2, ID => 3,
4152: # Section => 4, CODE => 5, FirstQuestion => 9 }).
1.858 raeburn 4153: # $allfiles - reference to hash for embedded objects
4154: # $codebase - reference to hash for codebase of java objects
1.1172.2.146. .13(raeb 4155:-23): # $destuname - username for permanent storage of uploaded file
4156:-23): # $destudom - domain for permanaent storage of uploaded file
1.860 raeburn 4157: # $thumbwidth - width (pixels) of thumbnail to make for uploaded image
4158: # $thumbheight - height (pixels) of thumbnail to make for uploaded image
1.1051 raeburn 4159: # $resizewidth - width (pixels) to which to resize uploaded image
4160: # $resizeheight - height (pixels) to which to resize uploaded image
1.1095 raeburn 4161: # $mimetype - reference to scalar to accommodate mime type determined
1.1152 raeburn 4162: # from File::MMagic.
1.858 raeburn 4163: #
1.686 albertel 4164: # output: url of file in userspace, or error: <message>
4165: # or /adm/notfound.html if failure to upload occurse
1.608 albertel 4166:
1.531 albertel 4167: sub userfileupload {
1.1090 raeburn 4168: my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
1.1095 raeburn 4169: $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
1.531 albertel 4170: if (!defined($subdir)) { $subdir='unknown'; }
1.620 albertel 4171: my $fname=$env{'form.'.$formname.'.filename'};
1.531 albertel 4172: $fname=&clean_filename($fname);
1.1090 raeburn 4173: # See if there is anything left
1.257 www 4174: unless ($fname) { return 'error: no uploaded file'; }
1.1172.2.110 raeburn 4175: # If filename now begins with a . prepend unix timestamp _ milliseconds
4176: if ($fname =~ /^\./) {
4177: my ($s,$usec) = &gettimeofday();
4178: while (length($usec) < 6) {
4179: $usec = '0'.$usec;
4180: }
4181: $fname = $s.'_'.substr($usec,0,3).$fname;
4182: }
1.1090 raeburn 4183: # Files uploaded to help request form, or uploaded to "create course" page are handled differently
4184: if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
4185: (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
4186: ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
1.523 raeburn 4187: my $now = time;
1.1090 raeburn 4188: my $filepath;
1.1095 raeburn 4189: if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
1.1090 raeburn 4190: $filepath = 'tmp/helprequests/'.$now;
4191: } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
4192: $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
4193: '_'.$env{'user.domain'}.'/pending';
4194: } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
4195: my ($docuname,$docudom);
1.1172.2.96 raeburn 4196: if ($destudom =~ /^$match_domain$/) {
1.1090 raeburn 4197: $docudom = $destudom;
4198: } else {
4199: $docudom = $env{'user.domain'};
4200: }
1.1172.2.96 raeburn 4201: if ($destuname =~ /^$match_username$/) {
1.1090 raeburn 4202: $docuname = $destuname;
4203: } else {
4204: $docuname = $env{'user.name'};
4205: }
4206: if (exists($env{'form.group'})) {
4207: $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
4208: $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
4209: }
4210: $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
4211: if ($context eq 'canceloverwrite') {
4212: my $tempfile = $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
4213: if (-e $tempfile) {
4214: my @info = stat($tempfile);
4215: if ($info[9] eq $env{'form.timestamp'}) {
4216: unlink($tempfile);
4217: }
4218: }
4219: return;
1.523 raeburn 4220: }
4221: }
1.1090 raeburn 4222: # Create the directory if not present
1.741 raeburn 4223: my @parts=split(/\//,$filepath);
4224: my $fullpath = $perlvar{'lonDaemons'};
4225: for (my $i=0;$i<@parts;$i++) {
4226: $fullpath .= '/'.$parts[$i];
4227: if ((-e $fullpath)!=1) {
4228: mkdir($fullpath,0777);
4229: }
4230: }
1.1172.2.96 raeburn 4231: open(my $fh,'>',$fullpath.'/'.$fname);
1.741 raeburn 4232: print $fh $env{'form.'.$formname};
4233: close($fh);
1.1090 raeburn 4234: if ($context eq 'existingfile') {
4235: my @info = stat($fullpath.'/'.$fname);
4236: return ($fullpath.'/'.$fname,$info[9]);
4237: } else {
4238: return $fullpath.'/'.$fname;
4239: }
1.523 raeburn 4240: }
1.995 raeburn 4241: if ($subdir eq 'scantron') {
4242: $fname = 'scantron_orig_'.$fname;
1.1093 raeburn 4243: } else {
1.995 raeburn 4244: $fname="$subdir/$fname";
4245: }
1.1090 raeburn 4246: if ($context eq 'coursedoc') {
1.638 albertel 4247: my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
4248: my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.646 raeburn 4249: if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
1.638 albertel 4250: return &finishuserfileupload($docuname,$docudom,
4251: $formname,$fname,$parser,$allfiles,
1.1051 raeburn 4252: $codebase,$thumbwidth,$thumbheight,
1.1095 raeburn 4253: $resizewidth,$resizeheight,$context,$mimetype);
1.481 raeburn 4254: } else {
1.1172.2.21 raeburn 4255: if ($env{'form.folder'}) {
4256: $fname=$env{'form.folder'}.'/'.$fname;
4257: }
1.638 albertel 4258: return &process_coursefile('uploaddoc',$docuname,$docudom,
4259: $fname,$formname,$parser,
1.1095 raeburn 4260: $allfiles,$codebase,$mimetype);
1.481 raeburn 4261: }
1.719 banghart 4262: } elsif (defined($destuname)) {
4263: my $docuname=$destuname;
4264: my $docudom=$destudom;
1.860 raeburn 4265: return &finishuserfileupload($docuname,$docudom,$formname,$fname,
4266: $parser,$allfiles,$codebase,
1.1051 raeburn 4267: $thumbwidth,$thumbheight,
1.1095 raeburn 4268: $resizewidth,$resizeheight,$context,$mimetype);
1.259 www 4269: } else {
1.638 albertel 4270: my $docuname=$env{'user.name'};
4271: my $docudom=$env{'user.domain'};
1.1172.2.23 raeburn 4272: if ((exists($env{'form.group'})) || ($context eq 'syllabus')) {
1.714 raeburn 4273: $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
4274: $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
4275: }
1.860 raeburn 4276: return &finishuserfileupload($docuname,$docudom,$formname,$fname,
4277: $parser,$allfiles,$codebase,
1.1051 raeburn 4278: $thumbwidth,$thumbheight,
1.1095 raeburn 4279: $resizewidth,$resizeheight,$context,$mimetype);
1.259 www 4280: }
1.271 www 4281: }
4282:
4283: sub finishuserfileupload {
1.860 raeburn 4284: my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
1.1095 raeburn 4285: $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
1.477 raeburn 4286: my $path=$docudom.'/'.$docuname.'/';
1.258 www 4287: my $filepath=$perlvar{'lonDocRoot'};
1.984 neumanie 4288:
1.860 raeburn 4289: my ($fnamepath,$file,$fetchthumb);
1.494 albertel 4290: $file=$fname;
4291: if ($fname=~m|/|) {
4292: ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
4293: $path.=$fnamepath.'/';
4294: }
1.259 www 4295: my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258 www 4296: my $count;
4297: for ($count=4;$count<=$#parts;$count++) {
4298: $filepath.="/$parts[$count]";
4299: if ((-e $filepath)!=1) {
4300: mkdir($filepath,0777);
4301: }
4302: }
1.984 neumanie 4303:
1.258 www 4304: # Save the file
4305: {
1.1172.2.96 raeburn 4306: if (!open(FH,'>',$filepath.'/'.$file)) {
1.701 albertel 4307: &logthis('Failed to create '.$filepath.'/'.$file);
4308: print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
4309: return '/adm/notfound.html';
4310: }
1.1090 raeburn 4311: if ($context eq 'overwrite') {
1.1117 foxr 4312: my $source = LONCAPA::tempdir().'/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
1.1090 raeburn 4313: my $target = $filepath.'/'.$file;
4314: if (-e $source) {
4315: my @info = stat($source);
4316: if ($info[9] eq $env{'form.timestamp'}) {
4317: unless (&File::Copy::move($source,$target)) {
4318: &logthis('Failed to overwrite '.$filepath.'/'.$file);
4319: return "Moving from $source failed";
4320: }
4321: } else {
4322: return "Temporary file: $source had unexpected date/time for last modification";
4323: }
4324: } else {
4325: return "Temporary file: $source missing";
4326: }
4327: } elsif (!print FH ($env{'form.'.$formname})) {
1.701 albertel 4328: &logthis('Failed to write to '.$filepath.'/'.$file);
4329: print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
4330: return '/adm/notfound.html';
4331: }
1.570 albertel 4332: close(FH);
1.1051 raeburn 4333: if ($resizewidth && $resizeheight) {
4334: my $mm = new File::MMagic;
4335: my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
4336: if ($mime_type =~ m{^image/}) {
4337: &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
4338: }
1.977 amueller 4339: }
1.258 www 4340: }
1.1152 raeburn 4341: if (($context eq 'coursedoc') || ($parser eq 'parse')) {
4342: if (ref($mimetype)) {
4343: if ($$mimetype eq '') {
4344: my $mm = new File::MMagic;
4345: my $type = $mm->checktype_filename($filepath.'/'.$file);
4346: $$mimetype = $type;
4347: }
4348: }
4349: }
1.1172.2.109 raeburn 4350: if (($context ne 'scantron') && ($parser eq 'parse')) {
1.1152 raeburn 4351: if ((ref($mimetype)) && ($$mimetype eq 'text/html')) {
1.1024 raeburn 4352: my $parse_result = &extract_embedded_items($filepath.'/'.$file,
4353: $allfiles,$codebase);
4354: unless ($parse_result eq 'ok') {
4355: &logthis('Failed to parse '.$filepath.$file.
4356: ' for embedded media: '.$parse_result);
4357: }
1.637 raeburn 4358: }
1.1172.2.109 raeburn 4359: } elsif (($context eq 'scantron') && (ref($parser) eq 'HASH')) {
4360: my $format = $env{'form.scantron_format'};
4361: &bubblesheet_converter($docudom,$filepath.'/'.$file,$parser,$format);
1.637 raeburn 4362: }
1.860 raeburn 4363: if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
4364: my $input = $filepath.'/'.$file;
4365: my $output = $filepath.'/'.'tn-'.$file;
1.1172.2.146. .13(raeb 4366:-23): my $makethumb;
1.860 raeburn 4367: my $thumbsize = $thumbwidth.'x'.$thumbheight;
1.1172.2.146. .13(raeb 4368:-23): if ($context eq 'toollogo') {
4369:-23): my ($fullwidth,$fullheight) = &check_dimensions($input);
4370:-23): if ($fullwidth ne '' && $fullheight ne '') {
4371:-23): if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
4372:-23): $makethumb = 1;
4373:-23): }
4374:-23): }
4375:-23): } else {
4376:-23): $makethumb = 1;
4377:-23): }
4378:-23): if ($makethumb) {
4379:-23): my @args = ('convert','-sample',$thumbsize,$input,$output);
4380:-23): system({$args[0]} @args);
4381:-23): if (-e $filepath.'/'.'tn-'.$file) {
4382:-23): $fetchthumb = 1;
4383:-23): }
1.860 raeburn 4384: }
4385: }
1.858 raeburn 4386:
1.259 www 4387: # Notify homeserver to grep it
4388: #
1.984 neumanie 4389: my $docuhome=&homeserver($docuname,$docudom);
1.494 albertel 4390: my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1.295 www 4391: if ($fetchresult eq 'ok') {
1.860 raeburn 4392: if ($fetchthumb) {
4393: my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
4394: if ($thumbresult ne 'ok') {
4395: &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
4396: $docuhome.': '.$thumbresult);
4397: }
4398: }
1.259 www 4399: #
1.258 www 4400: # Return the URL to it
1.494 albertel 4401: return '/uploaded/'.$path.$file;
1.263 www 4402: } else {
1.494 albertel 4403: &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
4404: ': '.$fetchresult);
1.263 www 4405: return '/adm/notfound.html';
1.858 raeburn 4406: }
1.493 albertel 4407: }
4408:
1.637 raeburn 4409: sub extract_embedded_items {
1.961 raeburn 4410: my ($fullpath,$allfiles,$codebase,$content) = @_;
1.637 raeburn 4411: my @state = ();
1.1164 raeburn 4412: my (%lastids,%related,%shockwave,%flashvars);
1.637 raeburn 4413: my %javafiles = (
4414: codebase => '',
4415: code => '',
4416: archive => ''
4417: );
4418: my %mediafiles = (
4419: src => '',
4420: movie => '',
4421: );
1.648 raeburn 4422: my $p;
4423: if ($content) {
4424: $p = HTML::LCParser->new($content);
4425: } else {
1.961 raeburn 4426: $p = HTML::LCParser->new($fullpath);
1.648 raeburn 4427: }
1.641 albertel 4428: while (my $t=$p->get_token()) {
1.640 albertel 4429: if ($t->[0] eq 'S') {
4430: my ($tagname, $attr) = ($t->[1],$t->[2]);
1.886 albertel 4431: push(@state, $tagname);
1.648 raeburn 4432: if (lc($tagname) eq 'allow') {
4433: &add_filetype($allfiles,$attr->{'src'},'src');
4434: }
1.640 albertel 4435: if (lc($tagname) eq 'img') {
4436: &add_filetype($allfiles,$attr->{'src'},'src');
4437: }
1.886 albertel 4438: if (lc($tagname) eq 'a') {
1.1172.2.24 raeburn 4439: unless (($attr->{'href'} =~ /^#/) || ($attr->{'href'} eq '')) {
4440: &add_filetype($allfiles,$attr->{'href'},'href');
4441: }
1.886 albertel 4442: }
1.645 raeburn 4443: if (lc($tagname) eq 'script') {
1.1164 raeburn 4444: my $src;
1.645 raeburn 4445: if ($attr->{'archive'} =~ /\.jar$/i) {
4446: &add_filetype($allfiles,$attr->{'archive'},'archive');
4447: } else {
1.1164 raeburn 4448: if ($attr->{'src'} ne '') {
4449: $src = $attr->{'src'};
4450: &add_filetype($allfiles,$src,'src');
4451: }
4452: }
4453: my $text = $p->get_trimmed_text();
4454: if ($text =~ /\Qswfobject.registerObject(\E([^\)]+)\)/) {
4455: my @swfargs = split(/,/,$1);
4456: foreach my $item (@swfargs) {
4457: $item =~ s/["']//g;
4458: $item =~ s/^\s+//;
4459: $item =~ s/\s+$//;
4460: }
4461: if (($swfargs[0] ne'') && ($swfargs[2] ne '')) {
4462: if (ref($related{$swfargs[0]}) eq 'ARRAY') {
4463: push(@{$related{$swfargs[0]}},$swfargs[2]);
4464: } else {
4465: $related{$swfargs[0]} = [$swfargs[2]];
4466: }
4467: }
1.645 raeburn 4468: }
4469: }
4470: if (lc($tagname) eq 'link') {
4471: if (lc($attr->{'rel'}) eq 'stylesheet') {
4472: &add_filetype($allfiles,$attr->{'href'},'href');
4473: }
4474: }
1.640 albertel 4475: if (lc($tagname) eq 'object' ||
4476: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
4477: foreach my $item (keys(%javafiles)) {
4478: $javafiles{$item} = '';
4479: }
1.1164 raeburn 4480: if ((lc($tagname) eq 'object') && (lc($state[-2]) ne 'object')) {
4481: $lastids{lc($tagname)} = $attr->{'id'};
4482: }
1.640 albertel 4483: }
4484: if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
4485: my $name = lc($attr->{'name'});
4486: foreach my $item (keys(%javafiles)) {
4487: if ($name eq $item) {
4488: $javafiles{$item} = $attr->{'value'};
4489: last;
4490: }
4491: }
1.1164 raeburn 4492: my $pathfrom;
1.640 albertel 4493: foreach my $item (keys(%mediafiles)) {
4494: if ($name eq $item) {
1.1164 raeburn 4495: $pathfrom = $attr->{'value'};
4496: $shockwave{$lastids{lc($state[-2])}} = $pathfrom;
4497: &add_filetype($allfiles,$pathfrom,$name);
1.640 albertel 4498: last;
4499: }
4500: }
1.1164 raeburn 4501: if ($name eq 'flashvars') {
4502: $flashvars{$lastids{lc($state[-2])}} = $attr->{'value'};
4503: }
4504: if ($pathfrom ne '') {
4505: &embedded_dependency($allfiles,\%related,$lastids{lc($state[-2])},
4506: $pathfrom);
4507: }
1.640 albertel 4508: }
4509: if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
4510: foreach my $item (keys(%javafiles)) {
4511: if ($attr->{$item}) {
4512: $javafiles{$item} = $attr->{$item};
4513: last;
4514: }
4515: }
4516: foreach my $item (keys(%mediafiles)) {
4517: if ($attr->{$item}) {
4518: &add_filetype($allfiles,$attr->{$item},$item);
4519: last;
4520: }
4521: }
1.1164 raeburn 4522: if (lc($tagname) eq 'embed') {
4523: if (($attr->{'name'} ne '') && ($attr->{'src'} ne '')) {
4524: &embedded_dependency($allfiles,\%related,$attr->{'name'},
4525: $attr->{'src'});
4526: }
4527: }
1.640 albertel 4528: }
1.1172.2.35 raeburn 4529: if (lc($tagname) eq 'iframe') {
4530: my $src = $attr->{'src'} ;
4531: if (($src ne '') && ($src !~ m{^(/|https?://)})) {
4532: &add_filetype($allfiles,$src,'src');
4533: } elsif ($src =~ m{^/}) {
4534: if ($env{'request.course.id'}) {
4535: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
4536: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
4537: my $url = &hreflocation('',$fullpath);
4538: if ($url =~ m{^/uploaded/$cdom/$cnum/docs/(\w+/\d+)/}) {
4539: my $relpath = $1;
4540: if ($src =~ m{^/uploaded/$cdom/$cnum/docs/\Q$relpath\E/(.+)$}) {
4541: &add_filetype($allfiles,$1,'src');
4542: }
4543: }
4544: }
4545: }
4546: }
1.1164 raeburn 4547: if ($t->[4] =~ m{/>$}) {
1.1172.2.35 raeburn 4548: pop(@state);
1.1164 raeburn 4549: }
1.640 albertel 4550: } elsif ($t->[0] eq 'E') {
4551: my ($tagname) = ($t->[1]);
4552: if ($javafiles{'codebase'} ne '') {
4553: $javafiles{'codebase'} .= '/';
4554: }
4555: if (lc($tagname) eq 'applet' ||
4556: lc($tagname) eq 'object' ||
4557: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
4558: ) {
4559: foreach my $item (keys(%javafiles)) {
4560: if ($item ne 'codebase' && $javafiles{$item} ne '') {
4561: my $file=$javafiles{'codebase'}.$javafiles{$item};
4562: &add_filetype($allfiles,$file,$item);
4563: }
4564: }
4565: }
4566: pop @state;
4567: }
4568: }
1.1164 raeburn 4569: foreach my $id (sort(keys(%flashvars))) {
4570: if ($shockwave{$id} ne '') {
4571: my @pairs = split(/\&/,$flashvars{$id});
4572: foreach my $pair (@pairs) {
4573: my ($key,$value) = split(/\=/,$pair);
4574: if ($key eq 'thumb') {
4575: &add_filetype($allfiles,$value,$key);
4576: } elsif ($key eq 'content') {
4577: my ($path) = ($shockwave{$id} =~ m{^(.+/)[^/]+$});
4578: my ($ext) = ($value =~ /\.([^.]+)$/);
4579: if ($ext ne '') {
4580: &add_filetype($allfiles,$path.$value,$ext);
4581: }
4582: }
4583: }
4584: }
4585: }
1.637 raeburn 4586: return 'ok';
4587: }
4588:
1.639 albertel 4589: sub add_filetype {
4590: my ($allfiles,$file,$type)=@_;
4591: if (exists($allfiles->{$file})) {
4592: unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
4593: push(@{$allfiles->{$file}}, &escape($type));
4594: }
4595: } else {
4596: @{$allfiles->{$file}} = (&escape($type));
1.637 raeburn 4597: }
4598: }
4599:
1.1164 raeburn 4600: sub embedded_dependency {
4601: my ($allfiles,$related,$identifier,$pathfrom) = @_;
4602: if ((ref($allfiles) eq 'HASH') && (ref($related) eq 'HASH')) {
4603: if (($identifier ne '') &&
4604: (ref($related->{$identifier}) eq 'ARRAY') &&
4605: ($pathfrom ne '')) {
4606: my ($path) = ($pathfrom =~ m{^(.+/)[^/]+$});
4607: foreach my $dep (@{$related->{$identifier}}) {
4608: &add_filetype($allfiles,$path.$dep,'object');
4609: }
4610: }
4611: }
4612: return;
4613: }
4614:
1.1172.2.146. .13(raeb 4615:-23): sub check_dimensions {
4616:-23): my ($inputfile) = @_;
4617:-23): my ($fullwidth,$fullheight);
4618:-23): if (($inputfile =~ m|^[/\w.\-]+$|) && (-e $inputfile)) {
4619:-23): my $mm = new File::MMagic;
4620:-23): my $mime_type = $mm->checktype_filename($inputfile);
4621:-23): if ($mime_type =~ m{^image/}) {
4622:-23): if (open(PIPE,"identify $inputfile 2>&1 |")) {
4623:-23): my $imageinfo = <PIPE>;
4624:-23): if (!close(PIPE)) {
4625:-23): &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
4626:-23): }
4627:-23): chomp($imageinfo);
4628:-23): my ($fullsize) =
4629:-23): ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
4630:-23): if ($fullsize) {
4631:-23): ($fullwidth,$fullheight) = split(/x/,$fullsize);
4632:-23): }
4633:-23): }
4634:-23): }
4635:-23): }
4636:-23): return ($fullwidth,$fullheight);
4637:-23): }
4638:-23):
1.1172.2.109 raeburn 4639: sub bubblesheet_converter {
4640: my ($cdom,$fullpath,$config,$format) = @_;
4641: if ((&domain($cdom) ne '') &&
4642: ($fullpath =~ m{^\Q$perlvar{'lonDocRoot'}/userfiles/$cdom/\E$match_courseid/scantron_orig}) &&
4643: (-e $fullpath) && (ref($config) eq 'HASH') && ($format ne '')) {
4644: my (%csvcols,%csvoptions);
4645: if (ref($config->{'fields'}) eq 'HASH') {
4646: %csvcols = %{$config->{'fields'}};
4647: }
4648: if (ref($config->{'options'}) eq 'HASH') {
4649: %csvoptions = %{$config->{'options'}};
4650: }
4651: my %csvbynum = reverse(%csvcols);
4652: my %scantronconf = &get_scantron_config($format,$cdom);
4653: if (keys(%scantronconf)) {
4654: my %bynum = (
4655: $scantronconf{CODEstart} => 'CODEstart',
4656: $scantronconf{IDstart} => 'IDstart',
4657: $scantronconf{PaperID} => 'PaperID',
4658: $scantronconf{FirstName} => 'FirstName',
4659: $scantronconf{LastName} => 'LastName',
4660: $scantronconf{Qstart} => 'Qstart',
4661: );
4662: my @ordered;
4663: foreach my $item (sort { $a <=> $b } keys(%bynum)) {
4664: push(@ordered,$bynum{$item});
4665: }
4666: my %mapstart = (
4667: CODEstart => 'CODE',
4668: IDstart => 'ID',
4669: PaperID => 'PaperID',
4670: FirstName => 'FirstName',
4671: LastName => 'LastName',
4672: Qstart => 'FirstQuestion',
4673: );
4674: my %maplength = (
4675: CODEstart => 'CODElength',
4676: IDstart => 'IDlength',
4677: PaperID => 'PaperIDlength',
4678: FirstName => 'FirstNamelength',
4679: LastName => 'LastNamelength',
4680: );
4681: if (open(my $fh,'<',$fullpath)) {
4682: my $output;
4683: my %lettdig = &letter_to_digits();
4684: my %diglett = reverse(%lettdig);
4685: my $numletts = scalar(keys(%lettdig));
4686: my $num = 0;
4687: while (my $line=<$fh>) {
4688: $num ++;
4689: next if (($num == 1) && ($csvoptions{'hdr'} == 1));
4690: $line =~ s{[\r\n]+$}{};
4691: my %found;
1.1172.2.143 raeburn 4692: my @values = split(/,/,$line,-1);
1.1172.2.109 raeburn 4693: my ($qstart,$record);
4694: for (my $i=0; $i<@values; $i++) {
4695: if ((($qstart ne '') && ($i > $qstart)) ||
4696: ($csvbynum{$i} eq 'FirstQuestion')) {
4697: if ($values[$i] eq '') {
4698: $values[$i] = $scantronconf{'Qoff'};
4699: } elsif ($scantronconf{'Qon'} eq 'number') {
4700: if ($values[$i] =~ /^[A-Ja-j]$/) {
4701: $values[$i] = $lettdig{uc($values[$i])};
4702: }
4703: } elsif ($scantronconf{'Qon'} eq 'letter') {
4704: if ($values[$i] =~ /^[0-9]$/) {
4705: $values[$i] = $diglett{$values[$i]};
4706: }
4707: } else {
4708: if ($values[$i] =~ /^[0-9A-Ja-j]$/) {
4709: my $digit;
4710: if ($values[$i] =~ /^[A-Ja-j]$/) {
4711: $digit = $lettdig{uc($values[$i])}-1;
4712: if ($values[$i] eq 'J') {
4713: $digit += $numletts;
4714: }
4715: } elsif ($values[$i] =~ /^[0-9]$/) {
4716: $digit = $values[$i]-1;
4717: if ($values[$i] eq '0') {
4718: $digit += $numletts;
4719: }
4720: }
4721: my $qval='';
4722: for (my $j=0; $j<$scantronconf{'Qlength'}; $j++) {
4723: if ($j == $digit) {
4724: $qval .= $scantronconf{'Qon'};
4725: } else {
4726: $qval .= $scantronconf{'Qoff'};
4727: }
4728: }
4729: $values[$i] = $qval;
4730: }
4731: }
4732: if (length($values[$i]) > $scantronconf{'Qlength'}) {
4733: $values[$i] = substr($values[$i],0,$scantronconf{'Qlength'});
4734: }
4735: my $numblank = $scantronconf{'Qlength'} - length($values[$i]);
4736: if ($numblank > 0) {
4737: $values[$i] .= ($scantronconf{'Qoff'} x $numblank);
4738: }
4739: if ($csvbynum{$i} eq 'FirstQuestion') {
4740: $qstart = $i;
4741: $found{$csvbynum{$i}} = $values[$i];
4742: } else {
4743: $found{'FirstQuestion'} .= $values[$i];
4744: }
4745: } elsif (exists($csvbynum{$i})) {
4746: if ($csvoptions{'rem'}) {
4747: $values[$i] =~ s/^\s+//;
4748: }
4749: if (($csvbynum{$i} eq 'PaperID') && ($csvoptions{'pad'})) {
4750: while (length($values[$i]) < $scantronconf{$maplength{$csvbynum{$i}}}) {
4751: $values[$i] = '0'.$values[$i];
4752: }
4753: }
4754: $found{$csvbynum{$i}} = $values[$i];
4755: }
4756: }
4757: foreach my $item (@ordered) {
4758: my $currlength = 1+length($record);
4759: my $numspaces = $scantronconf{$item} - $currlength;
4760: if ($numspaces > 0) {
4761: $record .= (' ' x $numspaces);
4762: }
4763: if (($mapstart{$item} ne '') && (exists($found{$mapstart{$item}}))) {
4764: unless ($item eq 'Qstart') {
4765: if (length($found{$mapstart{$item}}) > $scantronconf{$maplength{$item}}) {
4766: $found{$mapstart{$item}} = substr($found{$mapstart{$item}},0,$scantronconf{$maplength{$item}});
4767: }
4768: }
4769: $record .= $found{$mapstart{$item}};
4770: }
4771: }
4772: $output .= "$record\n";
4773: }
4774: close($fh);
4775: if ($output) {
4776: if (open(my $fh,'>',$fullpath)) {
4777: print $fh $output;
4778: close($fh);
4779: }
4780: }
4781: }
4782: }
4783: return;
4784: }
4785: }
4786:
4787: sub letter_to_digits {
4788: my %lettdig = (
4789: A => 1,
4790: B => 2,
4791: C => 3,
4792: D => 4,
4793: E => 5,
4794: F => 6,
4795: G => 7,
4796: H => 8,
4797: I => 9,
4798: J => 0,
4799: );
4800: return %lettdig;
4801: }
4802:
4803: sub get_scantron_config {
4804: my ($which,$cdom) = @_;
4805: my @lines = &get_scantronformat_file($cdom);
4806: my %config;
4807: #FIXME probably should move to XML it has already gotten a bit much now
4808: foreach my $line (@lines) {
4809: my ($name,$descrip)=split(/:/,$line);
4810: if ($name ne $which ) { next; }
4811: chomp($line);
4812: my @config=split(/:/,$line);
4813: $config{'name'}=$config[0];
4814: $config{'description'}=$config[1];
4815: $config{'CODElocation'}=$config[2];
4816: $config{'CODEstart'}=$config[3];
4817: $config{'CODElength'}=$config[4];
4818: $config{'IDstart'}=$config[5];
4819: $config{'IDlength'}=$config[6];
4820: $config{'Qstart'}=$config[7];
4821: $config{'Qlength'}=$config[8];
4822: $config{'Qoff'}=$config[9];
4823: $config{'Qon'}=$config[10];
4824: $config{'PaperID'}=$config[11];
4825: $config{'PaperIDlength'}=$config[12];
4826: $config{'FirstName'}=$config[13];
4827: $config{'FirstNamelength'}=$config[14];
4828: $config{'LastName'}=$config[15];
4829: $config{'LastNamelength'}=$config[16];
4830: $config{'BubblesPerRow'}=$config[17];
4831: last;
4832: }
4833: return %config;
4834: }
4835:
4836: sub get_scantronformat_file {
4837: my ($cdom) = @_;
4838: if ($cdom eq '') {
4839: $cdom= $env{'course.'.$env{'request.course.id'}.'.domain'};
4840: }
4841: my %domconfig = &get_dom('configuration',['scantron'],$cdom);
4842: my $gottab = 0;
4843: my @lines;
4844: if (ref($domconfig{'scantron'}) eq 'HASH') {
4845: if ($domconfig{'scantron'}{'scantronformat'} ne '') {
4846: my $formatfile = &getfile($perlvar{'lonDocRoot'}.$domconfig{'scantron'}{'scantronformat'});
4847: if ($formatfile ne '-1') {
4848: @lines = split("\n",$formatfile,-1);
4849: $gottab = 1;
4850: }
4851: }
4852: }
4853: if (!$gottab) {
4854: my $confname = $cdom.'-domainconfig';
4855: my $default = $perlvar{'lonDocRoot'}.'/res/'.$cdom.'/'.$confname.'/default.tab';
4856: my $formatfile = &getfile($default);
4857: if ($formatfile ne '-1') {
4858: @lines = split("\n",$formatfile,-1);
4859: $gottab = 1;
4860: }
4861: }
4862: if (!$gottab) {
4863: my @domains = ¤t_machine_domains();
4864: if (grep(/^\Q$cdom\E$/,@domains)) {
4865: if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/scantronformat.tab')) {
4866: @lines = <$fh>;
4867: close($fh);
4868: }
4869: } else {
4870: if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/default_scantronformat.tab')) {
4871: @lines = <$fh>;
4872: close($fh);
4873: }
4874: }
1.1172.2.146. .8(raebu 4875:23): chomp(@lines);
1.1172.2.109 raeburn 4876: }
4877: return @lines;
4878: }
4879:
1.493 albertel 4880: sub removeuploadedurl {
1.984 neumanie 4881: my ($url)=@_;
4882: my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);
1.613 albertel 4883: return &removeuserfile($uname,$udom,$fname);
1.490 albertel 4884: }
4885:
4886: sub removeuserfile {
4887: my ($docuname,$docudom,$fname)=@_;
1.984 neumanie 4888: my $home=&homeserver($docuname,$docudom);
1.798 raeburn 4889: my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
1.984 neumanie 4890: if ($result eq 'ok') {
1.798 raeburn 4891: if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
4892: my $metafile = $fname.'.meta';
4893: my $metaresult = &removeuserfile($docuname,$docudom,$metafile);
1.823 albertel 4894: my $url = "/uploaded/$docudom/$docuname/$fname";
1.984 neumanie 4895: my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821 raeburn 4896: my $sqlresult =
1.823 albertel 4897: &update_portfolio_table($docuname,$docudom,$file,
1.821 raeburn 4898: 'portfolio_metadata',$group,
4899: 'delete');
1.798 raeburn 4900: }
4901: }
4902: return $result;
1.257 www 4903: }
1.15 www 4904:
1.530 albertel 4905: sub mkdiruserfile {
4906: my ($docuname,$docudom,$dir)=@_;
4907: my $home=&homeserver($docuname,$docudom);
4908: return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
4909: }
4910:
1.531 albertel 4911: sub renameuserfile {
4912: my ($docuname,$docudom,$old,$new)=@_;
4913: my $home=&homeserver($docuname,$docudom);
1.798 raeburn 4914: my $result = &reply("renameuserfile:$docudom:$docuname:".
4915: &escape("$old").':'.&escape("$new"),$home);
4916: if ($result eq 'ok') {
4917: if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
4918: my $oldmeta = $old.'.meta';
4919: my $newmeta = $new.'.meta';
4920: my $metaresult =
4921: &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
1.823 albertel 4922: my $url = "/uploaded/$docudom/$docuname/$old";
4923: my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821 raeburn 4924: my $sqlresult =
1.823 albertel 4925: &update_portfolio_table($docuname,$docudom,$file,
1.821 raeburn 4926: 'portfolio_metadata',$group,
4927: 'delete');
1.798 raeburn 4928: }
4929: }
4930: return $result;
1.531 albertel 4931: }
4932:
1.14 www 4933: # ------------------------------------------------------------------------- Log
4934:
4935: sub log {
4936: my ($dom,$nam,$hom,$what)=@_;
1.47 www 4937: return critical("log:$dom:$nam:$what",$hom);
1.157 www 4938: }
4939:
4940: # ------------------------------------------------------------------ Course Log
1.352 www 4941: #
4942: # This routine flushes several buffers of non-mission-critical nature
4943: #
1.157 www 4944:
4945: sub flushcourselogs {
1.352 www 4946: &logthis('Flushing log buffers');
4947: #
4948: # course logs
4949: # This is a log of all transactions in a course, which can be used
4950: # for data mining purposes
4951: #
4952: # It also collects the courseid database, which lists last transaction
4953: # times and course titles for all courseids
4954: #
4955: my %courseidbuffer=();
1.921 raeburn 4956: foreach my $crsid (keys(%courselogs)) {
1.352 www 4957: if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1.188 www 4958: &escape($courselogs{$crsid}),
4959: $coursehombuf{$crsid}) eq 'ok') {
1.157 www 4960: delete $courselogs{$crsid};
4961: } else {
4962: &logthis('Failed to flush log buffer for '.$crsid);
4963: if (length($courselogs{$crsid})>40000) {
1.672 albertel 4964: &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
1.157 www 4965: " exceeded maximum size, deleting.</font>");
4966: delete $courselogs{$crsid};
4967: }
1.352 www 4968: }
1.920 raeburn 4969: $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
1.936 raeburn 4970: 'description' => $coursedescrbuf{$crsid},
4971: 'inst_code' => $courseinstcodebuf{$crsid},
4972: 'type' => $coursetypebuf{$crsid},
4973: 'owner' => $courseownerbuf{$crsid},
1.920 raeburn 4974: };
1.191 harris41 4975: }
1.352 www 4976: #
4977: # Write course id database (reverse lookup) to homeserver of courses
4978: # Is used in pickcourse
4979: #
1.840 albertel 4980: foreach my $crs_home (keys(%courseidbuffer)) {
1.918 raeburn 4981: my $response = &courseidput(&host_domain($crs_home),
1.921 raeburn 4982: $courseidbuffer{$crs_home},
4983: $crs_home,'timeonly');
1.352 www 4984: }
4985: #
4986: # File accesses
4987: # Writes to the dynamic metadata of resources to get hit counts, etc.
4988: #
1.449 matthew 4989: foreach my $entry (keys(%accesshash)) {
1.458 matthew 4990: if ($entry =~ /___count$/) {
4991: my ($dom,$name);
1.807 albertel 4992: ($dom,$name,undef)=
1.811 albertel 4993: ($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
1.458 matthew 4994: if (! defined($dom) || $dom eq '' ||
4995: ! defined($name) || $name eq '') {
1.620 albertel 4996: my $cid = $env{'request.course.id'};
1.1172.2.142 raeburn 4997: #
4998: # FIXME 11/29/2021
4999: # Typo in rev. 1.458 (2003/12/09)??
5000: # These should likely by $env{'course.'.$cid.'.domain'} and $env{'course.'.$cid.'.num'}
5001: #
1.1172.2.146. .13(raeb 5002:-23): # While these remain as $env{'request.'.$cid.'.domain'} and $env{'request.'.$cid.'.num'}
1.1172.2.142 raeburn 5003: # $dom and $name will always be null, so the &inc() call will default to storing this data
5004: # in a nohist_accesscount.db file for the user rather than the course.
5005: #
5006: # That said there is a lot of noise in the data being stored.
5007: # So counts for prtspool/ and adm/ etc. are recorded.
5008: #
5009: # A review of which items ending '___count' are written to %accesshash should likely be
5010: # made before deciding whether to set these to 'course.' instead of 'request.'
5011: #
5012: # Under the current scheme each user receives a nohist_accesscount.db file listing
5013: # accesses for things which are not published resources, regardless of course, and
5014: # there is not a nohist_accesscount.db file in a course, which might log accesses from
5015: # anyone in the course for things which are not published resources.
5016: #
5017: # For an author, nohist_accesscount.db ends up having records for other items
5018: # mixed up with the legitimate access counts for the author's published resources.
5019: #
1.620 albertel 5020: $dom = $env{'request.'.$cid.'.domain'};
5021: $name = $env{'request.'.$cid.'.num'};
1.458 matthew 5022: }
1.450 matthew 5023: my $value = $accesshash{$entry};
5024: my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
5025: my %temphash=($url => $value);
1.449 matthew 5026: my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
5027: if ($result eq 'ok') {
5028: delete $accesshash{$entry};
5029: }
5030: } else {
1.811 albertel 5031: my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
1.1159 www 5032: if (($dom eq 'uploaded') || ($dom eq 'adm')) { next; }
1.450 matthew 5033: my %temphash=($entry => $accesshash{$entry});
1.449 matthew 5034: if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
5035: delete $accesshash{$entry};
5036: }
1.185 www 5037: }
1.191 harris41 5038: }
1.352 www 5039: #
5040: # Roles
5041: # Reverse lookup of user roles for course faculty/staff and co-authorship
5042: #
1.800 albertel 5043: foreach my $entry (keys(%userrolehash)) {
1.351 www 5044: my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1.349 www 5045: split(/\:/,$entry);
5046: if (&Apache::lonnet::put('nohist_userroles',
1.351 www 5047: { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1.349 www 5048: $rudom,$runame) eq 'ok') {
5049: delete $userrolehash{$entry};
5050: }
5051: }
1.662 raeburn 5052: #
1.1172.2.90 raeburn 5053: # Reverse lookup of domain roles (dc, ad, li, sc, dh, da, au)
1.662 raeburn 5054: #
5055: my %domrolebuffer = ();
1.1000 raeburn 5056: foreach my $entry (keys(%domainrolehash)) {
1.901 albertel 5057: my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
1.662 raeburn 5058: if ($domrolebuffer{$rudom}) {
5059: $domrolebuffer{$rudom}.='&'.&escape($entry).
5060: '='.&escape($domainrolehash{$entry});
5061: } else {
5062: $domrolebuffer{$rudom}.=&escape($entry).
5063: '='.&escape($domainrolehash{$entry});
5064: }
5065: delete $domainrolehash{$entry};
5066: }
5067: foreach my $dom (keys(%domrolebuffer)) {
1.1172.2.82 raeburn 5068: my %servers;
5069: if (defined(&domain($dom,'primary'))) {
5070: my $primary=&domain($dom,'primary');
5071: my $hostname=&hostname($primary);
5072: $servers{$primary} = $hostname;
5073: } else {
5074: %servers = &get_servers($dom,'library');
5075: }
1.841 albertel 5076: foreach my $tryserver (keys(%servers)) {
1.1172.2.82 raeburn 5077: if (&reply('domroleput:'.$dom.':'.
5078: $domrolebuffer{$dom},$tryserver) eq 'ok') {
5079: last;
5080: } else {
1.841 albertel 5081: &logthis('Put of domain roles failed for '.$dom.' and '.$tryserver);
5082: }
1.662 raeburn 5083: }
5084: }
1.186 www 5085: $dumpcount++;
1.157 www 5086: }
5087:
5088: sub courselog {
5089: my $what=shift;
1.158 www 5090: $what=time.':'.$what;
1.620 albertel 5091: unless ($env{'request.course.id'}) { return ''; }
5092: $coursedombuf{$env{'request.course.id'}}=
5093: $env{'course.'.$env{'request.course.id'}.'.domain'};
5094: $coursenumbuf{$env{'request.course.id'}}=
5095: $env{'course.'.$env{'request.course.id'}.'.num'};
5096: $coursehombuf{$env{'request.course.id'}}=
5097: $env{'course.'.$env{'request.course.id'}.'.home'};
5098: $coursedescrbuf{$env{'request.course.id'}}=
5099: $env{'course.'.$env{'request.course.id'}.'.description'};
5100: $courseinstcodebuf{$env{'request.course.id'}}=
5101: $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
5102: $courseownerbuf{$env{'request.course.id'}}=
5103: $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
1.741 raeburn 5104: $coursetypebuf{$env{'request.course.id'}}=
5105: $env{'course.'.$env{'request.course.id'}.'.type'};
1.620 albertel 5106: if (defined $courselogs{$env{'request.course.id'}}) {
5107: $courselogs{$env{'request.course.id'}}.='&'.$what;
1.157 www 5108: } else {
1.620 albertel 5109: $courselogs{$env{'request.course.id'}}.=$what;
1.157 www 5110: }
1.620 albertel 5111: if (length($courselogs{$env{'request.course.id'}})>4048) {
1.157 www 5112: &flushcourselogs();
5113: }
1.158 www 5114: }
5115:
5116: sub courseacclog {
5117: my $fnsymb=shift;
1.620 albertel 5118: unless ($env{'request.course.id'}) { return ''; }
5119: my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
1.1144 www 5120: if ($fnsymb=~/$LONCAPA::assess_re/) {
1.187 www 5121: $what.=':POST';
1.583 matthew 5122: # FIXME: Probably ought to escape things....
1.800 albertel 5123: foreach my $key (keys(%env)) {
5124: if ($key=~/^form\.(.*)/) {
1.975 raeburn 5125: my $formitem = $1;
5126: if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
5127: $what.=':'.$formitem.'='.$env{$key};
5128: } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
1.1172.2.131 raeburn 5129: if ($formitem eq 'proctorpassword') {
1.1172.2.132 raeburn 5130: $what.=':'.$formitem.'=' . '*' x length($env{$key});
1.1172.2.131 raeburn 5131: } else {
5132: $what.=':'.$formitem.'='.$env{$key};
5133: }
1.975 raeburn 5134: }
1.158 www 5135: }
1.191 harris41 5136: }
1.583 matthew 5137: } elsif ($fnsymb =~ m:^/adm/searchcat:) {
5138: # FIXME: We should not be depending on a form parameter that someone
5139: # editing lonsearchcat.pm might change in the future.
1.620 albertel 5140: if ($env{'form.phase'} eq 'course_search') {
1.583 matthew 5141: $what.= ':POST';
5142: # FIXME: Probably ought to escape things....
5143: foreach my $element ('courseexp','crsfulltext','crsrelated',
5144: 'crsdiscuss') {
1.620 albertel 5145: $what.=':'.$element.'='.$env{'form.'.$element};
1.583 matthew 5146: }
5147: }
1.158 www 5148: }
5149: &courselog($what);
1.149 www 5150: }
5151:
1.185 www 5152: sub countacc {
5153: my $url=&declutter(shift);
1.458 matthew 5154: return if (! defined($url) || $url eq '');
1.620 albertel 5155: unless ($env{'request.course.id'}) { return ''; }
1.1158 www 5156: #
5157: # Mark that this url was used in this course
5158: #
1.620 albertel 5159: $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
1.1158 www 5160: #
5161: # Increase the access count for this resource in this child process
5162: #
1.281 www 5163: my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.450 matthew 5164: $accesshash{$key}++;
1.185 www 5165: }
1.349 www 5166:
1.361 www 5167: sub linklog {
5168: my ($from,$to)=@_;
5169: $from=&declutter($from);
5170: $to=&declutter($to);
5171: $accesshash{$from.'___'.$to.'___comefrom'}=1;
5172: $accesshash{$to.'___'.$from.'___goto'}=1;
5173: }
1.1160 www 5174:
5175: sub statslog {
5176: my ($symb,$part,$users,$av_attempts,$degdiff)=@_;
5177: if ($users<2) { return; }
5178: my %dynstore=&LONCAPA::lonmetadata::dynamic_metadata_storage({
5179: 'course' => $env{'request.course.id'},
5180: 'sections' => '"all"',
5181: 'num_students' => $users,
5182: 'part' => $part,
5183: 'symb' => $symb,
5184: 'mean_tries' => $av_attempts,
5185: 'deg_of_diff' => $degdiff});
5186: foreach my $key (keys(%dynstore)) {
5187: $accesshash{$key}=$dynstore{$key};
5188: }
5189: }
1.361 www 5190:
1.349 www 5191: sub userrolelog {
5192: my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
1.1169 droeschl 5193: if ( $trole =~ /^(ca|aa|in|cc|ep|cr|ta|co)/ ) {
1.350 www 5194: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
5195: $userrolehash
5196: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1.349 www 5197: =$tend.':'.$tstart;
1.662 raeburn 5198: }
1.1169 droeschl 5199: if ($env{'request.role'} =~ /dc\./ && $trole =~ /^(au|in|cc|ep|cr|ta|co)/) {
1.898 albertel 5200: $userrolehash
5201: {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
5202: =$tend.':'.$tstart;
5203: }
1.1172.2.90 raeburn 5204: if ($trole =~ /^(dc|ad|li|au|dg|sc|dh|da)/ ) {
1.662 raeburn 5205: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
5206: $domainrolehash
5207: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
5208: = $tend.':'.$tstart;
5209: }
1.351 www 5210: }
5211:
1.957 raeburn 5212: sub courserolelog {
5213: my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
1.1172.2.9 raeburn 5214: if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
5215: my $cdom = $1;
5216: my $cnum = $2;
5217: my $sec = $3;
5218: my $namespace = 'rolelog';
5219: my %storehash = (
5220: role => $trole,
5221: start => $tstart,
5222: end => $tend,
5223: selfenroll => $selfenroll,
5224: context => $context,
5225: );
5226: if ($trole eq 'gr') {
5227: $namespace = 'groupslog';
5228: $storehash{'group'} = $sec;
5229: } else {
5230: $storehash{'section'} = $sec;
1.1172.2.146. .16(raeb 5231:-23): my ($curruserdomstr,$newuserdomstr);
5232:-23): if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.userdomains'})) {
5233:-23): $curruserdomstr = $env{'course.'.$env{'request.course.id'}.'.internal.userdomains'};
5234:-23): } else {
5235:-23): my %courseinfo = &coursedescription($cdom.'/'.$cnum);
5236:-23): $curruserdomstr = $courseinfo{'internal.userdomains'};
5237:-23): }
5238:-23): if ($curruserdomstr ne '') {
5239:-23): my @udoms = split(/,/,$curruserdomstr);
5240:-23): unless (grep(/^\Q$domain\E/,@udoms)) {
5241:-23): push(@udoms,$domain);
5242:-23): $newuserdomstr = join(',',sort(@udoms));
5243:-23): }
5244:-23): } else {
5245:-23): $newuserdomstr = $domain;
5246:-23): }
5247:-23): if ($newuserdomstr ne '') {
5248:-23): my $putresult = &put('environment',{ 'internal.userdomains' => $newuserdomstr },
5249:-23): $cdom,$cnum);
5250:-23): if ($putresult eq 'ok') {
5251:-23): unless (($selfenroll) || ($context eq 'selfenroll')) {
5252:-23): if (($context eq 'createcourse') || ($context eq 'requestcourses') ||
5253:-23): ($context eq 'automated') || ($context eq 'domain')) {
5254:-23): $env{'course.'.$cdom.'_'.$cnum.'.internal.userdomains'} = $newuserdomstr;
5255:-23): } elsif ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
5256:-23): &appenv({'course.'.$cdom.'_'.$cnum.'.internal.userdomains' => $newuserdomstr});
5257:-23): }
5258:-23): }
5259:-23): }
5260:-23): }
1.1172.2.9 raeburn 5261: }
5262: &write_log('course',$namespace,\%storehash,$delflag,$username,
5263: $domain,$cnum,$cdom);
5264: if (($trole ne 'st') || ($sec ne '')) {
5265: &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
1.957 raeburn 5266: }
5267: }
5268: return;
5269: }
5270:
1.1172.2.9 raeburn 5271: sub domainrolelog {
5272: my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
5273: if ($area =~ m{^/($match_domain)/$}) {
5274: my $cdom = $1;
5275: my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
5276: my $namespace = 'rolelog';
5277: my %storehash = (
5278: role => $trole,
5279: start => $tstart,
5280: end => $tend,
5281: context => $context,
5282: );
5283: &write_log('domain',$namespace,\%storehash,$delflag,$username,
5284: $domain,$domconfiguser,$cdom);
5285: }
5286: return;
5287:
5288: }
5289:
5290: sub coauthorrolelog {
5291: my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
5292: if ($area =~ m{^/($match_domain)/($match_username)$}) {
5293: my $audom = $1;
5294: my $auname = $2;
5295: my $namespace = 'rolelog';
5296: my %storehash = (
5297: role => $trole,
5298: start => $tstart,
5299: end => $tend,
5300: context => $context,
5301: );
5302: &write_log('author',$namespace,\%storehash,$delflag,$username,
5303: $domain,$auname,$audom);
5304: }
5305: return;
5306: }
5307:
1.1172.2.146. .23(raeb 5308:-24): sub authorarchivelog {
5309:-24): my ($hashref,$size,$filesdest,$action) = @_;
5310:-24): my $lonprtdir = $Apache::lonnet::perlvar{'lonPrtDir'};
5311:-24): my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
5312:-24): $filesdest =~ s{^\Q$lonprtdir/\E}{};
5313:-24): if ($filesdest =~ m{^($match_username)_($match_domain)_archive_(\d+_\d+_\d+(|[.\w]+))$}) {
5314:-24): my ($auname,$audom,$id) = ($1,$2,$3);
5315:-24): if (ref($hashref) eq 'HASH') {
5316:-24): my $namespace = 'archivelog';
5317:-24): my $dir;
5318:-24): if ($hashref->{dir} =~ m{^\Q$londocroot/priv/$audom/$auname\E(.*)$}) {
5319:-24): $dir = $1;
5320:-24): }
5321:-24): my $delflag = 0;
5322:-24): my %storehash = (
5323:-24): id => $id,
5324:-24): dir => $dir,
5325:-24): files => $hashref->{numfiles},
5326:-24): subdirs => $hashref->{numdirs},
5327:-24): bytes => $hashref->{bytes},
5328:-24): size => $size,
5329:-24): action => $action,
5330:-24): );
5331:-24): if ($action eq 'delete') {
5332:-24): $delflag = 1;
5333:-24): }
5334:-24): &write_log('author',$namespace,\%storehash,$delflag,$auname,
5335:-24): $audom,$auname,$audom);
5336:-24): }
5337:-24): }
5338:-24): return;
5339:-24): }
5340:-24):
1.351 www 5341: sub get_course_adv_roles {
1.948 raeburn 5342: my ($cid,$codes) = @_;
1.620 albertel 5343: $cid=$env{'request.course.id'} unless (defined($cid));
1.351 www 5344: my %coursehash=&coursedescription($cid);
1.988 raeburn 5345: my $crstype = &Apache::loncommon::course_type($cid);
1.470 www 5346: my %nothide=();
1.800 albertel 5347: foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
1.937 raeburn 5348: if ($user !~ /:/) {
5349: $nothide{join(':',split(/[\@]/,$user))}=1;
5350: } else {
5351: $nothide{$user}=1;
5352: }
1.470 www 5353: }
1.1172.2.23 raeburn 5354: my @possdoms = ($coursehash{'domain'});
5355: if ($coursehash{'checkforpriv'}) {
5356: push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
5357: }
1.351 www 5358: my %returnhash=();
5359: my %dumphash=
5360: &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
5361: my $now=time;
1.997 raeburn 5362: my %privileged;
1.1000 raeburn 5363: foreach my $entry (keys(%dumphash)) {
1.800 albertel 5364: my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
1.351 www 5365: if (($tstart) && ($tstart<0)) { next; }
5366: if (($tend) && ($tend<$now)) { next; }
5367: if (($tstart) && ($now<$tstart)) { next; }
1.800 albertel 5368: my ($role,$username,$domain,$section)=split(/\:/,$entry);
1.576 albertel 5369: if ($username eq '' || $domain eq '') { next; }
1.1172.2.23 raeburn 5370: if ((&privileged($username,$domain,\@possdoms)) &&
1.997 raeburn 5371: (!$nothide{$username.':'.$domain})) { next; }
1.656 albertel 5372: if ($role eq 'cr') { next; }
1.948 raeburn 5373: if ($codes) {
5374: if ($section) { $role .= ':'.$section; }
5375: if ($returnhash{$role}) {
5376: $returnhash{$role}.=','.$username.':'.$domain;
5377: } else {
5378: $returnhash{$role}=$username.':'.$domain;
5379: }
1.351 www 5380: } else {
1.988 raeburn 5381: my $key=&plaintext($role,$crstype);
1.973 bisitz 5382: if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
1.948 raeburn 5383: if ($returnhash{$key}) {
5384: $returnhash{$key}.=','.$username.':'.$domain;
5385: } else {
5386: $returnhash{$key}=$username.':'.$domain;
5387: }
1.351 www 5388: }
1.948 raeburn 5389: }
1.400 www 5390: return %returnhash;
5391: }
5392:
5393: sub get_my_roles {
1.937 raeburn 5394: my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
1.620 albertel 5395: unless (defined($uname)) { $uname=$env{'user.name'}; }
5396: unless (defined($udom)) { $udom=$env{'user.domain'}; }
1.937 raeburn 5397: my (%dumphash,%nothide);
1.1086 raeburn 5398: if ($context eq 'userroles') {
1.1166 raeburn 5399: %dumphash = &dump('roles',$udom,$uname);
1.858 raeburn 5400: } else {
1.1172.2.23 raeburn 5401: %dumphash = &dump('nohist_userroles',$udom,$uname);
1.937 raeburn 5402: if ($hidepriv) {
5403: my %coursehash=&coursedescription($udom.'_'.$uname);
5404: foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
5405: if ($user !~ /:/) {
5406: $nothide{join(':',split(/[\@]/,$user))} = 1;
5407: } else {
5408: $nothide{$user} = 1;
5409: }
5410: }
5411: }
1.858 raeburn 5412: }
1.400 www 5413: my %returnhash=();
5414: my $now=time;
1.999 raeburn 5415: my %privileged;
1.800 albertel 5416: foreach my $entry (keys(%dumphash)) {
1.867 raeburn 5417: my ($role,$tend,$tstart);
5418: if ($context eq 'userroles') {
1.1149 raeburn 5419: next if ($entry =~ /^rolesdef/);
1.867 raeburn 5420: ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
5421: } else {
5422: ($tend,$tstart)=split(/\:/,$dumphash{$entry});
5423: }
1.400 www 5424: if (($tstart) && ($tstart<0)) { next; }
1.832 raeburn 5425: my $status = 'active';
1.939 raeburn 5426: if (($tend) && ($tend<=$now)) {
1.832 raeburn 5427: $status = 'previous';
5428: }
5429: if (($tstart) && ($now<$tstart)) {
5430: $status = 'future';
5431: }
5432: if (ref($types) eq 'ARRAY') {
5433: if (!grep(/^\Q$status\E$/,@{$types})) {
5434: next;
5435: }
5436: } else {
5437: if ($status ne 'active') {
5438: next;
5439: }
5440: }
1.867 raeburn 5441: my ($rolecode,$username,$domain,$section,$area);
5442: if ($context eq 'userroles') {
1.1172.2.9 raeburn 5443: ($area,$rolecode) = ($entry =~ /^(.+)_([^_]+)$/);
1.867 raeburn 5444: (undef,$domain,$username,$section) = split(/\//,$area);
5445: } else {
5446: ($role,$username,$domain,$section) = split(/\:/,$entry);
5447: }
1.832 raeburn 5448: if (ref($roledoms) eq 'ARRAY') {
5449: if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
5450: next;
5451: }
5452: }
5453: if (ref($roles) eq 'ARRAY') {
5454: if (!grep(/^\Q$role\E$/,@{$roles})) {
1.922 raeburn 5455: if ($role =~ /^cr\//) {
5456: if (!grep(/^cr$/,@{$roles})) {
5457: next;
5458: }
1.1104 raeburn 5459: } elsif ($role =~ /^gr\//) {
5460: if (!grep(/^gr$/,@{$roles})) {
5461: next;
5462: }
1.922 raeburn 5463: } else {
5464: next;
5465: }
1.832 raeburn 5466: }
1.867 raeburn 5467: }
1.937 raeburn 5468: if ($hidepriv) {
1.1172.2.23 raeburn 5469: my @privroles = ('dc','su');
1.999 raeburn 5470: if ($context eq 'userroles') {
1.1172.2.23 raeburn 5471: next if (grep(/^\Q$role\E$/,@privroles));
1.999 raeburn 5472: } else {
1.1172.2.23 raeburn 5473: my $possdoms = [$domain];
5474: if (ref($roledoms) eq 'ARRAY') {
5475: push(@{$possdoms},@{$roledoms});
1.999 raeburn 5476: }
1.1172.2.23 raeburn 5477: if (&privileged($username,$domain,$possdoms,\@privroles)) {
1.999 raeburn 5478: if (!$nothide{$username.':'.$domain}) {
5479: next;
5480: }
5481: }
1.937 raeburn 5482: }
5483: }
1.933 raeburn 5484: if ($withsec) {
5485: $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
5486: $tstart.':'.$tend;
5487: } else {
5488: $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
5489: }
1.832 raeburn 5490: }
1.373 www 5491: return %returnhash;
1.399 www 5492: }
5493:
1.1172.2.89 raeburn 5494: sub get_all_adhocroles {
5495: my ($dom) = @_;
5496: my @roles_by_num = ();
5497: my %domdefaults = &get_domain_defaults($dom);
5498: my (%description,%access_in_dom,%access_info);
5499: if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
5500: my $count = 0;
5501: my %domcurrent = %{$domdefaults{'adhocroles'}};
5502: my %ordered;
5503: foreach my $role (sort(keys(%domcurrent))) {
5504: my ($order,$desc,$access_in_dom);
5505: if (ref($domcurrent{$role}) eq 'HASH') {
5506: $order = $domcurrent{$role}{'order'};
5507: $desc = $domcurrent{$role}{'desc'};
5508: $access_in_dom{$role} = $domcurrent{$role}{'access'};
5509: $access_info{$role} = $domcurrent{$role}{$access_in_dom{$role}};
5510: }
5511: if ($order eq '') {
5512: $order = $count;
5513: }
5514: $ordered{$order} = $role;
5515: if ($desc ne '') {
5516: $description{$role} = $desc;
5517: } else {
5518: $description{$role}= $role;
5519: }
5520: $count++;
5521: }
5522: foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
5523: push(@roles_by_num,$ordered{$item});
5524: }
5525: }
5526: return (\@roles_by_num,\%description,\%access_in_dom,\%access_info);
5527: }
5528:
5529: sub get_my_adhocroles {
5530: my ($cid,$checkreg) = @_;
5531: my ($cdom,$cnum,%info,@possroles,$description,$roles_by_num);
5532: if ($env{'request.course.id'} eq $cid) {
5533: $cdom = $env{'course.'.$cid.'.domain'};
5534: $cnum = $env{'course.'.$cid.'.num'};
5535: $info{'internal.coursecode'} = $env{'course.'.$cid.'.internal.coursecode'};
5536: } elsif ($cid =~ /^($match_domain)_($match_courseid)$/) {
5537: $cdom = $1;
5538: $cnum = $2;
5539: %info = &Apache::lonnet::get('environment',['internal.coursecode'],
5540: $cdom,$cnum);
5541: }
5542: if (($info{'internal.coursecode'} ne '') && ($checkreg)) {
5543: my $user = $env{'user.name'}.':'.$env{'user.domain'};
5544: my %rosterhash = &get('classlist',[$user],$cdom,$cnum);
5545: if ($rosterhash{$user} ne '') {
5546: my $type = (split(/:/,$rosterhash{$user}))[5];
5547: return ([],{}) if ($type eq 'auto');
5548: }
5549: }
5550: if (($cdom ne '') && ($cnum ne '')) {
1.1172.2.90 raeburn 5551: if (($env{"user.role.dh./$cdom/"}) || ($env{"user.role.da./$cdom/"})) {
1.1172.2.89 raeburn 5552: my $then=$env{'user.login.time'};
5553: my $update=$env{'user.update.time'};
1.1172.2.90 raeburn 5554: if (!$update) {
5555: $update = $then;
5556: }
5557: my @liveroles;
5558: foreach my $role ('dh','da') {
5559: if ($env{"user.role.$role./$cdom/"}) {
5560: my ($tstart,$tend)=split(/\./,$env{"user.role.$role./$cdom/"});
5561: my $limit = $update;
5562: if ($env{'request.role'} eq "$role./$cdom/") {
5563: $limit = $then;
5564: }
5565: my $activerole = 1;
5566: if ($tstart && $tstart>$limit) { $activerole = 0; }
5567: if ($tend && $tend <$limit) { $activerole = 0; }
5568: if ($activerole) {
5569: push(@liveroles,$role);
5570: }
5571: }
5572: }
5573: if (@liveroles) {
1.1172.2.89 raeburn 5574: if (&homeserver($cnum,$cdom) ne 'no_host') {
5575: my ($accessref,$accessinfo,%access_in_dom);
5576: ($roles_by_num,$description,$accessref,$accessinfo) = &get_all_adhocroles($cdom);
5577: if (ref($roles_by_num) eq 'ARRAY') {
5578: if (@{$roles_by_num}) {
5579: my %settings;
5580: if ($env{'request.course.id'} eq $cid) {
5581: foreach my $envkey (keys(%env)) {
5582: if ($envkey =~ /^\Qcourse.$cid.\E(internal\.adhoc.+)$/) {
5583: $settings{$1} = $env{$envkey};
5584: }
5585: }
5586: } else {
5587: %settings = &dump('environment',$cdom,$cnum,'internal\.adhoc');
5588: }
5589: my %setincrs;
5590: if ($settings{'internal.adhocaccess'}) {
5591: map { $setincrs{$_} = 1; } split(/,/,$settings{'internal.adhocaccess'});
5592: }
5593: my @statuses;
5594: if ($env{'environment.inststatus'}) {
5595: @statuses = split(/,/,$env{'environment.inststatus'});
5596: }
5597: my $user = $env{'user.name'}.':'.$env{'user.domain'};
5598: if (ref($accessref) eq 'HASH') {
5599: %access_in_dom = %{$accessref};
5600: }
5601: foreach my $role (@{$roles_by_num}) {
5602: my ($curraccess,@okstatus,@personnel);
5603: if ($setincrs{$role}) {
5604: ($curraccess,my $rest) = split(/=/,$settings{'internal.adhoc.'.$role});
5605: if ($curraccess eq 'status') {
5606: @okstatus = split(/\&/,$rest);
5607: } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
5608: @personnel = split(/\&/,$rest);
5609: }
5610: } else {
5611: $curraccess = $access_in_dom{$role};
5612: if (ref($accessinfo) eq 'HASH') {
5613: if ($curraccess eq 'status') {
5614: if (ref($accessinfo->{$role}) eq 'ARRAY') {
5615: @okstatus = @{$accessinfo->{$role}};
5616: }
5617: } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
5618: if (ref($accessinfo->{$role}) eq 'ARRAY') {
5619: @personnel = @{$accessinfo->{$role}};
5620: }
5621: }
5622: }
5623: }
5624: if ($curraccess eq 'none') {
5625: next;
5626: } elsif ($curraccess eq 'all') {
5627: push(@possroles,$role);
1.1172.2.90 raeburn 5628: } elsif ($curraccess eq 'dh') {
5629: if (grep(/^dh$/,@liveroles)) {
5630: push(@possroles,$role);
5631: } else {
5632: next;
5633: }
5634: } elsif ($curraccess eq 'da') {
5635: if (grep(/^da$/,@liveroles)) {
5636: push(@possroles,$role);
5637: } else {
5638: next;
5639: }
1.1172.2.89 raeburn 5640: } elsif ($curraccess eq 'status') {
5641: if (@okstatus) {
5642: if (!@statuses) {
5643: if (grep(/^default$/,@okstatus)) {
5644: push(@possroles,$role);
5645: }
5646: } else {
5647: foreach my $status (@okstatus) {
5648: if (grep(/^\Q$status\E$/,@statuses)) {
5649: push(@possroles,$role);
5650: last;
5651: }
5652: }
5653: }
5654: }
5655: } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
5656: if (grep(/^\Q$user\E$/,@personnel)) {
5657: if ($curraccess eq 'exc') {
5658: push(@possroles,$role);
5659: }
5660: } elsif ($curraccess eq 'inc') {
5661: push(@possroles,$role);
5662: }
5663: }
5664: }
5665: }
5666: }
5667: }
5668: }
5669: }
5670: }
5671: unless (ref($description) eq 'HASH') {
5672: if (ref($roles_by_num) eq 'ARRAY') {
5673: my %desc;
5674: map { $desc{$_} = $_; } (@{$roles_by_num});
5675: $description = \%desc;
5676: } else {
5677: $description = {};
5678: }
5679: }
5680: return (\@possroles,$description);
5681: }
5682:
1.399 www 5683: # ----------------------------------------------------- Frontpage Announcements
5684: #
5685: #
5686:
5687: sub postannounce {
5688: my ($server,$text)=@_;
1.844 albertel 5689: unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
1.399 www 5690: unless ($text=~/\w/) { $text=''; }
5691: return &reply('setannounce:'.&escape($text),$server);
5692: }
5693:
5694: sub getannounce {
1.448 albertel 5695:
1.1172.2.96 raeburn 5696: if (open(my $fh,"<",$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1.399 www 5697: my $announcement='';
1.800 albertel 5698: while (my $line = <$fh>) { $announcement .= $line; }
1.448 albertel 5699: close($fh);
1.399 www 5700: if ($announcement=~/\w/) {
5701: return
5702: '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1.518 albertel 5703: '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>';
1.399 www 5704: } else {
5705: return '';
5706: }
5707: } else {
5708: return '';
5709: }
1.351 www 5710: }
1.353 www 5711:
5712: # ---------------------------------------------------------- Course ID routines
5713: # Deal with domain's nohist_courseid.db files
5714: #
5715:
5716: sub courseidput {
1.921 raeburn 5717: my ($domain,$storehash,$coursehome,$caller) = @_;
1.1054 raeburn 5718: return unless (ref($storehash) eq 'HASH');
1.921 raeburn 5719: my $outcome;
5720: if ($caller eq 'timeonly') {
5721: my $cids = '';
5722: foreach my $item (keys(%$storehash)) {
5723: $cids.=&escape($item).'&';
5724: }
5725: $cids=~s/\&$//;
5726: $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
5727: $coursehome);
5728: } else {
5729: my $items = '';
5730: foreach my $item (keys(%$storehash)) {
5731: $items.= &escape($item).'='.
5732: &freeze_escape($$storehash{$item}).'&';
5733: }
5734: $items=~s/\&$//;
5735: $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
5736: $coursehome);
1.918 raeburn 5737: }
5738: if ($outcome eq 'unknown_cmd') {
5739: my $what;
5740: foreach my $cid (keys(%$storehash)) {
5741: $what .= &escape($cid).'=';
1.921 raeburn 5742: foreach my $item ('description','inst_code','owner','type') {
1.936 raeburn 5743: $what .= &escape($storehash->{$cid}{$item}).':';
1.918 raeburn 5744: }
5745: $what =~ s/\:$/&/;
5746: }
5747: $what =~ s/\&$//;
5748: return &reply('courseidput:'.$domain.':'.$what,$coursehome);
5749: } else {
5750: return $outcome;
5751: }
1.353 www 5752: }
5753:
5754: sub courseiddump {
1.921 raeburn 5755: my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
1.947 raeburn 5756: $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
1.1029 raeburn 5757: $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
1.1172.2.38 raeburn 5758: $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner,
1.1172.2.68 raeburn 5759: $hasuniquecode,$reqcrsdom,$reqinstcode)=@_;
1.918 raeburn 5760: my $as_hash = 1;
5761: my %returnhash;
5762: if (!$domfilter) { $domfilter=''; }
1.845 albertel 5763: my %libserv = &all_library();
5764: foreach my $tryserver (keys(%libserv)) {
5765: if ( ( $hostidflag == 1
5766: && grep(/^\Q$tryserver\E$/,@{$hostidref}) )
5767: || (!defined($hostidflag)) ) {
5768:
1.918 raeburn 5769: if (($domfilter eq '') ||
5770: (&host_domain($tryserver) eq $domfilter)) {
1.1172.2.22 raeburn 5771: my $rep;
5772: if (grep { $_ eq $tryserver } ¤t_machine_ids()) {
5773: $rep = &LONCAPA::Lond::dump_course_id_handler(
5774: join(":", (&host_domain($tryserver), $sincefilter,
5775: &escape($descfilter), &escape($instcodefilter),
5776: &escape($ownerfilter), &escape($coursefilter),
5777: &escape($typefilter), &escape($regexp_ok),
5778: $as_hash, &escape($selfenrollonly),
5779: &escape($catfilter), $showhidden, $caller,
5780: &escape($cloner), &escape($cc_clone), $cloneonly,
5781: &escape($createdbefore), &escape($createdafter),
1.1172.2.68 raeburn 5782: &escape($creationcontext),$domcloner,$hasuniquecode,
5783: $reqcrsdom,&escape($reqinstcode))));
1.1172.2.22 raeburn 5784: } else {
5785: $rep = &reply('courseiddump:'.&host_domain($tryserver).':'.
5786: $sincefilter.':'.&escape($descfilter).':'.
5787: &escape($instcodefilter).':'.&escape($ownerfilter).
5788: ':'.&escape($coursefilter).':'.&escape($typefilter).
5789: ':'.&escape($regexp_ok).':'.$as_hash.':'.
5790: &escape($selfenrollonly).':'.&escape($catfilter).':'.
5791: $showhidden.':'.$caller.':'.&escape($cloner).':'.
5792: &escape($cc_clone).':'.$cloneonly.':'.
5793: &escape($createdbefore).':'.&escape($createdafter).':'.
1.1172.2.68 raeburn 5794: &escape($creationcontext).':'.$domcloner.':'.$hasuniquecode.
5795: ':'.$reqcrsdom.':'.&escape($reqinstcode),$tryserver);
1.1172.2.22 raeburn 5796: }
5797:
1.918 raeburn 5798: my @pairs=split(/\&/,$rep);
5799: foreach my $item (@pairs) {
5800: my ($key,$value)=split(/\=/,$item,2);
5801: $key = &unescape($key);
5802: next if ($key =~ /^error: 2 /);
5803: my $result = &thaw_unescape($value);
5804: if (ref($result) eq 'HASH') {
5805: $returnhash{$key}=$result;
5806: } else {
1.921 raeburn 5807: my @responses = split(/:/,$value);
5808: my @items = ('description','inst_code','owner','type');
1.918 raeburn 5809: for (my $i=0; $i<@responses; $i++) {
1.921 raeburn 5810: $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
1.918 raeburn 5811: }
1.1008 raeburn 5812: }
1.353 www 5813: }
5814: }
5815: }
5816: }
5817: return %returnhash;
5818: }
5819:
1.1055 raeburn 5820: sub courselastaccess {
5821: my ($cdom,$cnum,$hostidref) = @_;
5822: my %returnhash;
5823: if ($cdom && $cnum) {
5824: my $chome = &homeserver($cnum,$cdom);
5825: if ($chome ne 'no_host') {
5826: my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
5827: &extract_lastaccess(\%returnhash,$rep);
5828: }
5829: } else {
5830: if (!$cdom) { $cdom=''; }
5831: my %libserv = &all_library();
5832: foreach my $tryserver (keys(%libserv)) {
5833: if (ref($hostidref) eq 'ARRAY') {
5834: next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
5835: }
5836: if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
5837: my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
5838: &extract_lastaccess(\%returnhash,$rep);
5839: }
5840: }
5841: }
5842: return %returnhash;
5843: }
5844:
5845: sub extract_lastaccess {
5846: my ($returnhash,$rep) = @_;
5847: if (ref($returnhash) eq 'HASH') {
5848: unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' ||
5849: $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
5850: $rep eq '') {
5851: my @pairs=split(/\&/,$rep);
5852: foreach my $item (@pairs) {
5853: my ($key,$value)=split(/\=/,$item,2);
5854: $key = &unescape($key);
5855: next if ($key =~ /^error: 2 /);
5856: $returnhash->{$key} = &thaw_unescape($value);
5857: }
5858: }
5859: }
5860: return;
5861: }
5862:
1.658 raeburn 5863: # ---------------------------------------------------------- DC e-mail
1.662 raeburn 5864:
5865: sub dcmailput {
1.685 raeburn 5866: my ($domain,$msgid,$message,$server)=@_;
1.662 raeburn 5867: my $status = &Apache::lonnet::critical(
1.740 www 5868: 'dcmailput:'.$domain.':'.&escape($msgid).'='.
5869: &escape($message),$server);
1.662 raeburn 5870: return $status;
5871: }
5872:
1.658 raeburn 5873: sub dcmaildump {
5874: my ($dom,$startdate,$enddate,$senders) = @_;
1.685 raeburn 5875: my %returnhash=();
1.846 albertel 5876:
5877: if (defined(&domain($dom,'primary'))) {
1.685 raeburn 5878: my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
5879: &escape($enddate).':';
5880: my @esc_senders=map { &escape($_)} @$senders;
5881: $cmd.=&escape(join('&',@esc_senders));
1.846 albertel 5882: foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
1.800 albertel 5883: my ($key,$value) = split(/\=/,$line,2);
1.685 raeburn 5884: if (($key) && ($value)) {
5885: $returnhash{&unescape($key)} = &unescape($value);
1.658 raeburn 5886: }
5887: }
5888: }
5889: return %returnhash;
5890: }
1.662 raeburn 5891: # ---------------------------------------------------------- Domain roles
5892:
5893: sub get_domain_roles {
5894: my ($dom,$roles,$startdate,$enddate)=@_;
1.1018 raeburn 5895: if ((!defined($startdate)) || ($startdate eq '')) {
1.662 raeburn 5896: $startdate = '.';
5897: }
1.1018 raeburn 5898: if ((!defined($enddate)) || ($enddate eq '')) {
1.662 raeburn 5899: $enddate = '.';
5900: }
1.922 raeburn 5901: my $rolelist;
5902: if (ref($roles) eq 'ARRAY') {
1.1172.2.23 raeburn 5903: $rolelist = join('&',@{$roles});
1.922 raeburn 5904: }
1.662 raeburn 5905: my %personnel = ();
1.841 albertel 5906:
5907: my %servers = &get_servers($dom,'library');
5908: foreach my $tryserver (keys(%servers)) {
5909: %{$personnel{$tryserver}}=();
5910: foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
5911: &escape($startdate).':'.
5912: &escape($enddate).':'.
5913: &escape($rolelist), $tryserver))) {
5914: my ($key,$value) = split(/\=/,$line,2);
5915: if (($key) && ($value)) {
5916: $personnel{$tryserver}{&unescape($key)} = &unescape($value);
5917: }
5918: }
1.662 raeburn 5919: }
5920: return %personnel;
5921: }
1.658 raeburn 5922:
1.1172.2.89 raeburn 5923: sub get_active_domroles {
5924: my ($dom,$roles) = @_;
5925: return () unless (ref($roles) eq 'ARRAY');
5926: my $now = time;
5927: my %dompersonnel = &get_domain_roles($dom,$roles,$now,$now);
5928: my %domroles;
5929: foreach my $server (keys(%dompersonnel)) {
5930: foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
5931: my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
5932: $domroles{$uname.':'.$udom} = $dompersonnel{$server}{$user};
5933: }
5934: }
5935: return %domroles;
5936: }
5937:
1.1057 www 5938: # ----------------------------------------------------------- Interval timing
1.149 www 5939:
1.1153 www 5940: {
5941: # Caches needed for speedup of navmaps
5942: # We don't want to cache this for very long at all (5 seconds at most)
5943: #
5944: # The user for whom we cache
5945: my $cachedkey='';
5946: # The cached times for this user
5947: my %cachedtimes=();
5948: # When this was last done
1.1172.2.66 raeburn 5949: my $cachedtime='';
1.1153 www 5950:
5951: sub load_all_first_access {
1.1172.2.146. .1(raebu 5952:22): my ($uname,$udom,$ignorecache)=@_;
1.1156 www 5953: if (($cachedkey eq $uname.':'.$udom) &&
1.1172.2.146. .1(raebu 5954:22): (abs($cachedtime-time)<5) && (!$env{'form.markaccess'}) &&
5955:22): (!$ignorecache)) {
1.1154 raeburn 5956: return;
5957: }
5958: $cachedtime=time;
5959: $cachedkey=$uname.':'.$udom;
5960: %cachedtimes=&dump('firstaccesstimes',$udom,$uname);
1.1153 www 5961: }
5962:
1.504 albertel 5963: sub get_first_access {
1.1172.2.146. .1(raebu 5964:22): my ($type,$argsymb,$argmap,$ignorecache)=@_;
1.790 albertel 5965: my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504 albertel 5966: if ($argsymb) { $symb=$argsymb; }
5967: my ($map,$id,$res)=&decode_symb($symb);
1.1162 raeburn 5968: if ($argmap) { $map = $argmap; }
1.926 albertel 5969: if ($type eq 'course') {
5970: $res='course';
5971: } elsif ($type eq 'map') {
1.588 albertel 5972: $res=&symbread($map);
5973: } else {
5974: $res=$symb;
5975: }
1.1172.2.146. .1(raebu 5976:22): &load_all_first_access($uname,$udom,$ignorecache);
1.1153 www 5977: return $cachedtimes{"$courseid\0$res"};
1.504 albertel 5978: }
5979:
5980: sub set_first_access {
1.1162 raeburn 5981: my ($type,$interval)=@_;
1.790 albertel 5982: my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504 albertel 5983: my ($map,$id,$res)=&decode_symb($symb);
1.928 albertel 5984: if ($type eq 'course') {
5985: $res='course';
5986: } elsif ($type eq 'map') {
1.588 albertel 5987: $res=&symbread($map);
5988: } else {
5989: $res=$symb;
5990: }
1.1153 www 5991: $cachedkey='';
1.1162 raeburn 5992: my $firstaccess=&get_first_access($type,$symb,$map);
1.1172.2.102 raeburn 5993: if ($firstaccess) {
5994: &logthis("First access time already set ($firstaccess) when attempting ".
5995: "to set new value (type: $type, extent: $res) for $uname:$udom ".
5996: "in $courseid");
5997: return 'already_set';
5998: } else {
1.1162 raeburn 5999: my $start = time;
6000: my $putres = &put('firstaccesstimes',{"$courseid\0$res"=>$start},
6001: $udom,$uname);
6002: if ($putres eq 'ok') {
6003: &put('timerinterval',{"$courseid\0$res"=>$interval},
6004: $udom,$uname);
6005: &appenv(
6006: {
6007: 'course.'.$courseid.'.firstaccess.'.$res => $start,
6008: 'course.'.$courseid.'.timerinterval.'.$res => $interval,
6009: }
6010: );
1.1172.2.97 raeburn 6011: if (($cachedtime) && (abs($start-$cachedtime) < 5)) {
6012: $cachedtimes{"$courseid\0$res"} = $start;
6013: }
1.1172.2.102 raeburn 6014: } elsif ($putres ne 'refused') {
6015: &logthis("Result: $putres when attempting to set first access time ".
6016: "(type: $type, extent: $res) for $uname:$udom in $courseid");
1.1162 raeburn 6017: }
6018: return $putres;
1.505 albertel 6019: }
6020: return 'already_set';
1.504 albertel 6021: }
1.1153 www 6022: }
1.1172.2.32 raeburn 6023:
6024: sub checkout {
6025: my ($symb,$tuname,$tudom,$tcrsid)=@_;
6026: my $now=time;
6027: my $lonhost=$perlvar{'lonHostID'};
1.1172.2.134 raeburn 6028: my $ip = &get_requestor_ip();
1.1172.2.32 raeburn 6029: my $infostr=&escape(
6030: 'CHECKOUTTOKEN&'.
6031: $tuname.'&'.
6032: $tudom.'&'.
6033: $tcrsid.'&'.
6034: $symb.'&'.
1.1172.2.134 raeburn 6035: $now.'&'.$ip);
1.1172.2.32 raeburn 6036: my $token=&reply('tmpput:'.$infostr,$lonhost);
6037: if ($token=~/^error\:/) {
6038: &logthis("<font color=\"blue\">WARNING: ".
6039: "Checkout tmpput failed ".$tudom.' - '.$tuname.' - '.$symb.
6040: "</font>");
6041: return '';
6042: }
6043:
6044: $token=~s/^(\d+)\_.*\_(\d+)$/$1\*$2\*$lonhost/;
6045: $token=~tr/a-z/A-Z/;
6046:
6047: my %infohash=('resource.0.outtoken' => $token,
6048: 'resource.0.checkouttime' => $now,
1.1172.2.134 raeburn 6049: 'resource.0.outremote' => $ip);
1.1172.2.32 raeburn 6050:
6051: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
6052: return '';
6053: } else {
6054: &logthis("<font color=\"blue\">WARNING: ".
6055: "Checkout cstore failed ".$tudom.' - '.$tuname.' - '.$symb.
6056: "</font>");
6057: }
6058:
6059: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
6060: &escape('Checkout '.$infostr.' - '.
6061: $token)) ne 'ok') {
6062: return '';
6063: } else {
6064: &logthis("<font color=\"blue\">WARNING: ".
6065: "Checkout log failed ".$tudom.' - '.$tuname.' - '.$symb.
6066: "</font>");
6067: }
6068: return $token;
6069: }
6070:
6071: # ------------------------------------------------------------ Check in an item
6072:
6073: sub checkin {
6074: my $token=shift;
6075: my $now=time;
6076: my ($ta,$tb,$lonhost)=split(/\*/,$token);
6077: $lonhost=~tr/A-Z/a-z/;
6078: my $dtoken=$ta.'_'.&hostname($lonhost).'_'.$tb;
6079: $dtoken=~s/\W/\_/g;
1.1172.2.134 raeburn 6080: my $ip = &get_requestor_ip();
1.1172.2.32 raeburn 6081: my ($dummy,$tuname,$tudom,$tcrsid,$symb,$chtim,$rmaddr)=
6082: split(/\&/,&unescape(&reply('tmpget:'.$dtoken,$lonhost)));
6083:
6084: unless (($tuname) && ($tudom)) {
6085: &logthis('Check in '.$token.' ('.$dtoken.') failed');
6086: return '';
6087: }
6088:
6089: unless (&allowed('mgr',$tcrsid)) {
6090: &logthis('Check in '.$token.' ('.$dtoken.') unauthorized: '.
6091: $env{'user.name'}.' - '.$env{'user.domain'});
6092: return '';
6093: }
6094:
6095: my %infohash=('resource.0.intoken' => $token,
6096: 'resource.0.checkintime' => $now,
1.1172.2.134 raeburn 6097: 'resource.0.inremote' => $ip);
1.1172.2.32 raeburn 6098:
6099: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
6100: return '';
6101: }
6102:
6103: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
6104: &escape('Checkin - '.$token)) ne 'ok') {
6105: return '';
6106: }
6107:
6108: return ($symb,$tuname,$tudom,$tcrsid);
6109: }
6110:
1.110 www 6111: # --------------------------------------------- Set Expire Date for Spreadsheet
6112:
6113: sub expirespread {
6114: my ($uname,$udom,$stype,$usymb)=@_;
1.620 albertel 6115: my $cid=$env{'request.course.id'};
1.110 www 6116: if ($cid) {
6117: my $now=time;
6118: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1.620 albertel 6119: return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
6120: $env{'course.'.$cid.'.num'}.
1.110 www 6121: ':nohist_expirationdates:'.
6122: &escape($key).'='.$now,
1.620 albertel 6123: $env{'course.'.$cid.'.home'})
1.110 www 6124: }
6125: return 'ok';
1.14 www 6126: }
6127:
1.109 www 6128: # ----------------------------------------------------- Devalidate Spreadsheets
6129:
6130: sub devalidate {
1.325 www 6131: my ($symb,$uname,$udom)=@_;
1.620 albertel 6132: my $cid=$env{'request.course.id'};
1.109 www 6133: if ($cid) {
1.391 matthew 6134: # delete the stored spreadsheets for
6135: # - the student level sheet of this user in course's homespace
6136: # - the assessment level sheet for this resource
6137: # for this user in user's homespace
1.553 albertel 6138: # - current conditional state info
1.325 www 6139: my $key=$uname.':'.$udom.':';
1.109 www 6140: my $status=
1.299 matthew 6141: &del('nohist_calculatedsheets',
1.391 matthew 6142: [$key.'studentcalc:'],
1.620 albertel 6143: $env{'course.'.$cid.'.domain'},
6144: $env{'course.'.$cid.'.num'})
1.133 albertel 6145: .' '.
6146: &del('nohist_calculatedsheets_'.$cid,
1.391 matthew 6147: [$key.'assesscalc:'.$symb],$udom,$uname);
1.109 www 6148: unless ($status eq 'ok ok') {
6149: &logthis('Could not devalidate spreadsheet '.
1.325 www 6150: $uname.' at '.$udom.' for '.
1.109 www 6151: $symb.': '.$status);
1.133 albertel 6152: }
1.553 albertel 6153: &delenv('user.state.'.$cid);
1.109 www 6154: }
6155: }
6156:
1.265 albertel 6157: sub get_scalar {
6158: my ($string,$end) = @_;
6159: my $value;
6160: if ($$string =~ s/^([^&]*?)($end)/$2/) {
6161: $value = $1;
6162: } elsif ($$string =~ s/^([^&]*?)&//) {
6163: $value = $1;
6164: }
6165: return &unescape($value);
6166: }
6167:
6168: sub array2str {
6169: my (@array) = @_;
6170: my $result=&arrayref2str(\@array);
6171: $result=~s/^__ARRAY_REF__//;
6172: $result=~s/__END_ARRAY_REF__$//;
6173: return $result;
6174: }
6175:
1.204 albertel 6176: sub arrayref2str {
6177: my ($arrayref) = @_;
1.265 albertel 6178: my $result='__ARRAY_REF__';
1.204 albertel 6179: foreach my $elem (@$arrayref) {
1.265 albertel 6180: if(ref($elem) eq 'ARRAY') {
6181: $result.=&arrayref2str($elem).'&';
6182: } elsif(ref($elem) eq 'HASH') {
6183: $result.=&hashref2str($elem).'&';
6184: } elsif(ref($elem)) {
6185: #print("Got a ref of ".(ref($elem))." skipping.");
1.204 albertel 6186: } else {
6187: $result.=&escape($elem).'&';
6188: }
6189: }
6190: $result=~s/\&$//;
1.265 albertel 6191: $result .= '__END_ARRAY_REF__';
1.204 albertel 6192: return $result;
6193: }
6194:
1.168 albertel 6195: sub hash2str {
1.204 albertel 6196: my (%hash) = @_;
6197: my $result=&hashref2str(\%hash);
1.265 albertel 6198: $result=~s/^__HASH_REF__//;
6199: $result=~s/__END_HASH_REF__$//;
1.204 albertel 6200: return $result;
6201: }
6202:
6203: sub hashref2str {
6204: my ($hashref)=@_;
1.265 albertel 6205: my $result='__HASH_REF__';
1.800 albertel 6206: foreach my $key (sort(keys(%$hashref))) {
6207: if (ref($key) eq 'ARRAY') {
6208: $result.=&arrayref2str($key).'=';
6209: } elsif (ref($key) eq 'HASH') {
6210: $result.=&hashref2str($key).'=';
6211: } elsif (ref($key)) {
1.265 albertel 6212: $result.='=';
1.800 albertel 6213: #print("Got a ref of ".(ref($key))." skipping.");
1.204 albertel 6214: } else {
1.1132 raeburn 6215: if (defined($key)) {$result.=&escape($key).'=';} else { last; }
1.204 albertel 6216: }
6217:
1.800 albertel 6218: if(ref($hashref->{$key}) eq 'ARRAY') {
6219: $result.=&arrayref2str($hashref->{$key}).'&';
6220: } elsif(ref($hashref->{$key}) eq 'HASH') {
6221: $result.=&hashref2str($hashref->{$key}).'&';
6222: } elsif(ref($hashref->{$key})) {
1.265 albertel 6223: $result.='&';
1.800 albertel 6224: #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
1.204 albertel 6225: } else {
1.800 albertel 6226: $result.=&escape($hashref->{$key}).'&';
1.204 albertel 6227: }
6228: }
1.168 albertel 6229: $result=~s/\&$//;
1.265 albertel 6230: $result .= '__END_HASH_REF__';
1.168 albertel 6231: return $result;
6232: }
6233:
6234: sub str2hash {
1.265 albertel 6235: my ($string)=@_;
6236: my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
6237: return %$hash;
6238: }
6239:
6240: sub str2hashref {
1.168 albertel 6241: my ($string) = @_;
1.265 albertel 6242:
6243: my %hash;
6244:
6245: if($string !~ /^__HASH_REF__/) {
6246: if (! ($string eq '' || !defined($string))) {
6247: $hash{'error'}='Not hash reference';
6248: }
6249: return (\%hash, $string);
6250: }
6251:
6252: $string =~ s/^__HASH_REF__//;
6253:
6254: while($string !~ /^__END_HASH_REF__/) {
6255: #key
6256: my $key='';
6257: if($string =~ /^__HASH_REF__/) {
6258: ($key, $string)=&str2hashref($string);
6259: if(defined($key->{'error'})) {
6260: $hash{'error'}='Bad data';
6261: return (\%hash, $string);
6262: }
6263: } elsif($string =~ /^__ARRAY_REF__/) {
6264: ($key, $string)=&str2arrayref($string);
6265: if($key->[0] eq 'Array reference error') {
6266: $hash{'error'}='Bad data';
6267: return (\%hash, $string);
6268: }
6269: } else {
6270: $string =~ s/^(.*?)=//;
1.267 albertel 6271: $key=&unescape($1);
1.265 albertel 6272: }
6273: $string =~ s/^=//;
6274:
6275: #value
6276: my $value='';
6277: if($string =~ /^__HASH_REF__/) {
6278: ($value, $string)=&str2hashref($string);
6279: if(defined($value->{'error'})) {
6280: $hash{'error'}='Bad data';
6281: return (\%hash, $string);
6282: }
6283: } elsif($string =~ /^__ARRAY_REF__/) {
6284: ($value, $string)=&str2arrayref($string);
6285: if($value->[0] eq 'Array reference error') {
6286: $hash{'error'}='Bad data';
6287: return (\%hash, $string);
6288: }
6289: } else {
6290: $value=&get_scalar(\$string,'__END_HASH_REF__');
6291: }
6292: $string =~ s/^&//;
6293:
6294: $hash{$key}=$value;
1.204 albertel 6295: }
1.265 albertel 6296:
6297: $string =~ s/^__END_HASH_REF__//;
6298:
6299: return (\%hash, $string);
1.204 albertel 6300: }
6301:
6302: sub str2array {
1.265 albertel 6303: my ($string)=@_;
6304: my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
6305: return @$array;
6306: }
6307:
6308: sub str2arrayref {
1.204 albertel 6309: my ($string) = @_;
1.265 albertel 6310: my @array;
6311:
6312: if($string !~ /^__ARRAY_REF__/) {
6313: if (! ($string eq '' || !defined($string))) {
6314: $array[0]='Array reference error';
6315: }
6316: return (\@array, $string);
6317: }
6318:
6319: $string =~ s/^__ARRAY_REF__//;
6320:
6321: while($string !~ /^__END_ARRAY_REF__/) {
6322: my $value='';
6323: if($string =~ /^__HASH_REF__/) {
6324: ($value, $string)=&str2hashref($string);
6325: if(defined($value->{'error'})) {
6326: $array[0] ='Array reference error';
6327: return (\@array, $string);
6328: }
6329: } elsif($string =~ /^__ARRAY_REF__/) {
6330: ($value, $string)=&str2arrayref($string);
6331: if($value->[0] eq 'Array reference error') {
6332: $array[0] ='Array reference error';
6333: return (\@array, $string);
6334: }
6335: } else {
6336: $value=&get_scalar(\$string,'__END_ARRAY_REF__');
6337: }
6338: $string =~ s/^&//;
6339:
6340: push(@array, $value);
1.191 harris41 6341: }
1.265 albertel 6342:
6343: $string =~ s/^__END_ARRAY_REF__//;
6344:
6345: return (\@array, $string);
1.168 albertel 6346: }
6347:
1.167 albertel 6348: # -------------------------------------------------------------------Temp Store
6349:
1.168 albertel 6350: sub tmpreset {
6351: my ($symb,$namespace,$domain,$stuname) = @_;
6352: if (!$symb) {
6353: $symb=&symbread();
1.620 albertel 6354: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 6355: }
6356: $symb=escape($symb);
6357:
1.620 albertel 6358: if (!$namespace) { $namespace=$env{'request.state'}; }
1.168 albertel 6359: $namespace=~s/\//\_/g;
6360: $namespace=~s/\W//g;
6361:
1.620 albertel 6362: if (!$domain) { $domain=$env{'user.domain'}; }
6363: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 6364: if ($domain eq 'public' && $stuname eq 'public') {
1.1172.2.138 raeburn 6365: $stuname=&get_requestor_ip();
1.591 albertel 6366: }
1.1117 foxr 6367: my $path=LONCAPA::tempdir();
1.168 albertel 6368: my %hash;
6369: if (tie(%hash,'GDBM_File',
6370: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 6371: &GDBM_WRCREAT(),0640)) {
1.1000 raeburn 6372: foreach my $key (keys(%hash)) {
1.180 albertel 6373: if ($key=~ /:$symb/) {
1.168 albertel 6374: delete($hash{$key});
6375: }
6376: }
6377: }
6378: }
6379:
1.167 albertel 6380: sub tmpstore {
1.168 albertel 6381: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
6382:
6383: if (!$symb) {
6384: $symb=&symbread();
1.620 albertel 6385: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 6386: }
6387: $symb=escape($symb);
6388:
6389: if (!$namespace) {
6390: # I don't think we would ever want to store this for a course.
6391: # it seems this will only be used if we don't have a course.
1.620 albertel 6392: #$namespace=$env{'request.course.id'};
1.168 albertel 6393: #if (!$namespace) {
1.620 albertel 6394: $namespace=$env{'request.state'};
1.168 albertel 6395: #}
6396: }
6397: $namespace=~s/\//\_/g;
6398: $namespace=~s/\W//g;
1.620 albertel 6399: if (!$domain) { $domain=$env{'user.domain'}; }
6400: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 6401: if ($domain eq 'public' && $stuname eq 'public') {
1.1172.2.138 raeburn 6402: $stuname=&get_requestor_ip();
1.591 albertel 6403: }
1.168 albertel 6404: my $now=time;
6405: my %hash;
1.1117 foxr 6406: my $path=LONCAPA::tempdir();
1.168 albertel 6407: if (tie(%hash,'GDBM_File',
6408: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 6409: &GDBM_WRCREAT(),0640)) {
1.168 albertel 6410: $hash{"version:$symb"}++;
6411: my $version=$hash{"version:$symb"};
6412: my $allkeys='';
6413: foreach my $key (keys(%$storehash)) {
6414: $allkeys.=$key.':';
1.591 albertel 6415: $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
1.168 albertel 6416: }
6417: $hash{"$version:$symb:timestamp"}=$now;
6418: $allkeys.='timestamp';
6419: $hash{"$version:keys:$symb"}=$allkeys;
6420: if (untie(%hash)) {
6421: return 'ok';
6422: } else {
6423: return "error:$!";
6424: }
6425: } else {
6426: return "error:$!";
6427: }
6428: }
1.167 albertel 6429:
1.168 albertel 6430: # -----------------------------------------------------------------Temp Restore
1.167 albertel 6431:
1.168 albertel 6432: sub tmprestore {
6433: my ($symb,$namespace,$domain,$stuname) = @_;
1.167 albertel 6434:
1.168 albertel 6435: if (!$symb) {
6436: $symb=&symbread();
1.620 albertel 6437: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 6438: }
6439: $symb=escape($symb);
6440:
1.620 albertel 6441: if (!$namespace) { $namespace=$env{'request.state'}; }
1.591 albertel 6442:
1.620 albertel 6443: if (!$domain) { $domain=$env{'user.domain'}; }
6444: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 6445: if ($domain eq 'public' && $stuname eq 'public') {
1.1172.2.138 raeburn 6446: $stuname=&get_requestor_ip();
1.591 albertel 6447: }
1.168 albertel 6448: my %returnhash;
6449: $namespace=~s/\//\_/g;
6450: $namespace=~s/\W//g;
6451: my %hash;
1.1117 foxr 6452: my $path=LONCAPA::tempdir();
1.168 albertel 6453: if (tie(%hash,'GDBM_File',
6454: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 6455: &GDBM_READER(),0640)) {
1.168 albertel 6456: my $version=$hash{"version:$symb"};
6457: $returnhash{'version'}=$version;
6458: my $scope;
6459: for ($scope=1;$scope<=$version;$scope++) {
6460: my $vkeys=$hash{"$scope:keys:$symb"};
6461: my @keys=split(/:/,$vkeys);
6462: my $key;
6463: $returnhash{"$scope:keys"}=$vkeys;
6464: foreach $key (@keys) {
1.591 albertel 6465: $returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
6466: $returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
1.167 albertel 6467: }
6468: }
1.168 albertel 6469: if (!(untie(%hash))) {
6470: return "error:$!";
6471: }
6472: } else {
6473: return "error:$!";
6474: }
6475: return %returnhash;
1.167 albertel 6476: }
6477:
1.9 www 6478: # ----------------------------------------------------------------------- Store
6479:
6480: sub store {
1.1172.2.64 raeburn 6481: my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
1.124 www 6482: my $home='';
6483:
1.168 albertel 6484: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 6485:
1.213 www 6486: $symb=&symbclean($symb);
1.122 albertel 6487: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 6488:
1.620 albertel 6489: if (!$domain) { $domain=$env{'user.domain'}; }
6490: if (!$stuname) { $stuname=$env{'user.name'}; }
1.325 www 6491:
6492: &devalidate($symb,$stuname,$domain);
1.109 www 6493:
6494: $symb=escape($symb);
1.187 www 6495: if (!$namespace) {
1.620 albertel 6496: unless ($namespace=$env{'request.course.id'}) {
1.187 www 6497: return '';
6498: }
6499: }
1.620 albertel 6500: if (!$home) { $home=$env{'user.home'}; }
1.447 www 6501:
1.1172.2.138 raeburn 6502: $$storehash{'ip'}=&get_requestor_ip();
1.447 www 6503: $$storehash{'host'}=$perlvar{'lonHostID'};
6504:
1.12 www 6505: my $namevalue='';
1.800 albertel 6506: foreach my $key (keys(%$storehash)) {
6507: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191 harris41 6508: }
1.12 www 6509: $namevalue=~s/\&$//;
1.187 www 6510: &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.1172.2.64 raeburn 6511: return reply("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
1.9 www 6512: }
6513:
1.47 www 6514: # -------------------------------------------------------------- Critical Store
6515:
6516: sub cstore {
1.1172.2.64 raeburn 6517: my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
1.124 www 6518: my $home='';
6519:
1.168 albertel 6520: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 6521:
1.213 www 6522: $symb=&symbclean($symb);
1.122 albertel 6523: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 6524:
1.620 albertel 6525: if (!$domain) { $domain=$env{'user.domain'}; }
6526: if (!$stuname) { $stuname=$env{'user.name'}; }
1.325 www 6527:
6528: &devalidate($symb,$stuname,$domain);
1.109 www 6529:
6530: $symb=escape($symb);
1.187 www 6531: if (!$namespace) {
1.620 albertel 6532: unless ($namespace=$env{'request.course.id'}) {
1.187 www 6533: return '';
6534: }
6535: }
1.620 albertel 6536: if (!$home) { $home=$env{'user.home'}; }
1.447 www 6537:
1.1172.2.138 raeburn 6538: $$storehash{'ip'}=&get_requestor_ip();
1.447 www 6539: $$storehash{'host'}=$perlvar{'lonHostID'};
1.122 albertel 6540:
1.47 www 6541: my $namevalue='';
1.800 albertel 6542: foreach my $key (keys(%$storehash)) {
6543: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191 harris41 6544: }
1.47 www 6545: $namevalue=~s/\&$//;
1.187 www 6546: &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188 www 6547: return critical
1.1172.2.64 raeburn 6548: ("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
1.47 www 6549: }
6550:
1.9 www 6551: # --------------------------------------------------------------------- Restore
6552:
6553: sub restore {
1.124 www 6554: my ($symb,$namespace,$domain,$stuname) = @_;
6555: my $home='';
6556:
1.168 albertel 6557: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 6558:
1.122 albertel 6559: if (!$symb) {
1.1172.2.26 raeburn 6560: return if ($namespace eq 'courserequests');
6561: unless ($symb=escape(&symbread())) { return ''; }
1.122 albertel 6562: } else {
1.1172.2.26 raeburn 6563: unless ($namespace eq 'courserequests') {
6564: $symb=&escape(&symbclean($symb));
6565: }
1.122 albertel 6566: }
1.188 www 6567: if (!$namespace) {
1.620 albertel 6568: unless ($namespace=$env{'request.course.id'}) {
1.188 www 6569: return '';
6570: }
6571: }
1.620 albertel 6572: if (!$domain) { $domain=$env{'user.domain'}; }
6573: if (!$stuname) { $stuname=$env{'user.name'}; }
6574: if (!$home) { $home=$env{'user.home'}; }
1.122 albertel 6575: my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
6576:
1.12 www 6577: my %returnhash=();
1.800 albertel 6578: foreach my $line (split(/\&/,$answer)) {
6579: my ($name,$value)=split(/\=/,$line);
1.591 albertel 6580: $returnhash{&unescape($name)}=&thaw_unescape($value);
1.191 harris41 6581: }
1.75 www 6582: my $version;
6583: for ($version=1;$version<=$returnhash{'version'};$version++) {
1.800 albertel 6584: foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
6585: $returnhash{$item}=$returnhash{$version.':'.$item};
1.191 harris41 6586: }
1.75 www 6587: }
1.13 www 6588: return %returnhash;
1.34 www 6589: }
6590:
6591: # ---------------------------------------------------------- Course Description
1.1118 foxr 6592: #
6593: #
1.34 www 6594:
6595: sub coursedescription {
1.731 albertel 6596: my ($courseid,$args)=@_;
1.34 www 6597: $courseid=~s/^\///;
1.49 www 6598: $courseid=~s/\_/\//g;
1.34 www 6599: my ($cdomain,$cnum)=split(/\//,$courseid);
1.129 albertel 6600: my $chome=&homeserver($cnum,$cdomain);
1.302 albertel 6601: my $normalid=$cdomain.'_'.$cnum;
6602: # need to always cache even if we get errors otherwise we keep
6603: # trying and trying and trying to get the course description.
6604: my %envhash=();
6605: my %returnhash=();
1.731 albertel 6606:
6607: my $expiretime=600;
6608: if ($env{'request.course.id'} eq $normalid) {
6609: $expiretime=120;
6610: }
6611:
6612: my $prefix='course.'.$cdomain.'_'.$cnum.'.';
6613: if (!$args->{'freshen_cache'}
6614: && ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
6615: foreach my $key (keys(%env)) {
6616: next if ($key !~ /^\Q$prefix\E(.*)/);
6617: my ($setting) = $1;
6618: $returnhash{$setting} = $env{$key};
6619: }
6620: return %returnhash;
6621: }
6622:
1.1118 foxr 6623: # get the data again
6624:
1.731 albertel 6625: if (!$args->{'one_time'}) {
6626: $envhash{'course.'.$normalid.'.last_cache'}=time;
6627: }
1.811 albertel 6628:
1.34 www 6629: if ($chome ne 'no_host') {
1.302 albertel 6630: %returnhash=&dump('environment',$cdomain,$cnum);
1.129 albertel 6631: if (!exists($returnhash{'con_lost'})) {
1.1118 foxr 6632: my $username = $env{'user.name'}; # Defult username
6633: if(defined $args->{'user'}) {
6634: $username = $args->{'user'};
6635: }
1.129 albertel 6636: $returnhash{'home'}= $chome;
6637: $returnhash{'domain'} = $cdomain;
6638: $returnhash{'num'} = $cnum;
1.741 raeburn 6639: if (!defined($returnhash{'type'})) {
6640: $returnhash{'type'} = 'Course';
6641: }
1.130 albertel 6642: while (my ($name,$value) = each %returnhash) {
1.53 www 6643: $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129 albertel 6644: }
1.270 www 6645: $returnhash{'url'}=&clutter($returnhash{'url'});
1.1117 foxr 6646: $returnhash{'fn'}=LONCAPA::tempdir() .
1.1118 foxr 6647: $username.'_'.$cdomain.'_'.$cnum;
1.60 www 6648: $envhash{'course.'.$normalid.'.home'}=$chome;
6649: $envhash{'course.'.$normalid.'.domain'}=$cdomain;
6650: $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34 www 6651: }
6652: }
1.731 albertel 6653: if (!$args->{'one_time'}) {
1.949 raeburn 6654: &appenv(\%envhash);
1.731 albertel 6655: }
1.302 albertel 6656: return %returnhash;
1.461 www 6657: }
6658:
1.1080 raeburn 6659: sub update_released_required {
6660: my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
6661: if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
6662: $cid = $env{'request.course.id'};
6663: $cdom = $env{'course.'.$cid.'.domain'};
6664: $cnum = $env{'course.'.$cid.'.num'};
6665: $chome = $env{'course.'.$cid.'.home'};
6666: }
6667: if ($needsrelease) {
6668: my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
6669: my $needsupdate;
6670: if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
6671: $needsupdate = 1;
6672: } else {
6673: my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
6674: my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
6675: if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
6676: $needsupdate = 1;
6677: }
6678: }
6679: if ($needsupdate) {
6680: my %needshash = (
6681: 'internal.releaserequired' => $needsrelease,
6682: );
6683: my $putresult = &put('environment',\%needshash,$cdom,$cnum);
6684: if ($putresult eq 'ok') {
6685: &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
6686: my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
6687: if (ref($crsinfo{$cid}) eq 'HASH') {
6688: $crsinfo{$cid}{'releaserequired'} = $needsrelease;
6689: &courseidput($cdom,\%crsinfo,$chome,'notime');
6690: }
6691: }
6692: }
6693: }
6694: return;
6695: }
6696:
1.461 www 6697: # -------------------------------------------------See if a user is privileged
6698:
6699: sub privileged {
1.1172.2.23 raeburn 6700: my ($username,$domain,$possdomains,$possroles)=@_;
1.1170 droeschl 6701: my $now = time;
1.1172.2.23 raeburn 6702: my $roles;
6703: if (ref($possroles) eq 'ARRAY') {
6704: $roles = $possroles;
6705: } else {
6706: $roles = ['dc','su'];
6707: }
6708: if (ref($possdomains) eq 'ARRAY') {
6709: my %privileged = &privileged_by_domain($possdomains,$roles);
6710: foreach my $dom (@{$possdomains}) {
6711: if (($username =~ /^$match_username$/) && ($domain =~ /^$match_domain$/) &&
6712: (ref($privileged{$dom}) eq 'HASH')) {
6713: foreach my $role (@{$roles}) {
6714: if (ref($privileged{$dom}{$role}) eq 'HASH') {
6715: if (exists($privileged{$dom}{$role}{$username.':'.$domain})) {
6716: my ($end,$start) = split(/:/,$privileged{$dom}{$role}{$username.':'.$domain});
6717: return 1 unless (($end && $end < $now) ||
6718: ($start && $start > $now));
6719: }
6720: }
6721: }
6722: }
6723: }
6724: } else {
6725: my %rolesdump = &dump("roles", $domain, $username) or return 0;
6726: my $now = time;
1.1170 droeschl 6727:
1.1172.2.58 raeburn 6728: for my $role (@rolesdump{grep { ! /^rolesdef_/ } keys(%rolesdump)}) {
1.1170 droeschl 6729: my ($trole, $tend, $tstart) = split(/_/, $role);
1.1172.2.23 raeburn 6730: if (grep(/^\Q$trole\E$/,@{$roles})) {
6731: return 1 unless ($tend && $tend < $now)
6732: or ($tstart && $tstart > $now);
1.1170 droeschl 6733: }
1.1172.2.23 raeburn 6734: }
6735: }
1.461 www 6736: return 0;
1.9 www 6737: }
1.1 albertel 6738:
1.1172.2.23 raeburn 6739: sub privileged_by_domain {
6740: my ($domains,$roles) = @_;
6741: my %privileged = ();
6742: my $cachetime = 60*60*24;
6743: my $now = time;
6744: unless ((ref($domains) eq 'ARRAY') && (ref($roles) eq 'ARRAY')) {
6745: return %privileged;
6746: }
6747: foreach my $dom (@{$domains}) {
6748: next if (ref($privileged{$dom}) eq 'HASH');
6749: my $needroles;
6750: foreach my $role (@{$roles}) {
6751: my ($result,$cached)=&is_cached_new('priv_'.$role,$dom);
6752: if (defined($cached)) {
6753: if (ref($result) eq 'HASH') {
6754: $privileged{$dom}{$role} = $result;
6755: }
6756: } else {
6757: $needroles = 1;
6758: }
6759: }
6760: if ($needroles) {
6761: my %dompersonnel = &get_domain_roles($dom,$roles);
6762: $privileged{$dom} = {};
6763: foreach my $server (keys(%dompersonnel)) {
6764: if (ref($dompersonnel{$server}) eq 'HASH') {
6765: foreach my $item (keys(%{$dompersonnel{$server}})) {
6766: my ($trole,$uname,$udom,$rest) = split(/:/,$item,4);
6767: my ($end,$start) = split(/:/,$dompersonnel{$server}{$item});
6768: next if ($end && $end < $now);
6769: $privileged{$dom}{$trole}{$uname.':'.$udom} =
6770: $dompersonnel{$server}{$item};
6771: }
6772: }
6773: }
6774: if (ref($privileged{$dom}) eq 'HASH') {
6775: foreach my $role (@{$roles}) {
6776: if (ref($privileged{$dom}{$role}) eq 'HASH') {
6777: &do_cache_new('priv_'.$role,$dom,$privileged{$dom}{$role},$cachetime);
6778: } else {
6779: my %hash = ();
6780: &do_cache_new('priv_'.$role,$dom,\%hash,$cachetime);
6781: }
6782: }
6783: }
6784: }
6785: }
6786: return %privileged;
6787: }
6788:
1.103 harris41 6789: # -------------------------------------------------------- Get user privileges
1.11 www 6790:
6791: sub rolesinit {
1.1169 droeschl 6792: my ($domain, $username) = @_;
6793: my %userroles = ('user.login.time' => time);
6794: my %rolesdump = &dump("roles", $domain, $username) or return \%userroles;
6795:
6796: # firstaccess and timerinterval are related to timed maps/resources.
6797: # also, blocking can be triggered by an activating timer
6798: # it's saved in the user's %env.
6799: my %firstaccess = &dump('firstaccesstimes', $domain, $username);
6800: my %timerinterval = &dump('timerinterval', $domain, $username);
6801: my (%coursetimerstarts, %firstaccchk, %firstaccenv, %coursetimerintervals,
1.1172.2.146. .18(raeb 6802:-24): %timerintchk, %timerintenv,%coauthorenv);
1.1169 droeschl 6803:
1.1162 raeburn 6804: foreach my $key (keys(%firstaccess)) {
1.1169 droeschl 6805: my ($cid, $rest) = split(/\0/, $key);
1.1162 raeburn 6806: $coursetimerstarts{$cid}{$rest} = $firstaccess{$key};
6807: }
1.1169 droeschl 6808:
1.1162 raeburn 6809: foreach my $key (keys(%timerinterval)) {
6810: my ($cid,$rest) = split(/\0/,$key);
6811: $coursetimerintervals{$cid}{$rest} = $timerinterval{$key};
6812: }
1.1169 droeschl 6813:
1.11 www 6814: my %allroles=();
1.1162 raeburn 6815: my %allgroups=();
1.1172.2.146. .18(raeb 6816:-24): my %gotcoauconfig=();
.20(raeb 6817:-24): my %domdefaults=();
1.11 www 6818:
1.1172.2.58 raeburn 6819: for my $area (grep { ! /^rolesdef_/ } keys(%rolesdump)) {
1.1169 droeschl 6820: my $role = $rolesdump{$area};
6821: $area =~ s/\_\w\w$//;
6822:
6823: my ($trole, $tend, $tstart, $group_privs);
6824:
6825: if ($role =~ /^cr/) {
6826: # Custom role, defined by a user
6827: # e.g., user.role.cr/msu/smith/mynewrole
6828: if ($role =~ m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
6829: $trole = $1;
6830: ($tend, $tstart) = split('_', $2);
6831: } else {
6832: $trole = $role;
6833: }
6834: } elsif ($role =~ m|^gr/|) {
6835: # Role of member in a group, defined within a course/community
6836: # e.g., user.role.gr/msu/04935610a19ee4a5fmsul1/leopards
6837: ($trole, $tend, $tstart) = split(/_/, $role);
6838: next if $tstart eq '-1';
6839: ($trole, $group_privs) = split(/\//, $trole);
6840: $group_privs = &unescape($group_privs);
6841: } else {
6842: # Just a normal role, defined in roles.tab
6843: ($trole, $tend, $tstart) = split(/_/,$role);
6844: }
6845:
6846: my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
6847: $username);
6848: @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
6849:
6850: # role expired or not available yet?
6851: $trole = '' if ($tend != 0 && $tend < $userroles{'user.login.time'}) or
6852: ($tstart != 0 && $tstart > $userroles{'user.login.time'});
6853:
6854: next if $area eq '' or $trole eq '';
6855:
6856: my $spec = "$trole.$area";
6857: my ($tdummy, $tdomain, $trest) = split(/\//, $area);
6858:
6859: if ($trole =~ /^cr\//) {
6860: # Custom role, defined by a user
6861: &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
6862: } elsif ($trole eq 'gr') {
6863: # Role of a member in a group, defined within a course/community
6864: &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
6865: next;
6866: } else {
6867: # Normal role, defined in roles.tab
6868: &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
1.1172.2.146. .18(raeb 6869:-24): if (($trole eq 'ca') || ($trole eq 'aa')) {
6870:-24): (undef,my ($audom,$auname)) = split(/\//,$area);
6871:-24): unless ($gotcoauconfig{$area}) {
.22(raeb 6872:-24): my @ca_settings = ('authoreditors','coauthorlist','coauthoroptin');
.18(raeb 6873:-24): my %info = &userenvironment($audom,$auname,@ca_settings);
6874:-24): $gotcoauconfig{$area} = 1;
6875:-24): foreach my $item (@ca_settings) {
6876:-24): if (exists($info{$item})) {
6877:-24): my $name = $item;
6878:-24): if ($item eq 'authoreditors') {
6879:-24): $name = 'editors';
.20(raeb 6880:-24): unless ($info{'authoreditors'}) {
6881:-24): my %domdefs;
6882:-24): if (ref($domdefaults{$audom}) eq 'HASH') {
6883:-24): %domdefs = %{$domdefaults{$audom}};
6884:-24): } else {
6885:-24): %domdefs = &get_domain_defaults($audom);
6886:-24): $domdefaults{$audom} = \%domdefs;
6887:-24): }
6888:-24): if ($domdefs{$name} ne '') {
6889:-24): $info{'authoreditors'} = $domdefs{$name};
6890:-24): } else {
6891:-24): $info{'authoreditors'} = 'edit,xml';
6892:-24): }
6893:-24): }
.18(raeb 6894:-24): }
6895:-24): $coauthorenv{"environment.internal.$name.$area"} = $info{$item};
6896:-24): }
6897:-24): }
6898:-24): }
6899:-24): }
1.1169 droeschl 6900: }
6901:
6902: my $cid = $tdomain.'_'.$trest;
6903: unless ($firstaccchk{$cid}) {
6904: if (ref($coursetimerstarts{$cid}) eq 'HASH') {
6905: foreach my $item (keys(%{$coursetimerstarts{$cid}})) {
6906: $firstaccenv{'course.'.$cid.'.firstaccess.'.$item} =
6907: $coursetimerstarts{$cid}{$item};
6908: }
6909: }
6910: $firstaccchk{$cid} = 1;
6911: }
6912: unless ($timerintchk{$cid}) {
6913: if (ref($coursetimerintervals{$cid}) eq 'HASH') {
6914: foreach my $item (keys(%{$coursetimerintervals{$cid}})) {
6915: $timerintenv{'course.'.$cid.'.timerinterval.'.$item} =
6916: $coursetimerintervals{$cid}{$item};
1.1162 raeburn 6917: }
1.12 www 6918: }
1.1169 droeschl 6919: $timerintchk{$cid} = 1;
1.191 harris41 6920: }
1.11 www 6921: }
1.1169 droeschl 6922:
1.1172.2.91 raeburn 6923: @userroles{'user.author','user.adv','user.rar'} = &set_userprivs(\%userroles,
6924: \%allroles, \%allgroups);
1.1169 droeschl 6925: $env{'user.adv'} = $userroles{'user.adv'};
1.1172.2.91 raeburn 6926: $env{'user.rar'} = $userroles{'user.rar'};
1.1169 droeschl 6927:
1.1172.2.146. .18(raeb 6928:-24): return (\%userroles,\%firstaccenv,\%timerintenv,\%coauthorenv);
1.11 www 6929: }
6930:
1.567 raeburn 6931: sub set_arearole {
1.1172.2.19 raeburn 6932: my ($trole,$area,$tstart,$tend,$domain,$username,$nolog) = @_;
6933: unless ($nolog) {
1.567 raeburn 6934: # log the associated role with the area
1.1172.2.19 raeburn 6935: &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
6936: }
1.743 albertel 6937: return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
1.567 raeburn 6938: }
6939:
6940: sub custom_roleprivs {
6941: my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
6942: my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
1.1172.2.40 raeburn 6943: my $homsvr = &homeserver($rauthor,$rdomain);
1.838 albertel 6944: if (&hostname($homsvr) ne '') {
1.567 raeburn 6945: my ($rdummy,$roledef)=
6946: &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
6947: if (($rdummy ne 'con_lost') && ($roledef ne '')) {
6948: my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
6949: if (defined($syspriv)) {
1.1043 raeburn 6950: if ($trest =~ /^$match_community$/) {
6951: $syspriv =~ s/bre\&S//;
6952: }
1.567 raeburn 6953: $$allroles{'cm./'}.=':'.$syspriv;
6954: $$allroles{$spec.'./'}.=':'.$syspriv;
6955: }
6956: if ($tdomain ne '') {
6957: if (defined($dompriv)) {
6958: $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
6959: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
6960: }
6961: if (($trest ne '') && (defined($coursepriv))) {
1.1172.2.89 raeburn 6962: if ($trole =~ m{^cr/$tdomain/$tdomain\Q-domainconfig\E/([^/]+)$}) {
6963: my $rolename = $1;
6964: $coursepriv = &course_adhocrole_privs($rolename,$tdomain,$trest,$coursepriv);
6965: }
1.567 raeburn 6966: $$allroles{'cm.'.$area}.=':'.$coursepriv;
6967: $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
6968: }
6969: }
6970: }
6971: }
6972: }
6973:
1.1172.2.89 raeburn 6974: sub course_adhocrole_privs {
6975: my ($rolename,$cdom,$cnum,$coursepriv) = @_;
6976: my %overrides = &get('environment',["internal.adhocpriv.$rolename"],$cdom,$cnum);
6977: if ($overrides{"internal.adhocpriv.$rolename"}) {
6978: my (%currprivs,%storeprivs);
6979: foreach my $item (split(/:/,$coursepriv)) {
6980: my ($priv,$restrict) = split(/\&/,$item);
6981: $currprivs{$priv} = $restrict;
6982: }
6983: my (%possadd,%possremove,%full);
6984: foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
6985: my ($priv,$restrict)=split(/\&/,$item);
6986: $full{$priv} = $restrict;
6987: }
6988: foreach my $item (split(/,/,$overrides{"internal.adhocpriv.$rolename"})) {
1.1172.2.146. .6(raebu 6989:22): next if ($item eq '');
6990:22): my ($rule,$rest) = split(/=/,$item);
6991:22): next unless (($rule eq 'off') || ($rule eq 'on'));
6992:22): foreach my $priv (split(/:/,$rest)) {
6993:22): if ($priv ne '') {
6994:22): if ($rule eq 'off') {
6995:22): $possremove{$priv} = 1;
6996:22): } else {
6997:22): $possadd{$priv} = 1;
6998:22): }
6999:22): }
7000:22): }
7001:22): }
7002:22): foreach my $priv (sort(keys(%full))) {
7003:22): if (exists($currprivs{$priv})) {
7004:22): unless (exists($possremove{$priv})) {
7005:22): $storeprivs{$priv} = $currprivs{$priv};
7006:22): }
7007:22): } elsif (exists($possadd{$priv})) {
7008:22): $storeprivs{$priv} = $full{$priv};
7009:22): }
7010:22): }
7011:22): $coursepriv = ':'.join(':',map { $_.'&'.$storeprivs{$_}; } sort(keys(%storeprivs)));
7012:22): }
7013:22): return $coursepriv;
1.1172.2.89 raeburn 7014: }
7015:
1.678 raeburn 7016: sub group_roleprivs {
7017: my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
7018: my $access = 1;
7019: my $now = time;
7020: if (($tend!=0) && ($tend<$now)) { $access = 0; }
7021: if (($tstart!=0) && ($tstart>$now)) { $access=0; }
7022: if ($access) {
1.811 albertel 7023: my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
1.678 raeburn 7024: $$allgroups{$course}{$group} .=':'.$group_privs;
7025: }
7026: }
1.567 raeburn 7027:
7028: sub standard_roleprivs {
7029: my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
7030: if (defined($pr{$trole.':s'})) {
7031: $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
7032: $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
7033: }
7034: if ($tdomain ne '') {
7035: if (defined($pr{$trole.':d'})) {
7036: $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
7037: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
7038: }
7039: if (($trest ne '') && (defined($pr{$trole.':c'}))) {
7040: $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
7041: $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
7042: }
7043: }
7044: }
7045:
7046: sub set_userprivs {
1.1064 raeburn 7047: my ($userroles,$allroles,$allgroups,$groups_roles) = @_;
1.567 raeburn 7048: my $author=0;
7049: my $adv=0;
1.1172.2.91 raeburn 7050: my $rar=0;
1.678 raeburn 7051: my %grouproles = ();
7052: if (keys(%{$allgroups}) > 0) {
1.1064 raeburn 7053: my @groupkeys;
1.1000 raeburn 7054: foreach my $role (keys(%{$allroles})) {
1.1064 raeburn 7055: push(@groupkeys,$role);
7056: }
7057: if (ref($groups_roles) eq 'HASH') {
7058: foreach my $key (keys(%{$groups_roles})) {
7059: unless (grep(/^\Q$key\E$/,@groupkeys)) {
7060: push(@groupkeys,$key);
7061: }
7062: }
7063: }
7064: if (@groupkeys > 0) {
7065: foreach my $role (@groupkeys) {
7066: my ($trole,$area,$sec,$extendedarea);
7067: if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
7068: $trole = $1;
7069: $area = $2;
7070: $sec = $3;
7071: $extendedarea = $area.$sec;
7072: if (exists($$allgroups{$area})) {
7073: foreach my $group (keys(%{$$allgroups{$area}})) {
7074: my $spec = $trole.'.'.$extendedarea;
7075: $grouproles{$spec.'.'.$area.'/'.$group} =
1.681 raeburn 7076: $$allgroups{$area}{$group};
1.1064 raeburn 7077: }
1.678 raeburn 7078: }
7079: }
7080: }
7081: }
7082: }
1.800 albertel 7083: foreach my $group (keys(%grouproles)) {
7084: $$allroles{$group} = $grouproles{$group};
1.678 raeburn 7085: }
1.800 albertel 7086: foreach my $role (keys(%{$allroles})) {
7087: my %thesepriv;
1.941 raeburn 7088: if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
1.800 albertel 7089: foreach my $item (split(/:/,$$allroles{$role})) {
7090: if ($item ne '') {
7091: my ($privilege,$restrictions)=split(/&/,$item);
1.567 raeburn 7092: if ($restrictions eq '') {
7093: $thesepriv{$privilege}='F';
7094: } elsif ($thesepriv{$privilege} ne 'F') {
7095: $thesepriv{$privilege}.=$restrictions;
7096: }
7097: if ($thesepriv{'adv'} eq 'F') { $adv=1; }
1.1172.2.91 raeburn 7098: if ($thesepriv{'rar'} eq 'F') { $rar=1; }
1.567 raeburn 7099: }
7100: }
7101: my $thesestr='';
1.1104 raeburn 7102: foreach my $priv (sort(keys(%thesepriv))) {
1.800 albertel 7103: $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
7104: }
7105: $userroles->{'user.priv.'.$role} = $thesestr;
1.567 raeburn 7106: }
1.1172.2.91 raeburn 7107: return ($author,$adv,$rar);
1.567 raeburn 7108: }
7109:
1.994 raeburn 7110: sub role_status {
1.1104 raeburn 7111: my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
1.994 raeburn 7112: if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
1.1172.2.40 raeburn 7113: my ($one,$two) = split(m{\./},$rolekey,2);
7114: (undef,undef,$$role) = split(/\./,$one,3);
1.994 raeburn 7115: unless (!defined($$role) || $$role eq '') {
1.1172.2.40 raeburn 7116: $$where = '/'.$two;
1.994 raeburn 7117: $$trolecode=$$role.'.'.$$where;
7118: ($$tstart,$$tend)=split(/\./,$env{$rolekey});
7119: $$tstatus='is';
1.1104 raeburn 7120: if ($$tstart && $$tstart>$update) {
1.994 raeburn 7121: $$tstatus='future';
1.1034 raeburn 7122: if ($$tstart<$now) {
7123: if ($$tstart && $$tstart>$refresh) {
1.1002 raeburn 7124: if (($$where ne '') && ($$role ne '')) {
1.1064 raeburn 7125: my (%allroles,%allgroups,$group_privs,
7126: %groups_roles,@rolecodes);
1.1002 raeburn 7127: my %userroles = (
7128: 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
7129: );
1.1064 raeburn 7130: @rolecodes = ('cm');
1.1002 raeburn 7131: my $spec=$$role.'.'.$$where;
7132: my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
7133: if ($$role =~ /^cr\//) {
7134: &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
1.1064 raeburn 7135: push(@rolecodes,'cr');
1.1002 raeburn 7136: } elsif ($$role eq 'gr') {
1.1064 raeburn 7137: push(@rolecodes,$$role);
1.1002 raeburn 7138: my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
7139: $env{'user.name'});
1.1064 raeburn 7140: my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
1.1002 raeburn 7141: (undef,my $group_privs) = split(/\//,$trole);
7142: $group_privs = &unescape($group_privs);
7143: &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
1.1064 raeburn 7144: my %course_roles = &get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',['active'],['cc','co','in','ta','ep','ad','st','cr'],[$tdomain],1);
1.1104 raeburn 7145: &get_groups_roles($tdomain,$trest,
7146: \%course_roles,\@rolecodes,
7147: \%groups_roles);
1.1002 raeburn 7148: } else {
1.1064 raeburn 7149: push(@rolecodes,$$role);
1.1002 raeburn 7150: &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
7151: }
1.1172.2.91 raeburn 7152: my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%allroles,\%allgroups,
7153: \%groups_roles);
1.1064 raeburn 7154: &appenv(\%userroles,\@rolecodes);
1.1172.2.92 raeburn 7155: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
1.1002 raeburn 7156: }
7157: }
1.1034 raeburn 7158: $$tstatus = 'is';
1.1002 raeburn 7159: }
1.994 raeburn 7160: }
7161: if ($$tend) {
1.1104 raeburn 7162: if ($$tend<$update) {
1.994 raeburn 7163: $$tstatus='expired';
7164: } elsif ($$tend<$now) {
7165: $$tstatus='will_not';
7166: }
7167: }
7168: }
7169: }
7170: }
7171:
1.1104 raeburn 7172: sub get_groups_roles {
7173: my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
7174: return unless((ref($cdom_courseroles) eq 'HASH') &&
7175: (ref($rolecodes) eq 'ARRAY') &&
7176: (ref($groups_roles) eq 'HASH'));
7177: if (keys(%{$cdom_courseroles}) > 0) {
7178: my ($cnum) = ($rest =~ /^($match_courseid)/);
7179: if ($cdom ne '' && $cnum ne '') {
7180: foreach my $key (keys(%{$cdom_courseroles})) {
7181: if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
7182: my $crsrole = $1;
7183: my $crssec = $2;
7184: if ($crsrole =~ /^cr/) {
7185: unless (grep(/^cr$/,@{$rolecodes})) {
7186: push(@{$rolecodes},'cr');
7187: }
7188: } else {
7189: unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
7190: push(@{$rolecodes},$crsrole);
7191: }
7192: }
7193: my $rolekey = "$crsrole./$cdom/$cnum";
7194: if ($crssec ne '') {
7195: $rolekey .= "/$crssec";
7196: }
7197: $rolekey .= './';
7198: $groups_roles->{$rolekey} = $rolecodes;
7199: }
7200: }
7201: }
7202: }
7203: return;
7204: }
7205:
7206: sub delete_env_groupprivs {
7207: my ($where,$courseroles,$possroles) = @_;
7208: return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
7209: my ($dummy,$udom,$uname,$group) = split(/\//,$where);
7210: unless (ref($courseroles->{$udom}) eq 'HASH') {
7211: %{$courseroles->{$udom}} =
7212: &get_my_roles('','','userroles',['active'],
7213: $possroles,[$udom],1);
7214: }
7215: if (ref($courseroles->{$udom}) eq 'HASH') {
7216: foreach my $item (keys(%{$courseroles->{$udom}})) {
7217: my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
7218: my $area = '/'.$cdom.'/'.$cnum;
7219: my $privkey = "user.priv.$crsrole.$area";
7220: if ($crssec ne '') {
7221: $privkey .= '/'.$crssec;
7222: }
7223: $privkey .= ".$area/$group";
7224: &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
7225: }
7226: }
7227: return;
7228: }
7229:
1.994 raeburn 7230: sub check_adhoc_privs {
1.1172.2.86 raeburn 7231: my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller,$sec) = @_;
1.994 raeburn 7232: my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
1.1172.2.86 raeburn 7233: if ($sec) {
7234: $cckey .= '/'.$sec;
7235: }
1.1172.2.9 raeburn 7236: my $setprivs;
1.994 raeburn 7237: if ($env{$cckey}) {
7238: my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
1.1104 raeburn 7239: &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
1.994 raeburn 7240: unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
1.1172.2.86 raeburn 7241: &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
1.1172.2.9 raeburn 7242: $setprivs = 1;
1.994 raeburn 7243: }
7244: } else {
1.1172.2.87 raeburn 7245: &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
1.1172.2.9 raeburn 7246: $setprivs = 1;
1.994 raeburn 7247: }
1.1172.2.9 raeburn 7248: return $setprivs;
1.994 raeburn 7249: }
7250:
7251: sub set_adhoc_privileges {
1.1172.2.84 raeburn 7252: # role can be cc, ca, or cr/<dom>/<dom>-domainconfig/role
1.1172.2.86 raeburn 7253: my ($dcdom,$pickedcourse,$role,$caller,$sec) = @_;
1.994 raeburn 7254: my $area = '/'.$dcdom.'/'.$pickedcourse;
1.1172.2.86 raeburn 7255: if ($sec ne '') {
7256: $area .= '/'.$sec;
7257: }
1.994 raeburn 7258: my $spec = $role.'.'.$area;
7259: my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
1.1172.2.19 raeburn 7260: $env{'user.name'},1);
1.1172.2.84 raeburn 7261: my %rolehash = ();
1.1172.2.89 raeburn 7262: if ($role =~ m{^\Qcr/$dcdom/$dcdom\E\-domainconfig/(\w+)$}) {
7263: my $rolename = $1;
1.1172.2.84 raeburn 7264: &custom_roleprivs(\%rolehash,$role,$dcdom,$pickedcourse,$spec,$area);
1.1172.2.89 raeburn 7265: my %domdef = &get_domain_defaults($dcdom);
7266: if (ref($domdef{'adhocroles'}) eq 'HASH') {
7267: if (ref($domdef{'adhocroles'}{$rolename}) eq 'HASH') {
7268: &appenv({'request.role.desc' => $domdef{'adhocroles'}{$rolename}{'desc'},});
7269: }
7270: }
1.1172.2.84 raeburn 7271: } else {
7272: &standard_roleprivs(\%rolehash,$role,$dcdom,$spec,$pickedcourse,$area);
7273: }
1.1172.2.91 raeburn 7274: my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%rolehash);
1.994 raeburn 7275: &appenv(\%userroles,[$role,'cm']);
1.1172.2.92 raeburn 7276: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
1.1172.2.146. .1(raebu 7277:22): unless (($caller eq 'constructaccess' && $env{'request.course.id'}) ||
7278:22): ($caller eq 'tiny')) {
1.1088 raeburn 7279: &appenv( {'request.role' => $spec,
7280: 'request.role.domain' => $dcdom,
1.1172.2.89 raeburn 7281: 'request.course.sec' => $sec,
1.1088 raeburn 7282: }
7283: );
7284: my $tadv=0;
7285: if (&allowed('adv') eq 'F') { $tadv=1; }
7286: &appenv({'request.role.adv' => $tadv});
7287: }
1.994 raeburn 7288: }
7289:
1.12 www 7290: # --------------------------------------------------------------- get interface
7291:
7292: sub get {
1.131 albertel 7293: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 7294: my $items='';
1.800 albertel 7295: foreach my $item (@$storearr) {
7296: $items.=&escape($item).'&';
1.191 harris41 7297: }
1.12 www 7298: $items=~s/\&$//;
1.620 albertel 7299: if (!$udomain) { $udomain=$env{'user.domain'}; }
7300: if (!$uname) { $uname=$env{'user.name'}; }
1.131 albertel 7301: my $uhome=&homeserver($uname,$udomain);
7302:
1.133 albertel 7303: my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 7304: my @pairs=split(/\&/,$rep);
1.273 albertel 7305: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
7306: return @pairs;
7307: }
1.15 www 7308: my %returnhash=();
1.42 www 7309: my $i=0;
1.800 albertel 7310: foreach my $item (@$storearr) {
7311: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42 www 7312: $i++;
1.191 harris41 7313: }
1.15 www 7314: return %returnhash;
1.27 www 7315: }
7316:
7317: # --------------------------------------------------------------- del interface
7318:
7319: sub del {
1.133 albertel 7320: my ($namespace,$storearr,$udomain,$uname)=@_;
1.27 www 7321: my $items='';
1.800 albertel 7322: foreach my $item (@$storearr) {
7323: $items.=&escape($item).'&';
1.191 harris41 7324: }
1.984 neumanie 7325:
1.27 www 7326: $items=~s/\&$//;
1.620 albertel 7327: if (!$udomain) { $udomain=$env{'user.domain'}; }
7328: if (!$uname) { $uname=$env{'user.name'}; }
1.133 albertel 7329: my $uhome=&homeserver($uname,$udomain);
7330: return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 7331: }
7332:
7333: # -------------------------------------------------------------- dump interface
7334:
1.1172.2.22 raeburn 7335: sub unserialize {
7336: my ($rep, $escapedkeys) = @_;
7337:
7338: return {} if $rep =~ /^error/;
7339:
7340: my %returnhash=();
7341: foreach my $item (split(/\&/,$rep)) {
7342: my ($key, $value) = split(/=/, $item, 2);
7343: $key = unescape($key) unless $escapedkeys;
7344: next if $key =~ /^error: 2 /;
7345: $returnhash{$key} = &thaw_unescape($value);
7346: }
7347: return \%returnhash;
7348: }
7349:
7350: # see Lond::dump_with_regexp
7351: # if $escapedkeys hash keys won't get unescaped.
1.15 www 7352: sub dump {
1.1172.2.146. .1(raebu 7353:22): my ($namespace,$udomain,$uname,$regexp,$range,$escapedkeys,$encrypt)=@_;
1.755 albertel 7354: if (!$udomain) { $udomain=$env{'user.domain'}; }
7355: if (!$uname) { $uname=$env{'user.name'}; }
7356: my $uhome=&homeserver($uname,$udomain);
1.1167 droeschl 7357:
1.1172.2.55 raeburn 7358: if ($regexp) {
7359: $regexp=&escape($regexp);
7360: } else {
7361: $regexp='.';
7362: }
1.1172.2.22 raeburn 7363: if (grep { $_ eq $uhome } ¤t_machine_ids()) {
7364: # user is hosted on this machine
1.1172.2.55 raeburn 7365: my $reply = LONCAPA::Lond::dump_with_regexp(join(':', ($udomain,
1.1172.2.27 raeburn 7366: $uname, $namespace, $regexp, $range)), $perlvar{'lonVersion'});
1.1172.2.22 raeburn 7367: return %{&unserialize($reply, $escapedkeys)};
7368: }
1.1172.2.146. .1(raebu 7369:22): my $rep;
7370:22): if ($encrypt) {
7371:22): $rep=&reply("encrypt:edump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
7372:22): } else {
7373:22): $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
7374:22): }
1.755 albertel 7375: my @pairs=split(/\&/,$rep);
7376: my %returnhash=();
1.1098 foxr 7377: if (!($rep =~ /^error/ )) {
7378: foreach my $item (@pairs) {
7379: my ($key,$value)=split(/=/,$item,2);
1.1172.2.22 raeburn 7380: $key = &unescape($key) unless ($escapedkeys);
1.1098 foxr 7381: next if ($key =~ /^error: 2 /);
7382: $returnhash{$key}=&thaw_unescape($value);
7383: }
1.755 albertel 7384: }
7385: return %returnhash;
1.407 www 7386: }
7387:
1.1098 foxr 7388:
1.717 albertel 7389: # --------------------------------------------------------- dumpstore interface
7390:
7391: sub dumpstore {
7392: my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.1172.2.22 raeburn 7393: # same as dump but keys must be escaped. They may contain colon separated
7394: # lists of values that may themself contain colons (e.g. symbs).
7395: return &dump($namespace, $udomain, $uname, $regexp, $range, 1);
1.717 albertel 7396: }
7397:
1.407 www 7398: # -------------------------------------------------------------- keys interface
7399:
7400: sub getkeys {
7401: my ($namespace,$udomain,$uname)=@_;
1.620 albertel 7402: if (!$udomain) { $udomain=$env{'user.domain'}; }
7403: if (!$uname) { $uname=$env{'user.name'}; }
1.407 www 7404: my $uhome=&homeserver($uname,$udomain);
7405: my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
7406: my @keyarray=();
1.800 albertel 7407: foreach my $key (split(/\&/,$rep)) {
1.812 raeburn 7408: next if ($key =~ /^error: 2 /);
1.800 albertel 7409: push(@keyarray,&unescape($key));
1.407 www 7410: }
7411: return @keyarray;
1.318 matthew 7412: }
7413:
1.319 matthew 7414: # --------------------------------------------------------------- currentdump
7415: sub currentdump {
1.328 matthew 7416: my ($courseid,$sdom,$sname)=@_;
1.620 albertel 7417: $courseid = $env{'request.course.id'} if (! defined($courseid));
7418: $sdom = $env{'user.domain'} if (! defined($sdom));
7419: $sname = $env{'user.name'} if (! defined($sname));
1.326 matthew 7420: my $uhome = &homeserver($sname,$sdom);
1.1172.2.50 raeburn 7421: my $rep;
7422:
7423: if (grep { $_ eq $uhome } current_machine_ids()) {
7424: $rep = LONCAPA::Lond::dump_profile_database(join(":", ($sdom, $sname,
7425: $courseid)));
7426: } else {
7427: $rep = reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
7428: }
7429:
1.318 matthew 7430: return if ($rep =~ /^(error:|no_such_host)/);
1.319 matthew 7431: #
1.318 matthew 7432: my %returnhash=();
1.319 matthew 7433: #
7434: if ($rep eq "unknown_cmd") {
7435: # an old lond will not know currentdump
7436: # Do a dump and make it look like a currentdump
1.822 albertel 7437: my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
1.319 matthew 7438: return if ($tmp[0] =~ /^(error:|no_such_host)/);
7439: my %hash = @tmp;
7440: @tmp=();
1.424 matthew 7441: %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319 matthew 7442: } else {
7443: my @pairs=split(/\&/,$rep);
1.800 albertel 7444: foreach my $pair (@pairs) {
7445: my ($key,$value)=split(/=/,$pair,2);
1.319 matthew 7446: my ($symb,$param) = split(/:/,$key);
7447: $returnhash{&unescape($symb)}->{&unescape($param)} =
1.557 albertel 7448: &thaw_unescape($value);
1.319 matthew 7449: }
1.191 harris41 7450: }
1.12 www 7451: return %returnhash;
1.424 matthew 7452: }
7453:
7454: sub convert_dump_to_currentdump{
7455: my %hash = %{shift()};
7456: my %returnhash;
7457: # Code ripped from lond, essentially. The only difference
7458: # here is the unescaping done by lonnet::dump(). Conceivably
7459: # we might run in to problems with parameter names =~ /^v\./
7460: while (my ($key,$value) = each(%hash)) {
7461: my ($v,$symb,$param) = split(/:/,$key);
1.822 albertel 7462: $symb = &unescape($symb);
7463: $param = &unescape($param);
1.424 matthew 7464: next if ($v eq 'version' || $symb eq 'keys');
7465: next if (exists($returnhash{$symb}) &&
7466: exists($returnhash{$symb}->{$param}) &&
7467: $returnhash{$symb}->{'v.'.$param} > $v);
7468: $returnhash{$symb}->{$param}=$value;
7469: $returnhash{$symb}->{'v.'.$param}=$v;
7470: }
7471: #
7472: # Remove all of the keys in the hashes which keep track of
7473: # the version of the parameter.
7474: while (my ($symb,$param_hash) = each(%returnhash)) {
7475: # use a foreach because we are going to delete from the hash.
7476: foreach my $key (keys(%$param_hash)) {
7477: delete($param_hash->{$key}) if ($key =~ /^v\./);
7478: }
7479: }
7480: return \%returnhash;
1.12 www 7481: }
7482:
1.627 albertel 7483: # ------------------------------------------------------ critical inc interface
7484:
7485: sub cinc {
7486: return &inc(@_,'critical');
7487: }
7488:
1.449 matthew 7489: # --------------------------------------------------------------- inc interface
7490:
7491: sub inc {
1.627 albertel 7492: my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620 albertel 7493: if (!$udomain) { $udomain=$env{'user.domain'}; }
7494: if (!$uname) { $uname=$env{'user.name'}; }
1.449 matthew 7495: my $uhome=&homeserver($uname,$udomain);
7496: my $items='';
7497: if (! ref($store)) {
7498: # got a single value, so use that instead
7499: $items = &escape($store).'=&';
7500: } elsif (ref($store) eq 'SCALAR') {
7501: $items = &escape($$store).'=&';
7502: } elsif (ref($store) eq 'ARRAY') {
7503: $items = join('=&',map {&escape($_);} @{$store});
7504: } elsif (ref($store) eq 'HASH') {
7505: while (my($key,$value) = each(%{$store})) {
7506: $items.= &escape($key).'='.&escape($value).'&';
7507: }
7508: }
7509: $items=~s/\&$//;
1.627 albertel 7510: if ($critical) {
7511: return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
7512: } else {
7513: return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
7514: }
1.449 matthew 7515: }
7516:
1.12 www 7517: # --------------------------------------------------------------- put interface
7518:
7519: sub put {
1.1172.2.146. .1(raebu 7520:22): my ($namespace,$storehash,$udomain,$uname,$encrypt)=@_;
1.620 albertel 7521: if (!$udomain) { $udomain=$env{'user.domain'}; }
7522: if (!$uname) { $uname=$env{'user.name'}; }
1.134 albertel 7523: my $uhome=&homeserver($uname,$udomain);
1.12 www 7524: my $items='';
1.800 albertel 7525: foreach my $item (keys(%$storehash)) {
7526: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191 harris41 7527: }
1.12 www 7528: $items=~s/\&$//;
1.1172.2.146. .1(raebu 7529:22): if ($encrypt) {
7530:22): return &reply("encrypt:put:$udomain:$uname:$namespace:$items",$uhome);
7531:22): } else {
7532:22): return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
7533:22): }
1.47 www 7534: }
7535:
1.631 albertel 7536: # ------------------------------------------------------------ newput interface
7537:
7538: sub newput {
7539: my ($namespace,$storehash,$udomain,$uname)=@_;
7540: if (!$udomain) { $udomain=$env{'user.domain'}; }
7541: if (!$uname) { $uname=$env{'user.name'}; }
7542: my $uhome=&homeserver($uname,$udomain);
7543: my $items='';
7544: foreach my $key (keys(%$storehash)) {
7545: $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
7546: }
7547: $items=~s/\&$//;
7548: return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
7549: }
7550:
7551: # --------------------------------------------------------- putstore interface
7552:
1.524 raeburn 7553: sub putstore {
1.1172.2.59 raeburn 7554: my ($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog)=@_;
1.620 albertel 7555: if (!$udomain) { $udomain=$env{'user.domain'}; }
7556: if (!$uname) { $uname=$env{'user.name'}; }
1.524 raeburn 7557: my $uhome=&homeserver($uname,$udomain);
7558: my $items='';
1.715 albertel 7559: foreach my $key (keys(%$storehash)) {
7560: $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524 raeburn 7561: }
1.715 albertel 7562: $items=~s/\&$//;
1.716 albertel 7563: my $esc_symb=&escape($symb);
7564: my $esc_v=&escape($version);
1.715 albertel 7565: my $reply =
1.716 albertel 7566: &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715 albertel 7567: $uhome);
1.1172.2.59 raeburn 7568: if (($tolog) && ($reply eq 'ok')) {
7569: my $namevalue='';
7570: foreach my $key (keys(%{$storehash})) {
7571: $namevalue.=&escape($key).'='.&freeze_escape($storehash->{$key}).'&';
7572: }
1.1172.2.134 raeburn 7573: my $ip = &get_requestor_ip();
1.1172.2.135 raeburn 7574: $namevalue .= 'ip='.&escape($ip).
1.1172.2.59 raeburn 7575: '&host='.&escape($perlvar{'lonHostID'}).
7576: '&version='.$esc_v.
7577: '&by='.&escape($env{'user.name'}.':'.$env{'user.domain'});
7578: &Apache::lonnet::courselog($symb.':'.$uname.':'.$udomain.':PUTSTORE:'.$namevalue);
7579: }
1.715 albertel 7580: if ($reply eq 'unknown_cmd') {
1.716 albertel 7581: # gfall back to way things use to be done
1.715 albertel 7582: return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
7583: $uname);
1.524 raeburn 7584: }
1.715 albertel 7585: return $reply;
7586: }
7587:
7588: sub old_putstore {
1.716 albertel 7589: my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
7590: if (!$udomain) { $udomain=$env{'user.domain'}; }
7591: if (!$uname) { $uname=$env{'user.name'}; }
7592: my $uhome=&homeserver($uname,$udomain);
7593: my %newstorehash;
1.800 albertel 7594: foreach my $item (keys(%$storehash)) {
7595: my $key = $version.':'.&escape($symb).':'.$item;
7596: $newstorehash{$key} = $storehash->{$item};
1.716 albertel 7597: }
7598: my $items='';
7599: my %allitems = ();
1.800 albertel 7600: foreach my $item (keys(%newstorehash)) {
7601: if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
1.716 albertel 7602: my $key = $1.':keys:'.$2;
7603: $allitems{$key} .= $3.':';
7604: }
1.800 albertel 7605: $items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
1.716 albertel 7606: }
1.800 albertel 7607: foreach my $item (keys(%allitems)) {
7608: $allitems{$item} =~ s/\:$//;
7609: $items.= $item.'='.$allitems{$item}.'&';
1.716 albertel 7610: }
7611: $items=~s/\&$//;
7612: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524 raeburn 7613: }
7614:
1.47 www 7615: # ------------------------------------------------------ critical put interface
7616:
7617: sub cput {
1.134 albertel 7618: my ($namespace,$storehash,$udomain,$uname)=@_;
1.620 albertel 7619: if (!$udomain) { $udomain=$env{'user.domain'}; }
7620: if (!$uname) { $uname=$env{'user.name'}; }
1.134 albertel 7621: my $uhome=&homeserver($uname,$udomain);
1.47 www 7622: my $items='';
1.800 albertel 7623: foreach my $item (keys(%$storehash)) {
7624: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191 harris41 7625: }
1.47 www 7626: $items=~s/\&$//;
1.134 albertel 7627: return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 7628: }
7629:
7630: # -------------------------------------------------------------- eget interface
7631:
7632: sub eget {
1.133 albertel 7633: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 7634: my $items='';
1.800 albertel 7635: foreach my $item (@$storearr) {
7636: $items.=&escape($item).'&';
1.191 harris41 7637: }
1.12 www 7638: $items=~s/\&$//;
1.620 albertel 7639: if (!$udomain) { $udomain=$env{'user.domain'}; }
7640: if (!$uname) { $uname=$env{'user.name'}; }
1.133 albertel 7641: my $uhome=&homeserver($uname,$udomain);
7642: my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 7643: my @pairs=split(/\&/,$rep);
7644: my %returnhash=();
1.42 www 7645: my $i=0;
1.800 albertel 7646: foreach my $item (@$storearr) {
7647: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42 www 7648: $i++;
1.191 harris41 7649: }
1.12 www 7650: return %returnhash;
7651: }
7652:
1.667 albertel 7653: # ------------------------------------------------------------ tmpput interface
7654: sub tmpput {
1.802 raeburn 7655: my ($storehash,$server,$context)=@_;
1.667 albertel 7656: my $items='';
1.800 albertel 7657: foreach my $item (keys(%$storehash)) {
7658: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.667 albertel 7659: }
7660: $items=~s/\&$//;
1.802 raeburn 7661: if (defined($context)) {
7662: $items .= ':'.&escape($context);
7663: }
1.667 albertel 7664: return &reply("tmpput:$items",$server);
7665: }
7666:
7667: # ------------------------------------------------------------ tmpget interface
7668: sub tmpget {
1.688 albertel 7669: my ($token,$server)=@_;
7670: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
7671: my $rep=&reply("tmpget:$token",$server);
1.667 albertel 7672: my %returnhash;
1.1172.2.85 raeburn 7673: if ($rep =~ /^(con_lost|error|no_such_host)/i) {
7674: return %returnhash;
7675: }
1.667 albertel 7676: foreach my $item (split(/\&/,$rep)) {
7677: my ($key,$value)=split(/=/,$item);
7678: $returnhash{&unescape($key)}=&thaw_unescape($value);
7679: }
7680: return %returnhash;
7681: }
7682:
1.1113 raeburn 7683: # ------------------------------------------------------------ tmpdel interface
1.688 albertel 7684: sub tmpdel {
7685: my ($token,$server)=@_;
7686: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
7687: return &reply("tmpdel:$token",$server);
7688: }
7689:
1.1172.2.13 raeburn 7690: # ------------------------------------------------------------ get_timebased_id
7691:
7692: sub get_timebased_id {
7693: my ($prefix,$keyid,$namespace,$cdom,$cnum,$idtype,$who,$locktries,
7694: $maxtries) = @_;
7695: my ($newid,$error,$dellock);
7696: unless (($prefix =~ /^\w+$/) && ($keyid =~ /^\w+$/) && ($namespace ne '')) {
7697: return ('','ok','invalid call to get suffix');
7698: }
7699:
7700: # set defaults for any optional args for which values were not supplied
7701: if ($who eq '') {
7702: $who = $env{'user.name'}.':'.$env{'user.domain'};
7703: }
7704: if (!$locktries) {
7705: $locktries = 3;
7706: }
7707: if (!$maxtries) {
7708: $maxtries = 10;
7709: }
7710:
7711: if (($cdom eq '') || ($cnum eq '')) {
7712: if ($env{'request.course.id'}) {
7713: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
7714: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
7715: }
7716: if (($cdom eq '') || ($cnum eq '')) {
7717: return ('','ok','call to get suffix not in course context');
7718: }
7719: }
7720:
7721: # construct locking item
7722: my $lockhash = {
7723: $prefix."\0".'locked_'.$keyid => $who,
7724: };
7725: my $tries = 0;
7726:
7727: # attempt to get lock on nohist_$namespace file
7728: my $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
7729: while (($gotlock ne 'ok') && $tries <$locktries) {
7730: $tries ++;
7731: sleep 1;
7732: $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
7733: }
7734:
7735: # attempt to get unique identifier, based on current timestamp
7736: if ($gotlock eq 'ok') {
7737: my %inuse = &Apache::lonnet::dump('nohist_'.$namespace,$cdom,$cnum,$prefix);
7738: my $id = time;
7739: $newid = $id;
1.1172.2.56 raeburn 7740: if ($idtype eq 'addcode') {
7741: $newid .= &sixnum_code();
7742: }
1.1172.2.13 raeburn 7743: my $idtries = 0;
7744: while (exists($inuse{$prefix."\0".$newid}) && $idtries < $maxtries) {
7745: if ($idtype eq 'concat') {
7746: $newid = $id.$idtries;
1.1172.2.56 raeburn 7747: } elsif ($idtype eq 'addcode') {
7748: $newid = $newid.&sixnum_code();
1.1172.2.13 raeburn 7749: } else {
7750: $newid ++;
7751: }
7752: $idtries ++;
7753: }
7754: if (!exists($inuse{$prefix."\0".$newid})) {
7755: my %new_item = (
7756: $prefix."\0".$newid => $who,
7757: );
7758: my $putresult = &Apache::lonnet::put('nohist_'.$namespace,\%new_item,
7759: $cdom,$cnum);
7760: if ($putresult ne 'ok') {
7761: undef($newid);
7762: $error = 'error saving new item: '.$putresult;
7763: }
7764: } else {
1.1172.2.56 raeburn 7765: undef($newid);
1.1172.2.13 raeburn 7766: $error = ('error: no unique suffix available for the new item ');
7767: }
7768: # remove lock
7769: my @del_lock = ($prefix."\0".'locked_'.$keyid);
7770: $dellock = &Apache::lonnet::del('nohist_'.$namespace,\@del_lock,$cdom,$cnum);
7771: } else {
7772: $error = "error: could not obtain lockfile\n";
7773: $dellock = 'ok';
1.1172.2.58 raeburn 7774: if (($prefix eq 'paste') && ($namespace eq 'courseeditor') && ($keyid eq 'num')) {
7775: $dellock = 'nolock';
7776: }
1.1172.2.13 raeburn 7777: }
7778: return ($newid,$dellock,$error);
7779: }
7780:
1.1172.2.56 raeburn 7781: sub sixnum_code {
7782: my $code;
7783: for (0..6) {
7784: $code .= int( rand(9) );
7785: }
7786: return $code;
7787: }
7788:
1.765 albertel 7789: # -------------------------------------------------- portfolio access checking
7790:
7791: sub portfolio_access {
1.1172.2.77 raeburn 7792: my ($requrl,$clientip) = @_;
1.765 albertel 7793: my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
1.1172.2.77 raeburn 7794: my $result = &get_portfolio_access($udom,$unum,$file_name,$group,$clientip);
1.814 raeburn 7795: if ($result) {
7796: my %setters;
7797: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
1.1172.2.142 raeburn 7798: my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) =
7799: &Apache::loncommon::blockcheck(\%setters,'port',$clientip,$unum,$udom);
7800: if (($startblock && $endblock) || ($by_ip)) {
1.814 raeburn 7801: return 'B';
7802: }
7803: } else {
1.1172.2.142 raeburn 7804: my ($startblock,$endblock,$triggerblock,$by_ip,$blockdo) =
7805: &Apache::loncommon::blockcheck(\%setters,'port',$clientip);
7806: if (($startblock && $endblock) || ($by_ip)) {
1.814 raeburn 7807: return 'B';
7808: }
7809: }
7810: }
1.765 albertel 7811: if ($result eq 'ok') {
1.766 albertel 7812: return 'F';
1.765 albertel 7813: } elsif ($result =~ /^[^:]+:guest_/) {
1.766 albertel 7814: return 'A';
1.765 albertel 7815: }
1.766 albertel 7816: return '';
1.765 albertel 7817: }
7818:
7819: sub get_portfolio_access {
1.1172.2.146. .23(raeb 7820:-24): my ($udom,$unum,$file_name,$group,$clientip,$access_hash,$portaccessref) = @_;
1.767 albertel 7821:
7822: if (!ref($access_hash)) {
7823: my $current_perms = &get_portfile_permissions($udom,$unum);
7824: my %access_controls = &get_access_controls($current_perms,$group,
7825: $file_name);
7826: $access_hash = $access_controls{$file_name};
7827: }
7828:
1.1172.2.146. .23(raeb 7829:-24): my $portaccess;
7830:-24): if (ref($portaccess) eq 'SCALAR') {
7831:-24): $portaccess = $$portaccessref;
7832:-24): } else {
7833:-24): $portaccess = &usertools_access($unum,$udom,'portaccess',undef,'tools');
7834:-24): }
7835:-24):
7836:-24): my ($public,$guest,@domains,@users,@courses,@groups,@ips,@userips);
1.765 albertel 7837: my $now = time;
7838: if (ref($access_hash) eq 'HASH') {
7839: foreach my $key (keys(%{$access_hash})) {
7840: my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
1.1172.2.146. .23(raeb 7841:-24): next if (($scope ne 'ip') && ($portaccess == 0));
1.765 albertel 7842: if ($start > $now) {
7843: next;
7844: }
7845: if ($end && $end<$now) {
7846: next;
7847: }
7848: if ($scope eq 'public') {
7849: $public = $key;
7850: last;
7851: } elsif ($scope eq 'guest') {
7852: $guest = $key;
7853: } elsif ($scope eq 'domains') {
7854: push(@domains,$key);
7855: } elsif ($scope eq 'users') {
7856: push(@users,$key);
7857: } elsif ($scope eq 'course') {
7858: push(@courses,$key);
7859: } elsif ($scope eq 'group') {
7860: push(@groups,$key);
1.1172.2.77 raeburn 7861: } elsif ($scope eq 'ip') {
7862: push(@ips,$key);
1.1172.2.146. .23(raeb 7863:-24): } elsif ($scope eq 'userip') {
7864:-24): push(@userips,$key);
1.765 albertel 7865: }
7866: }
7867: if ($public) {
7868: return 'ok';
1.1172.2.77 raeburn 7869: } elsif (@ips > 0) {
7870: my $allowed;
7871: foreach my $ipkey (@ips) {
7872: if (ref($access_hash->{$ipkey}{'ip'}) eq 'ARRAY') {
7873: if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$ipkey}{'ip'}}),$clientip)) {
7874: $allowed = 1;
7875: last;
7876: }
7877: }
7878: }
7879: if ($allowed) {
7880: return 'ok';
7881: }
1.1172.2.146. .23(raeb 7882:-24): } elsif (@userips > 0) {
7883:-24): my $allowed;
7884:-24): foreach my $useripkey (@userips) {
7885:-24): if (ref($access_hash->{$useripkey}{'ip'}) eq 'ARRAY') {
7886:-24): if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$useripkey}{'ip'}}),$clientip)) {
7887:-24): $allowed = 1;
7888:-24): last;
7889:-24): }
7890:-24): }
7891:-24): }
7892:-24): if ($allowed) {
7893:-24): return 'ok';
7894:-24): }
1.765 albertel 7895: }
7896: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
7897: if ($guest) {
7898: return $guest;
7899: }
7900: } else {
7901: if (@domains > 0) {
7902: foreach my $domkey (@domains) {
7903: if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
7904: if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
7905: return 'ok';
7906: }
7907: }
7908: }
7909: }
7910: if (@users > 0) {
7911: foreach my $userkey (@users) {
1.865 raeburn 7912: if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
7913: foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
7914: if (ref($item) eq 'HASH') {
7915: if (($item->{'uname'} eq $env{'user.name'}) &&
7916: ($item->{'udom'} eq $env{'user.domain'})) {
7917: return 'ok';
7918: }
7919: }
7920: }
7921: }
1.765 albertel 7922: }
7923: }
7924: my %roleshash;
7925: my @courses_and_groups = @courses;
7926: push(@courses_and_groups,@groups);
7927: if (@courses_and_groups > 0) {
7928: my (%allgroups,%allroles);
7929: my ($start,$end,$role,$sec,$group);
7930: foreach my $envkey (%env) {
1.1060 raeburn 7931: if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765 albertel 7932: my $cid = $2.'_'.$3;
7933: if ($1 eq 'gr') {
7934: $group = $4;
7935: $allgroups{$cid}{$group} = $env{$envkey};
7936: } else {
7937: if ($4 eq '') {
7938: $sec = 'none';
7939: } else {
7940: $sec = $4;
7941: }
7942: $allroles{$cid}{$1}{$sec} = $env{$envkey};
7943: }
1.811 albertel 7944: } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765 albertel 7945: my $cid = $2.'_'.$3;
7946: if ($4 eq '') {
7947: $sec = 'none';
7948: } else {
7949: $sec = $4;
7950: }
7951: $allroles{$cid}{$1}{$sec} = $env{$envkey};
7952: }
7953: }
7954: if (keys(%allroles) == 0) {
7955: return;
7956: }
7957: foreach my $key (@courses_and_groups) {
7958: my %content = %{$$access_hash{$key}};
7959: my $cnum = $content{'number'};
7960: my $cdom = $content{'domain'};
7961: my $cid = $cdom.'_'.$cnum;
7962: if (!exists($allroles{$cid})) {
7963: next;
7964: }
7965: foreach my $role_id (keys(%{$content{'roles'}})) {
7966: my @sections = @{$content{'roles'}{$role_id}{'section'}};
7967: my @groups = @{$content{'roles'}{$role_id}{'group'}};
7968: my @status = @{$content{'roles'}{$role_id}{'access'}};
7969: my @roles = @{$content{'roles'}{$role_id}{'role'}};
7970: foreach my $role (keys(%{$allroles{$cid}})) {
7971: if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
7972: foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
7973: if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
7974: if (grep/^all$/,@sections) {
7975: return 'ok';
7976: } else {
7977: if (grep/^$sec$/,@sections) {
7978: return 'ok';
7979: }
7980: }
7981: }
7982: }
7983: if (keys(%{$allgroups{$cid}}) == 0) {
7984: if (grep/^none$/,@groups) {
7985: return 'ok';
7986: }
7987: } else {
7988: if (grep/^all$/,@groups) {
7989: return 'ok';
7990: }
7991: foreach my $group (keys(%{$allgroups{$cid}})) {
7992: if (grep/^$group$/,@groups) {
7993: return 'ok';
7994: }
7995: }
7996: }
7997: }
7998: }
7999: }
8000: }
8001: }
8002: if ($guest) {
8003: return $guest;
8004: }
8005: }
8006: }
8007: return;
8008: }
8009:
8010: sub course_group_datechecker {
8011: my ($dates,$now,$status) = @_;
8012: my ($start,$end) = split(/\./,$dates);
8013: if (!$start && !$end) {
8014: return 'ok';
8015: }
8016: if (grep/^active$/,@{$status}) {
8017: if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
8018: return 'ok';
8019: }
8020: }
8021: if (grep/^previous$/,@{$status}) {
8022: if ($end > $now ) {
8023: return 'ok';
8024: }
8025: }
8026: if (grep/^future$/,@{$status}) {
8027: if ($start > $now) {
8028: return 'ok';
8029: }
8030: }
8031: return;
8032: }
8033:
8034: sub parse_portfolio_url {
8035: my ($url) = @_;
8036:
8037: my ($type,$udom,$unum,$group,$file_name);
8038:
1.823 albertel 8039: if ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
1.765 albertel 8040: $type = 1;
8041: $udom = $1;
8042: $unum = $2;
8043: $file_name = $3;
1.823 albertel 8044: } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
1.765 albertel 8045: $type = 2;
8046: $udom = $1;
8047: $unum = $2;
8048: $group = $3;
8049: $file_name = $3.'/'.$4;
8050: }
8051: if (wantarray) {
8052: return ($type,$udom,$unum,$file_name,$group);
8053: }
8054: return $type;
8055: }
8056:
8057: sub is_portfolio_url {
8058: my ($url) = @_;
8059: return scalar(&parse_portfolio_url($url));
8060: }
8061:
1.798 raeburn 8062: sub is_portfolio_file {
8063: my ($file) = @_;
1.820 raeburn 8064: if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
1.798 raeburn 8065: return 1;
8066: }
8067: return;
8068: }
8069:
1.1172.2.146. .13(raeb 8070:-23): sub is_coursetool_logo {
8071:-23): my ($uri) = @_;
8072:-23): if ($env{'request.course.id'}) {
8073:-23): my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
8074:-23): if ($uri =~ m{^/*uploaded\Q$courseurl\E/toollogo/\d+/[^/]+$}) {
8075:-23): return 1;
8076:-23): }
8077:-23): }
8078:-23): return;
8079:-23): }
8080:-23):
1.976 raeburn 8081: sub usertools_access {
1.1084 raeburn 8082: my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
1.985 raeburn 8083: my ($access,%tools);
8084: if ($context eq '') {
8085: $context = 'tools';
8086: }
8087: if ($context eq 'requestcourses') {
8088: %tools = (
8089: official => 1,
8090: unofficial => 1,
1.1006 raeburn 8091: community => 1,
1.1172.2.37 raeburn 8092: textbook => 1,
1.1172.2.146. .13(raeb 8093:-23): lti => 1,
1.985 raeburn 8094: );
1.1172.2.9 raeburn 8095: } elsif ($context eq 'requestauthor') {
8096: %tools = (
8097: requestauthor => 1,
8098: );
1.1172.2.146. .18(raeb 8099:-24): } elsif ($context eq 'authordefaults') {
8100:-24): %tools = (
8101:-24): webdav => 1,
8102:-24): );
1.985 raeburn 8103: } else {
8104: %tools = (
8105: aboutme => 1,
8106: blog => 1,
1.1172.2.146. .24(raeb 8107:-24): webdav => 1,
1.985 raeburn 8108: portfolio => 1,
1.1172.2.146. .22(raeb 8109:-24): portaccess => 1,
.6(raebu 8110:22): timezone => 1,
1.985 raeburn 8111: );
8112: }
1.976 raeburn 8113: return if (!defined($tools{$tool}));
8114:
1.1172.2.35 raeburn 8115: if (($udom eq '') || ($uname eq '')) {
1.976 raeburn 8116: $udom = $env{'user.domain'};
8117: $uname = $env{'user.name'};
8118: }
8119:
1.978 raeburn 8120: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
8121: if ($action ne 'reload') {
1.985 raeburn 8122: if ($context eq 'requestcourses') {
8123: return $env{'environment.canrequest.'.$tool};
1.1172.2.9 raeburn 8124: } elsif ($context eq 'requestauthor') {
8125: return $env{'environment.canrequest.author'};
1.1172.2.146. .18(raeb 8126:-24): } elsif ($context eq 'authordefaults') {
8127:-24): if ($tool eq 'webdav') {
8128:-24): return $env{'environment.availabletools.'.$tool};
8129:-24): }
1.985 raeburn 8130: } else {
8131: return $env{'environment.availabletools.'.$tool};
8132: }
8133: }
1.976 raeburn 8134: }
8135:
1.1172.2.9 raeburn 8136: my ($toolstatus,$inststatus,$envkey);
8137: if ($context eq 'requestauthor') {
8138: $envkey = $context;
1.1172.2.146. .18(raeb 8139:-24): } elsif ($context eq 'authordefaults') {
8140:-24): if ($tool eq 'webdav') {
8141:-24): $envkey = 'tools.'.$tool;
8142:-24): }
1.1172.2.9 raeburn 8143: } else {
8144: $envkey = $context.'.'.$tool;
8145: }
1.976 raeburn 8146:
1.985 raeburn 8147: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
8148: ($action ne 'reload')) {
1.1172.2.9 raeburn 8149: $toolstatus = $env{'environment.'.$envkey};
1.976 raeburn 8150: $inststatus = $env{'environment.inststatus'};
8151: } else {
1.1084 raeburn 8152: if (ref($userenvref) eq 'HASH') {
1.1172.2.9 raeburn 8153: $toolstatus = $userenvref->{$envkey};
1.1084 raeburn 8154: $inststatus = $userenvref->{'inststatus'};
8155: } else {
1.1172.2.9 raeburn 8156: my %userenv = &userenvironment($udom,$uname,$envkey,'inststatus');
8157: $toolstatus = $userenv{$envkey};
1.1084 raeburn 8158: $inststatus = $userenv{'inststatus'};
8159: }
1.976 raeburn 8160: }
8161:
8162: if ($toolstatus ne '') {
8163: if ($toolstatus) {
8164: $access = 1;
8165: } else {
8166: $access = 0;
8167: }
8168: return $access;
8169: }
8170:
1.1084 raeburn 8171: my ($is_adv,%domdef);
8172: if (ref($is_advref) eq 'HASH') {
8173: $is_adv = $is_advref->{'is_adv'};
8174: } else {
8175: $is_adv = &is_advanced_user($udom,$uname);
8176: }
8177: if (ref($domdefref) eq 'HASH') {
8178: %domdef = %{$domdefref};
8179: } else {
8180: %domdef = &get_domain_defaults($udom);
8181: }
1.976 raeburn 8182: if (ref($domdef{$tool}) eq 'HASH') {
8183: if ($is_adv) {
8184: if ($domdef{$tool}{'_LC_adv'} ne '') {
8185: if ($domdef{$tool}{'_LC_adv'}) {
8186: $access = 1;
8187: } else {
8188: $access = 0;
8189: }
8190: return $access;
8191: }
8192: }
8193: if ($inststatus ne '') {
8194: my ($hasaccess,$hasnoaccess);
8195: foreach my $affiliation (split(/:/,$inststatus)) {
8196: if ($domdef{$tool}{$affiliation} ne '') {
8197: if ($domdef{$tool}{$affiliation}) {
8198: $hasaccess = 1;
8199: } else {
8200: $hasnoaccess = 1;
8201: }
8202: }
8203: }
8204: if ($hasaccess || $hasnoaccess) {
8205: if ($hasaccess) {
8206: $access = 1;
8207: } elsif ($hasnoaccess) {
8208: $access = 0;
8209: }
8210: return $access;
8211: }
8212: } else {
8213: if ($domdef{$tool}{'default'} ne '') {
8214: if ($domdef{$tool}{'default'}) {
8215: $access = 1;
8216: } elsif ($domdef{$tool}{'default'} == 0) {
8217: $access = 0;
8218: }
8219: return $access;
8220: }
8221: }
8222: } else {
1.1172.2.6 raeburn 8223: if (($context eq 'tools') && ($tool ne 'webdav')) {
1.985 raeburn 8224: $access = 1;
8225: } else {
8226: $access = 0;
8227: }
1.976 raeburn 8228: return $access;
8229: }
8230: }
8231:
1.1050 raeburn 8232: sub is_course_owner {
8233: my ($cdom,$cnum,$udom,$uname) = @_;
8234: if (($udom eq '') || ($uname eq '')) {
8235: $udom = $env{'user.domain'};
8236: $uname = $env{'user.name'};
8237: }
8238: unless (($udom eq '') || ($uname eq '')) {
8239: if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
8240: if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
8241: return 1;
8242: } else {
8243: my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
8244: if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
8245: return 1;
8246: }
8247: }
8248: }
8249: }
8250: return;
8251: }
8252:
1.976 raeburn 8253: sub is_advanced_user {
8254: my ($udom,$uname) = @_;
1.1085 raeburn 8255: if ($udom ne '' && $uname ne '') {
8256: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
1.1128 raeburn 8257: if (wantarray) {
8258: return ($env{'user.adv'},$env{'user.author'});
8259: } else {
8260: return $env{'user.adv'};
8261: }
1.1085 raeburn 8262: }
8263: }
1.976 raeburn 8264: my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
8265: my %allroles;
1.1128 raeburn 8266: my ($is_adv,$is_author);
1.976 raeburn 8267: foreach my $role (keys(%roleshash)) {
8268: my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
8269: my $area = '/'.$tdomain.'/'.$trest;
8270: if ($sec ne '') {
8271: $area .= '/'.$sec;
8272: }
8273: if (($area ne '') && ($trole ne '')) {
8274: my $spec=$trole.'.'.$area;
8275: if ($trole =~ /^cr\//) {
8276: &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
8277: } elsif ($trole ne 'gr') {
8278: &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
8279: }
1.1128 raeburn 8280: if ($trole eq 'au') {
8281: $is_author = 1;
8282: }
1.976 raeburn 8283: }
8284: }
8285: foreach my $role (keys(%allroles)) {
8286: last if ($is_adv);
8287: foreach my $item (split(/:/,$allroles{$role})) {
8288: if ($item ne '') {
8289: my ($privilege,$restrictions)=split(/&/,$item);
8290: if ($privilege eq 'adv') {
8291: $is_adv = 1;
8292: last;
8293: }
8294: }
8295: }
8296: }
1.1128 raeburn 8297: if (wantarray) {
8298: return ($is_adv,$is_author);
8299: }
1.976 raeburn 8300: return $is_adv;
8301: }
1.798 raeburn 8302:
1.1035 raeburn 8303: sub check_can_request {
1.1172.2.146. .13(raeb 8304:-23): my ($dom,$can_request,$request_domains,$uname,$udom) = @_;
1.1035 raeburn 8305: my $canreq = 0;
1.1172.2.146. .13(raeb 8306:-23): if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
8307:-23): $uname = $env{'user.name'};
8308:-23): $udom = $env{'user.domain'};
8309:-23): }
1.1035 raeburn 8310: my ($types,$typename) = &Apache::loncommon::course_types();
8311: my @options = ('approval','validate','autolimit');
8312: my $optregex = join('|',@options);
8313: if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
8314: foreach my $type (@{$types}) {
1.1172.2.146. .13(raeb 8315:-23): if (&usertools_access($uname,$udom,$type,undef,
8316:-23): 'requestcourses')) {
1.1035 raeburn 8317: $canreq ++;
1.1036 raeburn 8318: if (ref($request_domains) eq 'HASH') {
1.1172.2.146. .13(raeb 8319:-23): push(@{$request_domains->{$type}},$udom);
1.1036 raeburn 8320: }
1.1172.2.146. .13(raeb 8321:-23): if ($dom eq $udom) {
1.1035 raeburn 8322: $can_request->{$type} = 1;
8323: }
8324: }
1.1172.2.146. .13(raeb 8325:-23): if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '') &&
8326:-23): ($env{'environment.reqcrsotherdom.'.$type} ne '')) {
1.1035 raeburn 8327: my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
8328: if (@curr > 0) {
1.1036 raeburn 8329: foreach my $item (@curr) {
8330: if (ref($request_domains) eq 'HASH') {
8331: my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
8332: if ($otherdom ne '') {
8333: if (ref($request_domains->{$type}) eq 'ARRAY') {
8334: unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
8335: push(@{$request_domains->{$type}},$otherdom);
8336: }
8337: } else {
8338: push(@{$request_domains->{$type}},$otherdom);
8339: }
8340: }
8341: }
8342: }
1.1172.2.146. .13(raeb 8343:-23): unless ($dom eq $env{'user.domain'}) {
1.1036 raeburn 8344: $canreq ++;
1.1035 raeburn 8345: if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
8346: $can_request->{$type} = 1;
8347: }
8348: }
8349: }
8350: }
8351: }
8352: }
8353: return $canreq;
8354: }
8355:
1.341 www 8356: # ---------------------------------------------- Custom access rule evaluation
8357:
8358: sub customaccess {
8359: my ($priv,$uri)=@_;
1.807 albertel 8360: my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819 www 8361: my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807 albertel 8362: $udom = &LONCAPA::clean_domain($udom);
8363: $ucrs = &LONCAPA::clean_username($ucrs);
1.341 www 8364: my $access=0;
1.800 albertel 8365: foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.893 albertel 8366: my ($effect,$realm,$role,$type)=split(/\:/,$right);
8367: if ($type eq 'user') {
8368: foreach my $scope (split(/\s*\,\s*/,$realm)) {
1.896 albertel 8369: my ($tdom,$tuname)=split(m{/},$scope);
1.893 albertel 8370: if ($tdom) {
8371: if ($tdom ne $env{'user.domain'}) { next; }
8372: }
1.896 albertel 8373: if ($tuname) {
8374: if ($tuname ne $env{'user.name'}) { next; }
1.893 albertel 8375: }
8376: $access=($effect eq 'allow');
8377: last;
8378: }
8379: } else {
8380: if ($role) {
8381: if ($role ne $urole) { next; }
8382: }
8383: foreach my $scope (split(/\s*\,\s*/,$realm)) {
8384: my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
8385: if ($tdom) {
8386: if ($tdom ne $udom) { next; }
8387: }
8388: if ($tcrs) {
8389: if ($tcrs ne $ucrs) { next; }
8390: }
8391: if ($tsec) {
8392: if ($tsec ne $usec) { next; }
8393: }
8394: $access=($effect eq 'allow');
8395: last;
8396: }
8397: if ($realm eq '' && $role eq '') {
8398: $access=($effect eq 'allow');
8399: }
1.402 bowersj2 8400: }
1.341 www 8401: }
8402: return $access;
8403: }
8404:
1.103 harris41 8405: # ------------------------------------------------- Check for a user privilege
1.12 www 8406:
8407: sub allowed {
1.1172.2.146. .1(raebu 8408:22): my ($priv,$uri,$symb,$role,$clientip,$noblockcheck,$ignorecache,$nodeeplinkcheck,$nodeeplinkout)=@_;
1.705 albertel 8409: my $ver_orguri=$uri;
1.439 www 8410: $uri=&deversion($uri);
1.152 www 8411: my $orguri=$uri;
1.52 www 8412: $uri=&declutter($uri);
1.809 raeburn 8413:
1.810 raeburn 8414: if ($priv eq 'evb') {
1.1172.2.146. .3(raebu 8415:22): # Evade communication block restrictions for specified role in a course or domain
1.810 raeburn 8416: if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
8417: return $1;
8418: } else {
8419: return;
8420: }
8421: }
8422:
1.620 albertel 8423: if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54 www 8424: # Free bre access to adm and meta resources
1.1172.2.146. .1(raebu 8425:22): if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard|viewclasslist|aboutme|ext\.tool)$}))
1.769 albertel 8426: || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) ))
8427: && ($priv eq 'bre')) {
1.14 www 8428: return 'F';
1.159 www 8429: }
8430:
1.545 banghart 8431: # Free bre access to user's own portfolio contents
1.714 raeburn 8432: my ($space,$domain,$name,@dir)=split('/',$uri);
1.647 raeburn 8433: if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) &&
1.714 raeburn 8434: ($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814 raeburn 8435: my %setters;
1.1172.2.142 raeburn 8436: my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) =
8437: &Apache::loncommon::blockcheck(\%setters,'port',$clientip);
8438: if (($startblock && $endblock) || ($by_ip)) {
1.814 raeburn 8439: return 'B';
8440: } else {
8441: return 'F';
8442: }
1.545 banghart 8443: }
8444:
1.762 raeburn 8445: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714 raeburn 8446: if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups')
8447: && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
8448: if (exists($env{'request.course.id'})) {
8449: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
8450: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
8451: if (($domain eq $cdom) && ($name eq $cnum)) {
8452: my $courseprivid=$env{'request.course.id'};
8453: $courseprivid=~s/\_/\//;
8454: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
8455: .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
8456: return $1;
1.762 raeburn 8457: } else {
8458: if ($env{'request.course.sec'}) {
8459: $courseprivid.='/'.$env{'request.course.sec'};
8460: }
8461: if ($env{'user.priv.'.$env{'request.role'}.'./'.
8462: $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
8463: return $2;
8464: }
1.714 raeburn 8465: }
8466: }
8467: }
8468: }
8469:
1.159 www 8470: # Free bre to public access
8471:
8472: if ($priv eq 'bre') {
1.1172.2.146. .1(raebu 8473:22): my $copyright;
8474:22): unless ($uri =~ /ext\.tool/) {
8475:22): $copyright=&metadata($uri,'copyright');
8476:22): }
1.620 albertel 8477: if (($copyright eq 'public') && (!$env{'request.course.id'})) {
1.301 www 8478: return 'F';
8479: }
1.238 www 8480: if ($copyright eq 'priv') {
8481: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 8482: unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238 www 8483: return '';
8484: }
8485: }
8486: if ($copyright eq 'domain') {
8487: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 8488: unless (($env{'user.domain'} eq $1) ||
8489: ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238 www 8490: return '';
8491: }
1.262 matthew 8492: }
1.620 albertel 8493: if ($env{'request.role'}=~ /li\.\//) {
1.262 matthew 8494: # Library role, so allow browsing of resources in this domain.
8495: return 'F';
1.238 www 8496: }
1.341 www 8497: if ($copyright eq 'custom') {
8498: unless (&customaccess($priv,$uri)) { return ''; }
8499: }
1.14 www 8500: }
1.264 matthew 8501: # Domain coordinator is trying to create a course
1.620 albertel 8502: if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264 matthew 8503: # uri is the requested domain in this case.
8504: # comparison to 'request.role.domain' shows if the user has selected
1.678 raeburn 8505: # a role of dc for the domain in question.
1.620 albertel 8506: return 'F' if ($uri eq $env{'request.role.domain'});
1.264 matthew 8507: }
1.29 www 8508:
1.52 www 8509: my $thisallowed='';
8510: my $statecond=0;
8511: my $courseprivid='';
8512:
1.1039 raeburn 8513: my $ownaccess;
1.1043 raeburn 8514: # Community Coordinator or Assistant Co-author browsing resource space.
1.1039 raeburn 8515: if (($priv eq 'bro') && ($env{'user.author'})) {
8516: if ($uri eq '') {
8517: $ownaccess = 1;
8518: } else {
8519: if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
8520: my $udom = $env{'user.domain'};
8521: my $uname = $env{'user.name'};
8522: if ($uri =~ m{^\Q$udom\E/?$}) {
8523: $ownaccess = 1;
1.1040 raeburn 8524: } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
1.1039 raeburn 8525: unless ($uri =~ m{\.\./}) {
8526: $ownaccess = 1;
8527: }
8528: } elsif (($udom ne 'public') && ($uname ne 'public')) {
8529: my $now = time;
8530: if ($uri =~ m{^([^/]+)/?$}) {
8531: my $adom = $1;
8532: foreach my $key (keys(%env)) {
1.1042 raeburn 8533: if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
1.1172.2.142 raeburn 8534: my ($start,$end) = split(/\./,$env{$key});
8535: if (($now >= $start) && (!$end || $end > $now)) {
1.1039 raeburn 8536: $ownaccess = 1;
8537: last;
8538: }
8539: }
8540: }
8541: } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
8542: my $adom = $1;
8543: my $aname = $2;
1.1042 raeburn 8544: foreach my $role ('ca','aa') {
8545: if ($env{"user.role.$role./$adom/$aname"}) {
8546: my ($start,$end) =
1.1172.2.142 raeburn 8547: split(/\./,$env{"user.role.$role./$adom/$aname"});
8548: if (($now >= $start) && (!$end || $end > $now)) {
1.1042 raeburn 8549: $ownaccess = 1;
8550: last;
8551: }
1.1039 raeburn 8552: }
8553: }
8554: }
8555: }
8556: }
8557: }
8558: }
8559:
1.52 www 8560: # Course
8561:
1.620 albertel 8562: if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 8563: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 8564: $thisallowed.=$1;
8565: }
1.52 www 8566: }
1.29 www 8567:
1.52 www 8568: # Domain
8569:
1.620 albertel 8570: if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479 albertel 8571: =~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 8572: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 8573: $thisallowed.=$1;
8574: }
1.12 www 8575: }
1.52 www 8576:
1.1141 raeburn 8577: # User who is not author or co-author might still be able to edit
8578: # resource of an author in the domain (e.g., if Domain Coordinator).
8579: if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
8580: (&allowed('mdc',$env{'request.course.id'}))) {
8581: if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
8582: $thisallowed.=$1;
8583: }
8584: }
8585:
1.52 www 8586: # Course: uri itself is a course
1.66 www 8587: my $courseuri=$uri;
8588: $courseuri=~s/\_(\d)/\/$1/;
1.83 www 8589: $courseuri=~s/^([^\/])/\/$1/;
1.81 www 8590:
1.620 albertel 8591: if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479 albertel 8592: =~/\Q$priv\E\&([^\:]*)/) {
1.1172.2.115 raeburn 8593: if ($priv eq 'mip') {
8594: my $rem = $1;
8595: if (($uri ne '') && ($env{'request.course.id'} eq $uri) &&
8596: ($env{'course.'.$env{'request.course.id'}.'.internal.courseowner'} eq $env{'user.name'}.':'.$env{'user.domain'})) {
8597: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
8598: if ($cdom ne '') {
8599: my %passwdconf = &get_passwdconf($cdom);
8600: if (ref($passwdconf{'crsownerchg'}) eq 'HASH') {
8601: if (ref($passwdconf{'crsownerchg'}{'by'}) eq 'ARRAY') {
8602: if (@{$passwdconf{'crsownerchg'}{'by'}}) {
8603: my @inststatuses = split(':',$env{'environment.inststatus'});
8604: unless (@inststatuses) {
8605: @inststatuses = ('default');
8606: }
8607: foreach my $status (@inststatuses) {
8608: if (grep(/^\Q$status\E$/,@{$passwdconf{'crsownerchg'}{'by'}})) {
8609: $thisallowed.=$rem;
8610: }
8611: }
8612: }
8613: }
8614: }
8615: }
8616: }
8617: } else {
8618: unless (($priv eq 'bro') && (!$ownaccess)) {
8619: $thisallowed.=$1;
8620: }
1.1039 raeburn 8621: }
1.12 www 8622: }
1.29 www 8623:
1.665 albertel 8624: # URI is an uploaded document for this course, default permissions don't matter
1.611 albertel 8625: # not allowing 'edit' access (editupload) to uploaded course docs
1.492 albertel 8626: if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665 albertel 8627: $thisallowed='';
1.671 raeburn 8628: my ($match)=&is_on_map($uri);
8629: if ($match) {
8630: if ($env{'user.priv.'.$env{'request.role'}.'./'}
8631: =~/\Q$priv\E\&([^\:]*)/) {
1.1172.2.65 raeburn 8632: my $value = $1;
1.1172.2.146. .1(raebu 8633:22): my $deeplinkblock;
8634:22): unless ($nodeeplinkcheck) {
8635:22): $deeplinkblock = &deeplink_check($priv,$symb,$uri);
8636:22): }
8637:22): if ($deeplinkblock) {
8638:22): $thisallowed='D';
8639:22): } elsif ($noblockcheck) {
1.1172.2.65 raeburn 8640: $thisallowed.=$value;
1.1162 raeburn 8641: } else {
1.1172.2.126 raeburn 8642: my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
1.1172.2.65 raeburn 8643: if (@blockers > 0) {
8644: $thisallowed = 'B';
8645: } else {
8646: $thisallowed.=$value;
8647: }
1.1162 raeburn 8648: }
1.671 raeburn 8649: }
8650: } else {
1.705 albertel 8651: my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671 raeburn 8652: if ($refuri) {
8653: if ($refuri =~ m|^/adm/|) {
1.669 raeburn 8654: $thisallowed='F';
1.671 raeburn 8655: } else {
8656: $refuri=&declutter($refuri);
8657: my ($match) = &is_on_map($refuri);
8658: if ($match) {
1.1172.2.146. .1(raebu 8659:22): my $deeplinkblock;
8660:22): unless ($nodeeplinkcheck) {
8661:22): $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
8662:22): }
8663:22): if ($deeplinkblock) {
8664:22): $thisallowed='D';
8665:22): } elsif ($noblockcheck) {
1.1162 raeburn 8666: $thisallowed='F';
1.1172.2.65 raeburn 8667: } else {
1.1172.2.127 raeburn 8668: my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
1.1172.2.65 raeburn 8669: if (@blockers > 0) {
8670: $thisallowed = 'B';
8671: } else {
8672: $thisallowed='F';
8673: }
1.1162 raeburn 8674: }
1.671 raeburn 8675: }
1.669 raeburn 8676: }
1.671 raeburn 8677: }
8678: }
1.314 www 8679: }
1.492 albertel 8680:
1.766 albertel 8681: if ($priv eq 'bre'
8682: && $thisallowed ne 'F'
8683: && $thisallowed ne '2'
8684: && &is_portfolio_url($uri)) {
1.1172.2.77 raeburn 8685: $thisallowed = &portfolio_access($uri,$clientip);
1.766 albertel 8686: }
8687:
1.52 www 8688: # Full access at system, domain or course-wide level? Exit.
1.29 www 8689: if ($thisallowed=~/F/) {
8690: return 'F';
8691: }
8692:
1.52 www 8693: # If this is generating or modifying users, exit with special codes
1.29 www 8694:
1.1172.2.146. .22(raeb 8695:-24): if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa::vca:vaa:'=~/\:\Q$priv\E\:/) {
1.643 www 8696: if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642 albertel 8697: my ($audom,$auname)=split('/',$uri);
1.643 www 8698: # no author name given, so this just checks on the general right to make a co-author in this domain
8699: unless ($auname) { return $thisallowed; }
8700: # an author name is given, so we are about to actually make a co-author for a certain account
1.642 albertel 8701: if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
8702: (($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
8703: ($audom ne $env{'request.role.domain'}))) { return ''; }
1.1172.2.146. .22(raeb 8704:-24): } elsif (($priv eq 'vca') || ($priv eq 'vaa')) {
8705:-24): my ($audom,$auname)=split('/',$uri);
8706:-24): unless ($auname) { return $thisallowed; }
8707:-24): unless (($env{'request.role'} eq "dc./$audom") ||
8708:-24): ($env{'request.role'} eq "ca./$uri")) {
8709:-24): return '';
8710:-24): }
1.642 albertel 8711: }
1.52 www 8712: return $thisallowed;
8713: }
8714: #
1.103 harris41 8715: # Gathered so far: system, domain and course wide privileges
1.52 www 8716: #
8717: # Course: See if uri or referer is an individual resource that is part of
8718: # the course
8719:
1.620 albertel 8720: if ($env{'request.course.id'}) {
1.232 www 8721:
1.1172.2.146. .13(raeb 8722:-23): if ($priv eq 'bre') {
8723:-23): if (&is_coursetool_logo($uri)) {
8724:-23): return 'F';
8725:-23): }
8726:-23): }
8727:-23):
1.1172.2.115 raeburn 8728: # If this is modifying password (internal auth) domains must match for user and user's role.
8729:
8730: if ($priv eq 'mip') {
8731: if ($env{'user.domain'} eq $env{'request.role.domain'}) {
8732: return $thisallowed;
8733: } else {
8734: return '';
8735: }
8736: }
8737:
1.620 albertel 8738: $courseprivid=$env{'request.course.id'};
8739: if ($env{'request.course.sec'}) {
8740: $courseprivid.='/'.$env{'request.course.sec'};
1.52 www 8741: }
8742: $courseprivid=~s/\_/\//;
8743: my $checkreferer=1;
1.232 www 8744: my ($match,$cond)=&is_on_map($uri);
8745: if ($match) {
8746: $statecond=$cond;
1.620 albertel 8747: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 8748: =~/\Q$priv\E\&([^\:]*)/) {
1.1162 raeburn 8749: my $value = $1;
8750: if ($priv eq 'bre') {
1.1172.2.146. .1(raebu 8751:22): my $deeplinkblock;
8752:22): unless ($nodeeplinkcheck) {
8753:22): $deeplinkblock = &deeplink_check($priv,$symb,$uri);
8754:22): }
8755:22): if ($deeplinkblock) {
8756:22): $thisallowed = 'D';
8757:22): } elsif ($noblockcheck) {
1.1162 raeburn 8758: $thisallowed.=$value;
1.1172.2.65 raeburn 8759: } else {
1.1172.2.126 raeburn 8760: my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
1.1172.2.65 raeburn 8761: if (@blockers > 0) {
8762: $thisallowed = 'B';
8763: } else {
8764: $thisallowed.=$value;
8765: }
1.1162 raeburn 8766: }
8767: } else {
8768: $thisallowed.=$value;
8769: }
1.52 www 8770: $checkreferer=0;
8771: }
1.29 www 8772: }
1.1172.2.126 raeburn 8773:
1.148 www 8774: if ($checkreferer) {
1.620 albertel 8775: my $refuri=$env{'httpref.'.$orguri};
1.148 www 8776: unless ($refuri) {
1.800 albertel 8777: foreach my $key (keys(%env)) {
8778: if ($key=~/^httpref\..*\*/) {
8779: my $pattern=$key;
1.156 www 8780: $pattern=~s/^httpref\.\/res\///;
1.148 www 8781: $pattern=~s/\*/\[\^\/\]\+/g;
8782: $pattern=~s/\//\\\//g;
1.152 www 8783: if ($orguri=~/$pattern/) {
1.800 albertel 8784: $refuri=$env{$key};
1.148 www 8785: }
8786: }
1.191 harris41 8787: }
1.148 www 8788: }
1.232 www 8789:
1.148 www 8790: if ($refuri) {
1.152 www 8791: $refuri=&declutter($refuri);
1.232 www 8792: my ($match,$cond)=&is_on_map($refuri);
8793: if ($match) {
8794: my $refstatecond=$cond;
1.620 albertel 8795: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 8796: =~/\Q$priv\E\&([^\:]*)/) {
1.1162 raeburn 8797: my $value = $1;
8798: if ($priv eq 'bre') {
1.1172.2.146. .1(raebu 8799:22): my $deeplinkblock;
8800:22): unless ($nodeeplinkcheck) {
8801:22): $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
8802:22): }
8803:22): if ($deeplinkblock) {
8804:22): $thisallowed = 'D';
8805:22): } elsif ($noblockcheck) {
1.1162 raeburn 8806: $thisallowed.=$value;
1.1172.2.65 raeburn 8807: } else {
1.1172.2.127 raeburn 8808: my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
1.1172.2.65 raeburn 8809: if (@blockers > 0) {
8810: $thisallowed = 'B';
8811: } else {
8812: $thisallowed.=$value;
8813: }
1.1162 raeburn 8814: }
8815: } else {
8816: $thisallowed.=$value;
8817: }
1.53 www 8818: $uri=$refuri;
8819: $statecond=$refstatecond;
1.52 www 8820: }
8821: }
1.148 www 8822: }
1.29 www 8823: }
1.52 www 8824: }
1.29 www 8825:
1.52 www 8826: #
1.103 harris41 8827: # Gathered now: all privileges that could apply, and condition number
1.52 www 8828: #
8829: #
8830: # Full or no access?
8831: #
1.29 www 8832:
1.52 www 8833: if ($thisallowed=~/F/) {
8834: return 'F';
8835: }
1.29 www 8836:
1.52 www 8837: unless ($thisallowed) {
8838: return '';
8839: }
1.29 www 8840:
1.52 www 8841: # Restrictions exist, deal with them
8842: #
8843: # C:according to course preferences
8844: # R:according to resource settings
8845: # L:unless locked
8846: # X:according to user session state
8847: #
8848:
8849: # Possibly locked functionality, check all courses
1.1172.2.142 raeburn 8850: # In roles.tab, L (unless locked) available for bre, pch, plc, pac and sma.
1.54 www 8851: # Locks might take effect only after 10 minutes cache expiration for other
1.1172.2.142 raeburn 8852: # courses, and 2 minutes for current course, in which user has st or ta role
8853: # which is neither expired nor a future role (unless current course).
1.52 www 8854:
1.1172.2.142 raeburn 8855: my ($needlockcheck,$now,$crsonly);
1.52 www 8856: if ($thisallowed=~/L/) {
1.1172.2.142 raeburn 8857: $now = time;
8858: if ($priv eq 'bre') {
8859: if ($uri ne '') {
8860: if ($orguri =~ m{^/+res/}) {
8861: if ($uri =~ m{^lib/templates/}) {
8862: if ($env{'request.course.id'}) {
8863: $crsonly = 1;
8864: $needlockcheck = 1;
8865: }
8866: } else {
8867: $needlockcheck = 1;
8868: }
8869: } elsif ($env{'request.course.id'}) {
8870: my ($crsdom,$crsnum) = split('_',$env{'request.course.id'});
8871: if (($uri =~ m{^(adm|uploaded|public)/$crsdom/$crsnum/}) ||
8872: ($uri =~ m{^adm/$match_domain/$match_username/\d+/(smppg|bulletinboard)$})) {
8873: $crsonly = 1;
8874: }
8875: $needlockcheck = 1;
8876: }
8877: }
8878: } elsif (($priv eq 'pch') || ($priv eq 'plc') || ($priv eq 'pac') || ($priv eq 'sma')) {
8879: $needlockcheck = 1;
8880: }
8881: }
8882: if ($needlockcheck) {
8883: foreach my $envkey (keys(%env)) {
1.54 www 8884: if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
8885: my $courseid=$2;
8886: my $roleid=$1.'.'.$2;
1.92 www 8887: $courseid=~s/^\///;
1.1172.2.142 raeburn 8888: unless ($env{'request.role'} eq $roleid) {
8889: my ($start,$end) = split(/\./,$env{$envkey});
8890: next unless (($now >= $start) && (!$end || $end > $now));
8891: }
1.54 www 8892: my $expiretime=600;
1.620 albertel 8893: if ($env{'request.role'} eq $roleid) {
1.54 www 8894: $expiretime=120;
8895: }
8896: my ($cdom,$cnum,$csec)=split(/\//,$courseid);
8897: my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620 albertel 8898: if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731 albertel 8899: &coursedescription($courseid,{'freshen_cache' => 1});
1.54 www 8900: }
1.620 albertel 8901: if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
8902: || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
8903: if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
8904: &log($env{'user.domain'},$env{'user.name'},
8905: $env{'user.home'},
1.57 www 8906: 'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52 www 8907: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 8908: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 8909: return '';
8910: }
8911: }
1.620 albertel 8912: if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
8913: || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
1.1172.2.142 raeburn 8914: if ($env{$prefix.'priv.'.$priv.'.lock.expire'}>time) {
1.620 albertel 8915: &log($env{'user.domain'},$env{'user.name'},
8916: $env{'user.home'},
1.57 www 8917: 'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52 www 8918: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 8919: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 8920: return '';
8921: }
8922: }
8923: }
1.29 www 8924: }
1.52 www 8925: }
1.1172.2.126 raeburn 8926:
1.52 www 8927: #
8928: # Rest of the restrictions depend on selected course
8929: #
8930:
1.620 albertel 8931: unless ($env{'request.course.id'}) {
1.766 albertel 8932: if ($thisallowed eq 'A') {
8933: return 'A';
1.814 raeburn 8934: } elsif ($thisallowed eq 'B') {
8935: return 'B';
1.766 albertel 8936: } else {
8937: return '1';
8938: }
1.52 www 8939: }
1.29 www 8940:
1.52 www 8941: #
8942: # Now user is definitely in a course
8943: #
1.53 www 8944:
8945:
8946: # Course preferences
8947:
8948: if ($thisallowed=~/C/) {
1.620 albertel 8949: my $rolecode=(split(/\./,$env{'request.role'}))[0];
8950: my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
8951: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479 albertel 8952: =~/\Q$rolecode\E/) {
1.1103 raeburn 8953: if (($priv ne 'pch') && ($priv ne 'plc')) {
1.689 albertel 8954: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
8955: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
8956: $env{'request.course.id'});
8957: }
1.237 www 8958: return '';
8959: }
8960:
1.620 albertel 8961: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479 albertel 8962: =~/\Q$unamedom\E/) {
1.1103 raeburn 8963: if (($priv ne 'pch') && ($priv ne 'plc')) {
1.689 albertel 8964: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
8965: 'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
8966: $env{'request.course.id'});
8967: }
1.54 www 8968: return '';
8969: }
1.53 www 8970: }
8971:
8972: # Resource preferences
8973:
8974: if ($thisallowed=~/R/) {
1.620 albertel 8975: my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479 albertel 8976: if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.1103 raeburn 8977: if (($priv ne 'pch') && ($priv ne 'plc')) {
1.689 albertel 8978: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
8979: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
8980: }
8981: return '';
1.54 www 8982: }
1.53 www 8983: }
1.30 www 8984:
1.1172.2.146. .1(raebu 8985:22): # Restricted for deeplinked session?
8986:22):
8987:22): if ($env{'request.deeplink.login'}) {
8988:22): if ($env{'acc.deeplinkout'} && !$nodeeplinkout) {
8989:22): if (!$symb) { $symb=&symbread($uri,1); }
8990:22): if (($symb) && ($env{'acc.deeplinkout'}=~/\&\Q$symb\E\&/)) {
8991:22): return '';
8992:22): }
8993:22): }
8994:22): }
8995:22):
1.246 www 8996: # Restricted by state or randomout?
1.30 www 8997:
1.52 www 8998: if ($thisallowed=~/X/) {
1.620 albertel 8999: if ($env{'acc.randomout'}) {
1.579 albertel 9000: if (!$symb) { $symb=&symbread($uri,1); }
1.620 albertel 9001: if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) {
1.248 www 9002: return '';
9003: }
1.247 www 9004: }
9005: if (&condval($statecond)) {
1.52 www 9006: return '2';
9007: } else {
9008: return '';
9009: }
9010: }
1.30 www 9011:
1.766 albertel 9012: if ($thisallowed eq 'A') {
9013: return 'A';
1.814 raeburn 9014: } elsif ($thisallowed eq 'B') {
9015: return 'B';
1.1172.2.146. .1(raebu 9016:22): } elsif ($thisallowed eq 'D') {
9017:22): return 'D';
1.766 albertel 9018: }
1.52 www 9019: return 'F';
1.232 www 9020: }
1.1162 raeburn 9021:
1.1172.2.13 raeburn 9022: # ------------------------------------------- Check construction space access
9023:
9024: sub constructaccess {
9025: my ($url,$setpriv)=@_;
9026:
9027: # We do not allow editing of previous versions of files
9028: if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
9029:
9030: # Get username and domain from URL
9031: my ($ownername,$ownerdomain,$ownerhome);
9032:
9033: ($ownerdomain,$ownername) =
1.1172.2.146. .19(raeb 9034:-24): ($url=~ m{^(?:\Q$perlvar{'lonDocRoot'}\E|)(?:/daxepage|/daxeopen)?/priv/($match_domain)/($match_username)(?:/|$)});
1.1172.2.13 raeburn 9035:
9036: # The URL does not really point to any authorspace, forget it
9037: unless (($ownername) && ($ownerdomain)) { return ''; }
9038:
9039: # Now we need to see if the user has access to the authorspace of
9040: # $ownername at $ownerdomain
9041:
9042: if (($ownername eq $env{'user.name'}) && ($ownerdomain eq $env{'user.domain'})) {
9043: # Real author for this?
9044: $ownerhome = $env{'user.home'};
9045: if (exists($env{'user.priv.au./'.$ownerdomain.'/./'})) {
9046: return ($ownername,$ownerdomain,$ownerhome);
9047: }
9048: } else {
9049: # Co-author for this?
9050: if (exists($env{'user.priv.ca./'.$ownerdomain.'/'.$ownername.'./'}) ||
9051: exists($env{'user.priv.aa./'.$ownerdomain.'/'.$ownername.'./'}) ) {
9052: $ownerhome = &homeserver($ownername,$ownerdomain);
9053: return ($ownername,$ownerdomain,$ownerhome);
9054: }
9055: }
9056:
9057: # We don't have any access right now. If we are not possibly going to do anything about this,
9058: # we might as well leave
9059: unless ($setpriv) { return ''; }
9060:
9061: # Backdoor access?
9062: my $allowed=&allowed('eco',$ownerdomain);
9063: # Nope
9064: unless ($allowed) { return ''; }
9065: # Looks like we may have access, but could be locked by the owner of the construction space
9066: if ($allowed eq 'U') {
9067: my %blocked=&get('environment',['domcoord.author'],
9068: $ownerdomain,$ownername);
9069: # Is blocked by owner
9070: if ($blocked{'domcoord.author'} eq 'blocked') { return ''; }
9071: }
9072: if (($allowed eq 'F') || ($allowed eq 'U')) {
9073: # Grant temporary access
9074: my $then=$env{'user.login.time'};
1.1172.2.16 raeburn 9075: my $update=$env{'user.update.time'};
1.1172.2.13 raeburn 9076: if (!$update) { $update = $then; }
9077: my $refresh=$env{'user.refresh.time'};
9078: if (!$refresh) { $refresh = $update; }
9079: my $now = time;
9080: &check_adhoc_privs($ownerdomain,$ownername,$update,$refresh,
9081: $now,'ca','constructaccess');
9082: $ownerhome = &homeserver($ownername,$ownerdomain);
9083: return($ownername,$ownerdomain,$ownerhome);
9084: }
9085: # No business here
9086: return '';
9087: }
9088:
1.1172.2.66 raeburn 9089: # ----------------------------------------------------------- Content Blocking
9090:
9091: {
9092: # Caches for faster Course Contents display where content blocking
9093: # is in operation (i.e., interval param set) for timed quiz.
9094: #
9095: # User for whom data are being temporarily cached.
9096: my $cacheduser='';
1.1172.2.126 raeburn 9097: # Course for which data are being temporarily cached.
9098: my $cachedcid='';
1.1172.2.66 raeburn 9099: # Cached blockers for this user (a hash of blocking items).
9100: my %cachedblockers=();
9101: # When the data were last cached.
9102: my $cachedlast='';
9103:
9104: sub load_all_blockers {
1.1172.2.128 raeburn 9105: my ($uname,$udom)=@_;
1.1172.2.66 raeburn 9106: if (($uname ne '') && ($udom ne '')) {
9107: if (($cacheduser eq $uname.':'.$udom) &&
1.1172.2.126 raeburn 9108: ($cachedcid eq $env{'request.course.id'}) &&
1.1172.2.128 raeburn 9109: (abs($cachedlast-time)<5)) {
1.1172.2.66 raeburn 9110: return;
9111: }
9112: }
9113: $cachedlast=time;
9114: $cacheduser=$uname.':'.$udom;
1.1172.2.126 raeburn 9115: $cachedcid=$env{'request.course.id'};
1.1172.2.128 raeburn 9116: %cachedblockers = &get_commblock_resources();
1.1172.2.126 raeburn 9117: return;
1.1172.2.66 raeburn 9118: }
9119:
1.1162 raeburn 9120: sub get_comm_blocks {
9121: my ($cdom,$cnum) = @_;
9122: if ($cdom eq '' || $cnum eq '') {
9123: return unless ($env{'request.course.id'});
9124: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
9125: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
9126: }
9127: my %commblocks;
9128: my $hashid=$cdom.'_'.$cnum;
9129: my ($blocksref,$cached)=&is_cached_new('comm_block',$hashid);
9130: if ((defined($cached)) && (ref($blocksref) eq 'HASH')) {
9131: %commblocks = %{$blocksref};
9132: } else {
9133: %commblocks = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
9134: my $cachetime = 600;
9135: &do_cache_new('comm_block',$hashid,\%commblocks,$cachetime);
9136: }
9137: return %commblocks;
9138: }
9139:
1.1172.2.66 raeburn 9140: sub get_commblock_resources {
9141: my ($blocks) = @_;
9142: my %blockers = ();
9143: return %blockers unless ($env{'request.course.id'});
1.1172.2.142 raeburn 9144: my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
9145: if ($env{'request.course.sec'}) {
9146: $courseurl .= '/'.$env{'request.course.sec'};
9147: }
9148: return %blockers if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseurl} =~/evb\&([^\:]*)/);
1.1162 raeburn 9149: my %commblocks;
9150: if (ref($blocks) eq 'HASH') {
9151: %commblocks = %{$blocks};
9152: } else {
9153: %commblocks = &get_comm_blocks();
9154: }
1.1172.2.66 raeburn 9155: return %blockers unless (keys(%commblocks) > 0);
1.1163 raeburn 9156: my $navmap = Apache::lonnavmaps::navmap->new();
1.1172.2.66 raeburn 9157: return %blockers unless (ref($navmap));
9158: my $now = time;
1.1162 raeburn 9159: foreach my $block (keys(%commblocks)) {
9160: if ($block =~ /^(\d+)____(\d+)$/) {
9161: my ($start,$end) = ($1,$2);
9162: if ($start <= $now && $end >= $now) {
9163: if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
9164: if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
9165: if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
1.1172.2.66 raeburn 9166: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
9167: $blockers{$block}{maps} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
1.1162 raeburn 9168: }
9169: }
9170: if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
1.1172.2.66 raeburn 9171: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
9172: $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
1.1162 raeburn 9173: }
9174: }
9175: }
9176: }
9177: }
9178: } elsif ($block =~ /^firstaccess____(.+)$/) {
9179: my $item = $1;
9180: if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
9181: if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
1.1172.2.142 raeburn 9182: my (@interval,$mapname);
1.1172.2.66 raeburn 9183: my $type = 'map';
9184: if ($item eq 'course') {
9185: $type = 'course';
9186: @interval=&EXT("resource.0.interval");
9187: } else {
9188: if ($item =~ /___\d+___/) {
9189: $type = 'resource';
9190: @interval=&EXT("resource.0.interval",$item);
1.1162 raeburn 9191: } else {
1.1172.2.142 raeburn 9192: $mapname = &deversion($item);
9193: if (ref($navmap)) {
9194: my $timelimit = $navmap->get_mapparam(undef,$mapname,'0.interval');
9195: @interval = ($timelimit,'map');
1.1162 raeburn 9196: }
9197: }
1.1172.2.66 raeburn 9198: }
1.1172.2.146. .1(raebu 9199:22): if ($interval[0] =~ /^(\d+)/) {
9200:22): my $timelimit = $1;
1.1172.2.66 raeburn 9201: my $first_access;
9202: if ($type eq 'resource') {
9203: $first_access=&get_first_access($interval[1],$item);
9204: } elsif ($type eq 'map') {
9205: $first_access=&get_first_access($interval[1],undef,$item);
9206: } else {
9207: $first_access=&get_first_access($interval[1]);
9208: }
9209: if ($first_access) {
1.1172.2.146. .1(raebu 9210:22): my $timesup = $first_access+$timelimit;
1.1172.2.66 raeburn 9211: if ($timesup > $now) {
9212: my $activeblock;
1.1172.2.142 raeburn 9213: if ($type eq 'resource') {
9214: if (ref($navmap)) {
9215: my $res = $navmap->getBySymb($item);
9216: if ($res->answerable()) {
9217: $activeblock = 1;
9218: }
9219: }
9220: } elsif ($type eq 'map') {
9221: my $mapsymb = &symbread($mapname,1);
9222: if (($mapsymb) && (ref($navmap))) {
9223: my $mapres = $navmap->getBySymb($mapsymb);
9224: if (ref($mapres)) {
9225: my $first = $mapres->map_start();
9226: my $finish = $mapres->map_finish();
9227: my $it = $navmap->getIterator($first,$finish,undef,0,0);
9228: if (ref($it)) {
9229: my $res;
9230: while ($res = $it->next(undef,1)) {
9231: next unless (ref($res));
9232: my $symb = $res->symb();
9233: next if (($symb eq $mapsymb) || ($symb eq ''));
9234: @interval=&EXT("resource.0.interval",$symb);
9235: if ($interval[1] eq 'map') {
9236: if ($res->answerable()) {
9237: $activeblock = 1;
9238: last;
9239: }
9240: }
9241: }
9242: }
9243: }
1.1172.2.66 raeburn 9244: }
9245: }
9246: if ($activeblock) {
9247: if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
9248: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
9249: $blockers{$block}{'maps'} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
9250: }
9251: }
9252: if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
9253: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
9254: $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
1.1163 raeburn 9255: }
1.1162 raeburn 9256: }
9257: }
9258: }
9259: }
9260: }
9261: }
9262: }
9263: }
9264: }
1.1172.2.66 raeburn 9265: return %blockers;
1.1162 raeburn 9266: }
9267:
1.1172.2.66 raeburn 9268: sub has_comm_blocking {
1.1172.2.128 raeburn 9269: my ($priv,$symb,$uri,$ignoresymbdb,$noenccheck,$blocked,$blocks) = @_;
1.1172.2.66 raeburn 9270: my @blockers;
9271: return unless ($env{'request.course.id'});
9272: return unless ($priv eq 'bre');
9273: return if ($env{'request.state'} eq 'construct');
1.1172.2.142 raeburn 9274: my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
9275: if ($env{'request.course.sec'}) {
9276: $courseurl .= '/'.$env{'request.course.sec'};
9277: }
9278: return if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseurl} =~/evb\&([^\:]*)/);
1.1172.2.128 raeburn 9279: my %blockinfo;
9280: if (ref($blocks) eq 'HASH') {
9281: %blockinfo = &get_commblock_resources($blocks);
9282: } else {
9283: &load_all_blockers($env{'user.name'},$env{'user.domain'});
9284: %blockinfo = %cachedblockers;
9285: }
9286: return unless (keys(%blockinfo) > 0);
1.1172.2.66 raeburn 9287: my (%possibles,@symbs);
9288: if (!$symb) {
1.1172.2.128 raeburn 9289: $symb = &symbread($uri,1,1,1,\%possibles,$ignoresymbdb,$noenccheck);
1.1162 raeburn 9290: }
1.1172.2.66 raeburn 9291: if ($symb) {
9292: @symbs = ($symb);
9293: } elsif (keys(%possibles)) {
9294: @symbs = keys(%possibles);
9295: }
9296: my $noblock;
9297: foreach my $symb (@symbs) {
9298: last if ($noblock);
9299: my ($map,$resid,$resurl)=&decode_symb($symb);
1.1172.2.128 raeburn 9300: foreach my $block (keys(%blockinfo)) {
1.1172.2.66 raeburn 9301: if ($block =~ /^firstaccess____(.+)$/) {
9302: my $item = $1;
1.1172.2.126 raeburn 9303: unless ($blocked) {
9304: if (($item eq $map) || ($item eq $symb)) {
9305: $noblock = 1;
9306: last;
9307: }
1.1172.2.66 raeburn 9308: }
1.1162 raeburn 9309: }
1.1172.2.128 raeburn 9310: if (ref($blockinfo{$block}) eq 'HASH') {
9311: if (ref($blockinfo{$block}{'resources'}) eq 'HASH') {
9312: if ($blockinfo{$block}{'resources'}{$symb}) {
1.1172.2.66 raeburn 9313: unless (grep(/^\Q$block\E$/,@blockers)) {
9314: push(@blockers,$block);
9315: }
9316: }
9317: }
1.1172.2.128 raeburn 9318: if (ref($blockinfo{$block}{'maps'}) eq 'HASH') {
9319: if ($blockinfo{$block}{'maps'}{$map}) {
1.1172.2.126 raeburn 9320: unless (grep(/^\Q$block\E$/,@blockers)) {
9321: push(@blockers,$block);
9322: }
1.1172.2.66 raeburn 9323: }
9324: }
1.1162 raeburn 9325: }
9326: }
9327: }
1.1172.2.126 raeburn 9328: unless ($noblock) {
9329: return @blockers;
9330: }
9331: return;
1.1172.2.66 raeburn 9332: }
1.1162 raeburn 9333: }
9334:
1.1172.2.146. .1(raebu 9335:22): sub deeplink_check {
9336:22): my ($priv,$symb,$uri) = @_;
9337:22): return unless ($env{'request.course.id'});
9338:22): return unless ($priv eq 'bre');
9339:22): return if ($env{'request.state'} eq 'construct');
9340:22): return if ($env{'request.role.adv'});
9341:22): my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
9342:22): my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
9343:22): my (%possibles,@symbs);
9344:22): if (!$symb) {
9345:22): $symb = &symbread($uri,1,1,1,\%possibles);
9346:22): }
9347:22): if ($symb) {
9348:22): @symbs = ($symb);
9349:22): } elsif (keys(%possibles)) {
9350:22): @symbs = keys(%possibles);
9351:22): }
9352:22):
9353:22): my ($deeplink_symb,$allow);
9354:22): if ($env{'request.deeplink.login'}) {
9355:22): $deeplink_symb = &Apache::loncommon::deeplink_login_symb($cnum,$cdom);
9356:22): }
9357:22): foreach my $symb (@symbs) {
9358:22): last if ($allow);
9359:22): my $deeplink = &EXT("resource.0.deeplink",$symb);
9360:22): if ($deeplink eq '') {
9361:22): $allow = 1;
9362:22): } else {
9363:22): my ($state,$others,$listed,$scope,$protect) = split(/,/,$deeplink);
9364:22): if ($state ne 'only') {
9365:22): $allow = 1;
9366:22): } else {
9367:22): my $check_deeplink_entry;
9368:22): if ($protect ne 'none') {
9369:22): my ($acctype,$item) = split(/:/,$protect);
9370:22): if (($acctype eq 'ltic') && ($env{'user.linkprotector'})) {
9371:22): if (grep(/^\Q$item\Ec$/,split(/,/,$env{'user.linkprotector'}))) {
9372:22): $check_deeplink_entry = 1
9373:22): }
9374:22): } elsif (($acctype eq 'ltid') && ($env{'user.linkprotector'})) {
9375:22): if (grep(/^\Q$item\Ed$/,split(/,/,$env{'user.linkprotector'}))) {
9376:22): $check_deeplink_entry = 1;
9377:22): }
9378:22): } elsif (($acctype eq 'key') && ($env{'user.deeplinkkey'})) {
9379:22): if (grep(/^\Q$item\E$/,split(/,/,$env{'user.deeplinkkey'}))) {
9380:22): $check_deeplink_entry = 1;
9381:22): }
9382:22): }
9383:22): }
9384:22): if (($protect eq 'none') || ($check_deeplink_entry)) {
9385:22): if ($scope eq 'res') {
9386:22): if ($symb eq $deeplink_symb) {
9387:22): $allow = 1;
9388:22): }
9389:22): } elsif (($scope eq 'map') || ($scope eq 'rec')) {
9390:22): my ($map_from_symb,$map_from_login);
9391:22): $map_from_symb = &deversion((&decode_symb($symb))[0]);
9392:22): if ($deeplink_symb =~ /\.(page|sequence)$/) {
9393:22): $map_from_login = &deversion((&decode_symb($deeplink_symb))[2]);
9394:22): } else {
9395:22): $map_from_login = &deversion((&decode_symb($deeplink_symb))[0]);
9396:22): }
9397:22): if (($map_from_symb) && ($map_from_login)) {
9398:22): if ($map_from_symb eq $map_from_login) {
9399:22): $allow = 1;
9400:22): } elsif ($scope eq 'rec') {
9401:22): my @recurseup = &get_map_hierarchy($map_from_symb,$env{'request.course.id'});
9402:22): if (grep(/^\Q$map_from_login\E$/,@recurseup)) {
9403:22): $allow = 1;
9404:22): }
9405:22): }
9406:22): }
9407:22): }
9408:22): }
9409:22): }
9410:22): }
9411:22): }
9412:22): return if ($allow);
9413:22): return 1;
9414:22): }
9415:22):
1.1172.2.66 raeburn 9416: # -------------------------------- Deversion and split uri into path an filename
9417:
1.1133 foxr 9418: #
1.1172.2.66 raeburn 9419: # Removes the version from a URI and
1.1133 foxr 9420: # splits it in to its filename and path to the filename.
9421: # Seems like File::Basename could have done this more clearly.
9422: # Parameters:
9423: # $uri - input URI
9424: # Returns:
9425: # Two element list consisting of
9426: # $pathname - the URI up to and excluding the trailing /
9427: # $filename - The part of the URI following the last /
9428: # NOTE:
9429: # Another realization of this is simply:
9430: # use File::Basename;
9431: # ...
9432: # $uri = shift;
9433: # $filename = basename($uri);
9434: # $path = dirname($uri);
9435: # return ($filename, $path);
9436: #
9437: # The implementation below is probably faster however.
9438: #
1.710 albertel 9439: sub split_uri_for_cond {
9440: my $uri=&deversion(&declutter(shift));
9441: my @uriparts=split(/\//,$uri);
9442: my $filename=pop(@uriparts);
9443: my $pathname=join('/',@uriparts);
9444: return ($pathname,$filename);
9445: }
1.232 www 9446: # --------------------------------------------------- Is a resource on the map?
9447:
9448: sub is_on_map {
1.710 albertel 9449: my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289 bowersj2 9450: #Trying to find the conditional for the file
1.620 albertel 9451: my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289 bowersj2 9452: /\&\Q$filename\E\:([\d\|]+)\&/);
1.232 www 9453: if ($match) {
1.289 bowersj2 9454: return (1,$1);
9455: } else {
1.434 www 9456: return (0,0);
1.289 bowersj2 9457: }
1.12 www 9458: }
9459:
1.427 www 9460: # --------------------------------------------------------- Get symb from alias
9461:
9462: sub get_symb_from_alias {
9463: my $symb=shift;
9464: my ($map,$resid,$url)=&decode_symb($symb);
9465: # Already is a symb
9466: if ($url) { return $symb; }
9467: # Must be an alias
9468: my $aliassymb='';
9469: my %bighash;
1.620 albertel 9470: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427 www 9471: &GDBM_READER(),0640)) {
9472: my $rid=$bighash{'mapalias_'.$symb};
9473: if ($rid) {
9474: my ($mapid,$resid)=split(/\./,$rid);
1.429 albertel 9475: $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
9476: $resid,$bighash{'src_'.$rid});
1.427 www 9477: }
9478: untie %bighash;
9479: }
9480: return $aliassymb;
9481: }
9482:
1.12 www 9483: # ----------------------------------------------------------------- Define Role
9484:
9485: sub definerole {
9486: if (allowed('mcr','/')) {
1.1172.2.84 raeburn 9487: my ($rolename,$sysrole,$domrole,$courole,$uname,$udom)=@_;
1.800 albertel 9488: foreach my $role (split(':',$sysrole)) {
9489: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 9490: if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
9491: if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
9492: if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 9493: return "refused:s:$crole&$cqual";
9494: }
9495: }
1.191 harris41 9496: }
1.800 albertel 9497: foreach my $role (split(':',$domrole)) {
9498: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 9499: if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
9500: if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
9501: if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) {
1.21 www 9502: return "refused:d:$crole&$cqual";
9503: }
9504: }
1.191 harris41 9505: }
1.800 albertel 9506: foreach my $role (split(':',$courole)) {
9507: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 9508: if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
9509: if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
9510: if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 9511: return "refused:c:$crole&$cqual";
9512: }
9513: }
1.191 harris41 9514: }
1.1172.2.84 raeburn 9515: my $uhome;
9516: if (($uname ne '') && ($udom ne '')) {
9517: $uhome = &homeserver($uname,$udom);
9518: return $uhome if ($uhome eq 'no_host');
9519: } else {
9520: $uname = $env{'user.name'};
9521: $udom = $env{'user.domain'};
9522: $uhome = $env{'user.home'};
9523: }
1.620 albertel 9524: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.1172.2.84 raeburn 9525: "$udom:$uname:rolesdef_$rolename=".
1.21 www 9526: escape($sysrole.'_'.$domrole.'_'.$courole);
1.1172.2.84 raeburn 9527: return reply($command,$uhome);
1.12 www 9528: } else {
9529: return 'refused';
9530: }
1.105 harris41 9531: }
9532:
9533: # ---------------- Make a metadata query against the network of library servers
9534:
9535: sub metadata_query {
1.1172.2.33 raeburn 9536: my ($query,$custom,$customshow,$server_array,$domains_hash)=@_;
1.120 harris41 9537: my %rhash;
1.845 albertel 9538: my %libserv = &all_library();
1.244 matthew 9539: my @server_list = (defined($server_array) ? @$server_array
9540: : keys(%libserv) );
9541: for my $server (@server_list) {
1.1172.2.33 raeburn 9542: my $domains = '';
9543: if (ref($domains_hash) eq 'HASH') {
9544: $domains = $domains_hash->{$server};
9545: }
1.118 harris41 9546: unless ($custom or $customshow) {
1.1172.2.33 raeburn 9547: my $reply=&reply("querysend:".&escape($query).':::'.&escape($domains),$server);
1.118 harris41 9548: $rhash{$server}=$reply;
9549: }
9550: else {
9551: my $reply=&reply("querysend:".&escape($query).':'.
1.1172.2.33 raeburn 9552: &escape($custom).':'.&escape($customshow).':'.&escape($domains),
1.118 harris41 9553: $server);
9554: $rhash{$server}=$reply;
9555: }
1.112 harris41 9556: }
1.118 harris41 9557: return \%rhash;
1.240 www 9558: }
9559:
9560: # ----------------------------------------- Send log queries and wait for reply
9561:
9562: sub log_query {
9563: my ($uname,$udom,$query,%filters)=@_;
9564: my $uhome=&homeserver($uname,$udom);
9565: if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838 albertel 9566: my $uhost=&hostname($uhome);
1.800 albertel 9567: my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240 www 9568: my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
9569: $uhome);
1.479 albertel 9570: unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242 www 9571: return get_query_reply($queryid);
9572: }
9573:
1.818 raeburn 9574: # -------------------------- Update MySQL table for portfolio file
9575:
9576: sub update_portfolio_table {
1.821 raeburn 9577: my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.970 raeburn 9578: if ($group ne '') {
9579: $file_name =~s /^\Q$group\E//;
9580: }
1.818 raeburn 9581: my $homeserver = &homeserver($uname,$udom);
9582: my $queryid=
1.821 raeburn 9583: &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
9584: ':'.&escape($file_name).':'.$action,$homeserver);
1.818 raeburn 9585: my $reply = &get_query_reply($queryid);
9586: return $reply;
9587: }
9588:
1.899 raeburn 9589: # -------------------------- Update MySQL allusers table
9590:
9591: sub update_allusers_table {
9592: my ($uname,$udom,$names) = @_;
9593: my $homeserver = &homeserver($uname,$udom);
9594: my $queryid=
9595: &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
9596: 'lastname='.&escape($names->{'lastname'}).'%%'.
9597: 'firstname='.&escape($names->{'firstname'}).'%%'.
9598: 'middlename='.&escape($names->{'middlename'}).'%%'.
9599: 'generation='.&escape($names->{'generation'}).'%%'.
9600: 'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
9601: 'id='.&escape($names->{'id'}),$homeserver);
1.1075 raeburn 9602: return;
1.899 raeburn 9603: }
9604:
1.508 raeburn 9605: # ------- Request retrieval of institutional classlists for course(s)
1.506 raeburn 9606:
9607: sub fetch_enrollment_query {
1.511 raeburn 9608: my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.1172.2.79 raeburn 9609: my ($homeserver,$sleep,$loopmax);
1.547 raeburn 9610: my $maxtries = 1;
1.508 raeburn 9611: if ($context eq 'automated') {
9612: $homeserver = $perlvar{'lonHostID'};
1.1172.2.79 raeburn 9613: $sleep = 2;
9614: $loopmax = 100;
1.547 raeburn 9615: $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508 raeburn 9616: } else {
9617: $homeserver = &homeserver($cnum,$dom);
9618: }
1.838 albertel 9619: my $host=&hostname($homeserver);
1.506 raeburn 9620: my $cmd = '';
1.1000 raeburn 9621: foreach my $affiliate (keys(%{$affiliatesref})) {
1.800 albertel 9622: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506 raeburn 9623: }
9624: $cmd =~ s/%%$//;
9625: $cmd = &escape($cmd);
9626: my $query = 'fetchenrollment';
1.620 albertel 9627: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526 raeburn 9628: unless ($queryid=~/^\Q$host\E\_/) {
9629: &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum);
9630: return 'error: '.$queryid;
9631: }
1.1172.2.93 raeburn 9632: my $reply = &get_query_reply($queryid,$sleep,$loopmax);
1.547 raeburn 9633: my $tries = 1;
9634: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
1.1172.2.79 raeburn 9635: $reply = &get_query_reply($queryid,$sleep,$loopmax);
1.547 raeburn 9636: $tries ++;
9637: }
1.526 raeburn 9638: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620 albertel 9639: &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526 raeburn 9640: } else {
1.901 albertel 9641: my @responses = split(/:/,$reply);
1.1172.2.82 raeburn 9642: if (grep { $_ eq $homeserver } ¤t_machine_ids()) {
1.800 albertel 9643: foreach my $line (@responses) {
9644: my ($key,$value) = split(/=/,$line,2);
1.515 raeburn 9645: $$replyref{$key} = $value;
9646: }
9647: } else {
1.1117 foxr 9648: my $pathname = LONCAPA::tempdir();
1.800 albertel 9649: foreach my $line (@responses) {
9650: my ($key,$value) = split(/=/,$line);
1.506 raeburn 9651: $$replyref{$key} = $value;
9652: if ($value > 0) {
1.800 albertel 9653: foreach my $item (@{$$affiliatesref{$key}}) {
9654: my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506 raeburn 9655: my $destname = $pathname.'/'.$filename;
9656: my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526 raeburn 9657: if ($xml_classlist =~ /^error/) {
9658: &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
9659: } else {
1.1172.2.96 raeburn 9660: if ( open(FILE,">",$destname) ) {
1.506 raeburn 9661: print FILE &unescape($xml_classlist);
9662: close(FILE);
1.526 raeburn 9663: } else {
9664: &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506 raeburn 9665: }
9666: }
9667: }
9668: }
9669: }
9670: }
9671: return 'ok';
9672: }
9673: return 'error';
9674: }
9675:
1.242 www 9676: sub get_query_reply {
1.1172.2.79 raeburn 9677: my ($queryid,$sleep,$loopmax) = @_;
9678: if (($sleep eq '') || ($sleep !~ /^\d+\.?\d*$/)) {
9679: $sleep = 0.2;
9680: }
9681: if (($loopmax eq '') || ($loopmax =~ /\D/)) {
9682: $loopmax = 100;
9683: }
1.1117 foxr 9684: my $replyfile=LONCAPA::tempdir().$queryid;
1.240 www 9685: my $reply='';
1.1172.2.79 raeburn 9686: for (1..$loopmax) {
9687: sleep($sleep);
1.240 www 9688: if (-e $replyfile.'.end') {
1.1172.2.96 raeburn 9689: if (open(my $fh,"<",$replyfile)) {
1.904 albertel 9690: $reply = join('',<$fh>);
9691: close($fh);
1.240 www 9692: } else { return 'error: reply_file_error'; }
1.242 www 9693: return &unescape($reply);
9694: }
1.240 www 9695: }
1.242 www 9696: return 'timeout:'.$queryid;
1.240 www 9697: }
9698:
9699: sub courselog_query {
1.241 www 9700: #
9701: # possible filters:
9702: # url: url or symb
9703: # username
9704: # domain
9705: # action: view, submit, grade
9706: # start: timestamp
9707: # end: timestamp
9708: #
1.240 www 9709: my (%filters)=@_;
1.620 albertel 9710: unless ($env{'request.course.id'}) { return 'no_course'; }
1.241 www 9711: if ($filters{'url'}) {
9712: $filters{'url'}=&symbclean(&declutter($filters{'url'}));
9713: $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
9714: $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
9715: }
1.620 albertel 9716: my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
9717: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240 www 9718: return &log_query($cname,$cdom,'courselog',%filters);
9719: }
9720:
9721: sub userlog_query {
1.858 raeburn 9722: #
9723: # possible filters:
9724: # action: log check role
9725: # start: timestamp
9726: # end: timestamp
9727: #
1.240 www 9728: my ($uname,$udom,%filters)=@_;
9729: return &log_query($uname,$udom,'userlog',%filters);
1.12 www 9730: }
9731:
1.506 raeburn 9732: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course
9733:
9734: sub auto_run {
1.508 raeburn 9735: my ($cnum,$cdom) = @_;
1.876 raeburn 9736: my $response = 0;
9737: my $settings;
9738: my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
9739: if (ref($domconfig{'autoenroll'}) eq 'HASH') {
9740: $settings = $domconfig{'autoenroll'};
9741: if ($settings->{'run'} eq '1') {
9742: $response = 1;
9743: }
9744: } else {
1.934 raeburn 9745: my $homeserver;
9746: if (&is_course($cdom,$cnum)) {
9747: $homeserver = &homeserver($cnum,$cdom);
9748: } else {
9749: $homeserver = &domain($cdom,'primary');
9750: }
9751: if ($homeserver ne 'no_host') {
9752: $response = &reply('autorun:'.$cdom,$homeserver);
9753: }
1.876 raeburn 9754: }
1.506 raeburn 9755: return $response;
9756: }
1.776 albertel 9757:
1.506 raeburn 9758: sub auto_get_sections {
1.508 raeburn 9759: my ($cnum,$cdom,$inst_coursecode) = @_;
1.1007 raeburn 9760: my $homeserver;
9761: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
9762: $homeserver = &homeserver($cnum,$cdom);
9763: }
9764: if (!defined($homeserver)) {
9765: if ($cdom =~ /^$match_domain$/) {
9766: $homeserver = &domain($cdom,'primary');
9767: }
9768: }
9769: my @secs;
9770: if (defined($homeserver)) {
9771: my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
9772: unless ($response eq 'refused') {
9773: @secs = split(/:/,$response);
9774: }
1.506 raeburn 9775: }
9776: return @secs;
9777: }
1.776 albertel 9778:
1.506 raeburn 9779: sub auto_new_course {
1.1099 raeburn 9780: my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
1.508 raeburn 9781: my $homeserver = &homeserver($cnum,$cdom);
1.1099 raeburn 9782: my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
1.506 raeburn 9783: return $response;
9784: }
1.776 albertel 9785:
1.506 raeburn 9786: sub auto_validate_courseID {
1.508 raeburn 9787: my ($cnum,$cdom,$inst_course_id) = @_;
9788: my $homeserver = &homeserver($cnum,$cdom);
1.511 raeburn 9789: my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506 raeburn 9790: return $response;
9791: }
1.776 albertel 9792:
1.1007 raeburn 9793: sub auto_validate_instcode {
1.1020 raeburn 9794: my ($cnum,$cdom,$instcode,$owner) = @_;
1.1007 raeburn 9795: my ($homeserver,$response);
9796: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
9797: $homeserver = &homeserver($cnum,$cdom);
9798: }
9799: if (!defined($homeserver)) {
9800: if ($cdom =~ /^$match_domain$/) {
9801: $homeserver = &domain($cdom,'primary');
9802: }
9803: }
1.1065 raeburn 9804: $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
9805: &escape($instcode).':'.&escape($owner),$homeserver));
1.1172.2.19 raeburn 9806: my ($outcome,$description,$defaultcredits) = map { &unescape($_); } split('&',$response,3);
9807: return ($outcome,$description,$defaultcredits);
1.1007 raeburn 9808: }
9809:
1.1172.2.141 raeburn 9810: sub auto_validate_inst_crosslist {
9811: my ($cnum,$cdom,$instcode,$inst_xlist,$coowner) = @_;
9812: my ($homeserver,$response);
9813: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
9814: $homeserver = &homeserver($cnum,$cdom);
9815: }
9816: if (!defined($homeserver)) {
9817: if ($cdom =~ /^$match_domain$/) {
9818: $homeserver = &domain($cdom,'primary');
9819: }
9820: }
9821: unless (($homeserver eq '') || ($homeserver eq 'no_host')) {
9822: $response=&reply('autovalidateinstcrosslist:'.$cdom.':'.
9823: &escape($instcode).':'.&escape($inst_xlist).':'.
9824: &escape($coowner),$homeserver);
9825: }
9826: return $response;
9827: }
9828:
1.506 raeburn 9829: sub auto_create_password {
1.873 raeburn 9830: my ($cnum,$cdom,$authparam,$udom) = @_;
9831: my ($homeserver,$response);
1.506 raeburn 9832: my $create_passwd = 0;
9833: my $authchk = '';
1.873 raeburn 9834: if ($udom =~ /^$match_domain$/) {
9835: $homeserver = &domain($udom,'primary');
9836: }
9837: if ($homeserver eq '') {
9838: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
9839: $homeserver = &homeserver($cnum,$cdom);
9840: }
9841: }
9842: if ($homeserver eq '') {
9843: $authchk = 'nodomain';
1.506 raeburn 9844: } else {
1.873 raeburn 9845: $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
9846: if ($response eq 'refused') {
9847: $authchk = 'refused';
9848: } else {
1.901 albertel 9849: ($authparam,$create_passwd,$authchk) = split(/:/,$response);
1.873 raeburn 9850: }
1.506 raeburn 9851: }
9852: return ($authparam,$create_passwd,$authchk);
9853: }
9854:
1.706 raeburn 9855: sub auto_photo_permission {
9856: my ($cnum,$cdom,$students) = @_;
9857: my $homeserver = &homeserver($cnum,$cdom);
1.707 albertel 9858: my ($outcome,$perm_reqd,$conditions) =
9859: split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709 albertel 9860: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
9861: return (undef,undef);
9862: }
1.706 raeburn 9863: return ($outcome,$perm_reqd,$conditions);
9864: }
9865:
9866: sub auto_checkphotos {
9867: my ($uname,$udom,$pid) = @_;
9868: my $homeserver = &homeserver($uname,$udom);
9869: my ($result,$resulttype);
9870: my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707 albertel 9871: &escape($uname).':'.&escape($pid),
9872: $homeserver));
1.709 albertel 9873: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
9874: return (undef,undef);
9875: }
1.706 raeburn 9876: if ($outcome) {
9877: ($result,$resulttype) = split(/:/,$outcome);
9878: }
9879: return ($result,$resulttype);
9880: }
9881:
9882: sub auto_photochoice {
9883: my ($cnum,$cdom) = @_;
9884: my $homeserver = &homeserver($cnum,$cdom);
9885: my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707 albertel 9886: &escape($cdom),
9887: $homeserver)));
1.709 albertel 9888: if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
9889: return (undef,undef);
9890: }
1.706 raeburn 9891: return ($update,$comment);
9892: }
9893:
9894: sub auto_photoupdate {
9895: my ($affiliatesref,$dom,$cnum,$photo) = @_;
9896: my $homeserver = &homeserver($cnum,$dom);
1.838 albertel 9897: my $host=&hostname($homeserver);
1.706 raeburn 9898: my $cmd = '';
9899: my $maxtries = 1;
1.800 albertel 9900: foreach my $affiliate (keys(%{$affiliatesref})) {
9901: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706 raeburn 9902: }
9903: $cmd =~ s/%%$//;
9904: $cmd = &escape($cmd);
9905: my $query = 'institutionalphotos';
9906: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
9907: unless ($queryid=~/^\Q$host\E\_/) {
9908: &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
9909: return 'error: '.$queryid;
9910: }
9911: my $reply = &get_query_reply($queryid);
9912: my $tries = 1;
9913: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
9914: $reply = &get_query_reply($queryid);
9915: $tries ++;
9916: }
9917: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
9918: &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
9919: } else {
9920: my @responses = split(/:/,$reply);
9921: my $outcome = shift(@responses);
9922: foreach my $item (@responses) {
9923: my ($key,$value) = split(/=/,$item);
9924: $$photo{$key} = $value;
9925: }
9926: return $outcome;
9927: }
9928: return 'error';
9929: }
9930:
1.521 raeburn 9931: sub auto_instcode_format {
1.793 albertel 9932: my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
9933: $cat_order) = @_;
1.521 raeburn 9934: my $courses = '';
1.772 raeburn 9935: my @homeservers;
1.521 raeburn 9936: if ($caller eq 'global') {
1.841 albertel 9937: my %servers = &get_servers($codedom,'library');
9938: foreach my $tryserver (keys(%servers)) {
9939: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
9940: push(@homeservers,$tryserver);
9941: }
1.584 raeburn 9942: }
1.1022 raeburn 9943: } elsif ($caller eq 'requests') {
9944: if ($codedom =~ /^$match_domain$/) {
9945: my $chome = &domain($codedom,'primary');
9946: unless ($chome eq 'no_host') {
9947: push(@homeservers,$chome);
9948: }
9949: }
1.521 raeburn 9950: } else {
1.772 raeburn 9951: push(@homeservers,&homeserver($caller,$codedom));
1.521 raeburn 9952: }
1.793 albertel 9953: foreach my $code (keys(%{$instcodes})) {
9954: $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521 raeburn 9955: }
9956: chop($courses);
1.772 raeburn 9957: my $ok_response = 0;
9958: my $response;
9959: while (@homeservers > 0 && $ok_response == 0) {
9960: my $server = shift(@homeservers);
9961: $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
9962: if ($response !~ /(con_lost|error|no_such_host|refused)/) {
9963: my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) =
1.901 albertel 9964: split(/:/,$response);
1.772 raeburn 9965: %{$codes} = (%{$codes},&str2hash($codes_str));
9966: push(@{$codetitles},&str2array($codetitles_str));
9967: %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
9968: %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
9969: $ok_response = 1;
9970: }
9971: }
9972: if ($ok_response) {
1.521 raeburn 9973: return 'ok';
1.772 raeburn 9974: } else {
9975: return $response;
1.521 raeburn 9976: }
9977: }
9978:
1.792 raeburn 9979: sub auto_instcode_defaults {
9980: my ($domain,$returnhash,$code_order) = @_;
9981: my @homeservers;
1.841 albertel 9982:
9983: my %servers = &get_servers($domain,'library');
9984: foreach my $tryserver (keys(%servers)) {
9985: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
9986: push(@homeservers,$tryserver);
9987: }
1.792 raeburn 9988: }
1.841 albertel 9989:
1.792 raeburn 9990: my $response;
1.841 albertel 9991: foreach my $server (@homeservers) {
1.792 raeburn 9992: $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841 albertel 9993: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
9994:
9995: foreach my $pair (split(/\&/,$response)) {
9996: my ($name,$value)=split(/\=/,$pair);
9997: if ($name eq 'code_order') {
9998: @{$code_order} = split(/\&/,&unescape($value));
9999: } else {
10000: $returnhash->{&unescape($name)}=&unescape($value);
10001: }
10002: }
10003: return 'ok';
1.792 raeburn 10004: }
1.841 albertel 10005:
10006: return $response;
1.1003 raeburn 10007: }
10008:
10009: sub auto_possible_instcodes {
1.1007 raeburn 10010: my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
10011: unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') &&
10012: (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
10013: return;
10014: }
1.1003 raeburn 10015: my (@homeservers,$uhome);
10016: if (defined(&domain($domain,'primary'))) {
10017: $uhome=&domain($domain,'primary');
10018: push(@homeservers,&domain($domain,'primary'));
10019: } else {
10020: my %servers = &get_servers($domain,'library');
10021: foreach my $tryserver (keys(%servers)) {
10022: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
10023: push(@homeservers,$tryserver);
10024: }
10025: }
10026: }
10027: my $response;
10028: foreach my $server (@homeservers) {
10029: $response=&reply('autopossibleinstcodes:'.$domain,$server);
10030: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
1.1007 raeburn 10031: my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) =
10032: split(':',$response);
10033: @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
10034: @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
1.1003 raeburn 10035: foreach my $item (split('&',$cat_title)) {
1.1005 raeburn 10036: my ($name,$value)=split('=',$item);
10037: $cat_titles->{&unescape($name)}=&thaw_unescape($value);
1.1003 raeburn 10038: }
10039: foreach my $item (split('&',$cat_order)) {
1.1005 raeburn 10040: my ($name,$value)=split('=',$item);
10041: $cat_orders->{&unescape($name)}=&thaw_unescape($value);
1.1003 raeburn 10042: }
10043: return 'ok';
10044: }
10045: return $response;
10046: }
1.792 raeburn 10047:
1.1010 raeburn 10048: sub auto_courserequest_checks {
10049: my ($dom) = @_;
1.1020 raeburn 10050: my ($homeserver,%validations);
10051: if ($dom =~ /^$match_domain$/) {
10052: $homeserver = &domain($dom,'primary');
10053: }
10054: unless ($homeserver eq 'no_host') {
10055: my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
10056: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
10057: my @items = split(/&/,$response);
10058: foreach my $item (@items) {
10059: my ($key,$value) = split('=',$item);
10060: $validations{&unescape($key)} = &thaw_unescape($value);
10061: }
10062: }
10063: }
1.1010 raeburn 10064: return %validations;
10065: }
10066:
1.1020 raeburn 10067: sub auto_courserequest_validation {
1.1172.2.43 raeburn 10068: my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist,$custominfo) = @_;
1.1020 raeburn 10069: my ($homeserver,$response);
10070: if ($dom =~ /^$match_domain$/) {
10071: $homeserver = &domain($dom,'primary');
10072: }
1.1172.2.43 raeburn 10073: unless ($homeserver eq 'no_host') {
10074: my $customdata;
10075: if (ref($custominfo) eq 'HASH') {
10076: $customdata = &freeze_escape($custominfo);
10077: }
1.1020 raeburn 10078: $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
1.1021 raeburn 10079: ':'.&escape($crstype).':'.&escape($inststatuslist).
1.1172.2.43 raeburn 10080: ':'.&escape($instcode).':'.&escape($instseclist).':'.
10081: $customdata,$homeserver));
1.1020 raeburn 10082: }
10083: return $response;
10084: }
10085:
1.777 albertel 10086: sub auto_validate_class_sec {
1.918 raeburn 10087: my ($cdom,$cnum,$owners,$inst_class) = @_;
1.773 raeburn 10088: my $homeserver = &homeserver($cnum,$cdom);
1.918 raeburn 10089: my $ownerlist;
10090: if (ref($owners) eq 'ARRAY') {
10091: $ownerlist = join(',',@{$owners});
10092: } else {
10093: $ownerlist = $owners;
10094: }
1.773 raeburn 10095: my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.918 raeburn 10096: &escape($ownerlist).':'.$cdom,$homeserver);
1.773 raeburn 10097: return $response;
10098: }
10099:
1.1172.2.141 raeburn 10100: sub auto_instsec_reformat {
10101: my ($cdom,$action,$instsecref) = @_;
10102: return unless(($action eq 'clutter') || ($action eq 'declutter'));
10103: my @homeservers;
10104: if (defined(&domain($cdom,'primary'))) {
10105: push(@homeservers,&domain($cdom,'primary'));
10106: } else {
10107: my %servers = &get_servers($cdom,'library');
10108: foreach my $tryserver (keys(%servers)) {
10109: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
10110: push(@homeservers,$tryserver);
10111: }
10112: }
10113: }
10114: my $response;
10115: my %reformatted = %{$instsecref};
10116: foreach my $server (@homeservers) {
10117: if (ref($instsecref) eq 'HASH') {
10118: my $info = &freeze_escape($instsecref);
10119: my $response=&reply('autoinstsecreformat:'.$cdom.':'.
10120: $action.':'.$info,$server);
10121: next if ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/);
10122: my @items = split(/&/,$response);
10123: foreach my $item (@items) {
10124: my ($key,$value) = split(/=/,$item);
10125: $reformatted{&unescape($key)} = &thaw_unescape($value);
10126: }
10127: }
10128: }
10129: return %reformatted;
10130: }
10131:
1.1172.2.94 raeburn 10132: sub auto_validate_instclasses {
10133: my ($cdom,$cnum,$owners,$classesref) = @_;
10134: my ($homeserver,%validations);
10135: $homeserver = &homeserver($cnum,$cdom);
10136: unless ($homeserver eq 'no_host') {
10137: my $ownerlist;
10138: if (ref($owners) eq 'ARRAY') {
10139: $ownerlist = join(',',@{$owners});
10140: } else {
10141: $ownerlist = $owners;
10142: }
10143: if (ref($classesref) eq 'HASH') {
10144: my $classes = &freeze_escape($classesref);
10145: my $response=&reply('autovalidateinstclasses:'.&escape($ownerlist).
10146: ':'.$cdom.':'.$classes,$homeserver);
10147: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
10148: my @items = split(/&/,$response);
10149: foreach my $item (@items) {
10150: my ($key,$value) = split('=',$item);
10151: $validations{&unescape($key)} = &thaw_unescape($value);
10152: }
10153: }
10154: }
10155: }
10156: return %validations;
10157: }
10158:
1.1172.2.38 raeburn 10159: sub auto_crsreq_update {
10160: my ($cdom,$cnum,$crstype,$action,$ownername,$ownerdomain,$fullname,$title,
1.1172.2.45 raeburn 10161: $code,$accessstart,$accessend,$inbound) = @_;
1.1172.2.38 raeburn 10162: my ($homeserver,%crsreqresponse);
10163: if ($cdom =~ /^$match_domain$/) {
10164: $homeserver = &domain($cdom,'primary');
10165: }
10166: unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
10167: my $info;
10168: if (ref($inbound) eq 'HASH') {
10169: $info = &freeze_escape($inbound);
10170: }
10171: my $response=&reply('autocrsrequpdate:'.$cdom.':'.$cnum.':'.&escape($crstype).
10172: ':'.&escape($action).':'.&escape($ownername).':'.
10173: &escape($ownerdomain).':'.&escape($fullname).':'.
1.1172.2.45 raeburn 10174: &escape($title).':'.&escape($code).':'.
10175: &escape($accessstart).':'.&escape($accessend).':'.$info,$homeserver);
1.1172.2.38 raeburn 10176: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
10177: my @items = split(/&/,$response);
10178: foreach my $item (@items) {
10179: my ($key,$value) = split('=',$item);
10180: $crsreqresponse{&unescape($key)} = &thaw_unescape($value);
10181: }
10182: }
10183: }
10184: return \%crsreqresponse;
10185: }
10186:
1.1172.2.78 raeburn 10187: sub auto_export_grades {
10188: my ($cdom,$cnum,$inforef,$gradesref) = @_;
10189: my ($homeserver,%exportresponse);
10190: if ($cdom =~ /^$match_domain$/) {
10191: $homeserver = &domain($cdom,'primary');
10192: }
10193: unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
10194: my $info;
10195: if (ref($inforef) eq 'HASH') {
10196: $info = &freeze_escape($inforef);
10197: }
10198: if (ref($gradesref) eq 'HASH') {
10199: my $grades = &freeze_escape($gradesref);
10200: my $response=&reply('encrypt:autoexportgrades:'.$cdom.':'.$cnum.':'.
10201: $info.':'.$grades,$homeserver);
10202: unless ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/) {
10203: my @items = split(/&/,$response);
10204: foreach my $item (@items) {
10205: my ($key,$value) = split('=',$item);
10206: $exportresponse{&unescape($key)} = &thaw_unescape($value);
10207: }
10208: }
10209: }
10210: }
10211: return \%exportresponse;
10212: }
10213:
1.1172.2.68 raeburn 10214: sub check_instcode_cloning {
10215: my ($codedefaults,$code_order,$cloner,$clonefromcode,$clonetocode) = @_;
10216: unless ((ref($codedefaults) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
10217: return;
10218: }
10219: my $canclone;
10220: if (@{$code_order} > 0) {
10221: my $instcoderegexp ='^';
10222: my @clonecodes = split(/\&/,$cloner);
10223: foreach my $item (@{$code_order}) {
10224: if (grep(/^\Q$item\E=/,@clonecodes)) {
10225: foreach my $pair (@clonecodes) {
10226: my ($key,$val) = split(/\=/,$pair,2);
10227: $val = &unescape($val);
10228: if ($key eq $item) {
10229: $instcoderegexp .= '('.$val.')';
10230: last;
10231: }
10232: }
10233: } else {
10234: $instcoderegexp .= $codedefaults->{$item};
10235: }
10236: }
10237: $instcoderegexp .= '$';
10238: my (@from,@to);
10239: eval {
10240: (@from) = ($clonefromcode =~ /$instcoderegexp/);
10241: (@to) = ($clonetocode =~ /$instcoderegexp/);
10242: };
10243: if ((@from > 0) && (@to > 0)) {
10244: my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
10245: if (!@diffs) {
10246: $canclone = 1;
10247: }
10248: }
10249: }
10250: return $canclone;
10251: }
10252:
10253: sub default_instcode_cloning {
10254: my ($clonedom,$domdefclone,$clonefromcode,$clonetocode,$codedefaultsref,$codeorderref) = @_;
10255: my (%codedefaults,@code_order,$canclone);
10256: if ((ref($codedefaultsref) eq 'HASH') && (ref($codeorderref) eq 'ARRAY')) {
10257: %codedefaults = %{$codedefaultsref};
10258: @code_order = @{$codeorderref};
10259: } elsif ($clonedom) {
10260: &auto_instcode_defaults($clonedom,\%codedefaults,\@code_order);
10261: }
10262: if (($domdefclone) && (@code_order)) {
10263: my @clonecodes = split(/\+/,$domdefclone);
10264: my $instcoderegexp ='^';
10265: foreach my $item (@code_order) {
10266: if (grep(/^\Q$item\E$/,@clonecodes)) {
10267: $instcoderegexp .= '('.$codedefaults{$item}.')';
10268: } else {
10269: $instcoderegexp .= $codedefaults{$item};
10270: }
10271: }
10272: $instcoderegexp .= '$';
10273: my (@from,@to);
10274: eval {
10275: (@from) = ($clonefromcode =~ /$instcoderegexp/);
10276: (@to) = ($clonetocode =~ /$instcoderegexp/);
10277: };
10278: if ((@from > 0) && (@to > 0)) {
10279: my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
10280: if (!@diffs) {
10281: $canclone = 1;
10282: }
10283: }
10284: }
10285: return $canclone;
10286: }
10287:
1.679 raeburn 10288: # ------------------------------------------------------- Course Group routines
10289:
10290: sub get_coursegroups {
1.809 raeburn 10291: my ($cdom,$cnum,$group,$namespace) = @_;
10292: return(&dump($namespace,$cdom,$cnum,$group));
1.805 raeburn 10293: }
10294:
1.679 raeburn 10295: sub modify_coursegroup {
10296: my ($cdom,$cnum,$groupsettings) = @_;
10297: return(&put('coursegroups',$groupsettings,$cdom,$cnum));
10298: }
10299:
1.809 raeburn 10300: sub toggle_coursegroup_status {
10301: my ($cdom,$cnum,$group,$action) = @_;
10302: my ($from_namespace,$to_namespace);
10303: if ($action eq 'delete') {
10304: $from_namespace = 'coursegroups';
10305: $to_namespace = 'deleted_groups';
10306: } else {
10307: $from_namespace = 'deleted_groups';
10308: $to_namespace = 'coursegroups';
10309: }
10310: my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805 raeburn 10311: if (my $tmp = &error(%curr_group)) {
10312: &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
10313: return ('read error',$tmp);
10314: } else {
10315: my %savedsettings = %curr_group;
1.809 raeburn 10316: my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805 raeburn 10317: my $deloutcome;
10318: if ($result eq 'ok') {
1.809 raeburn 10319: $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805 raeburn 10320: } else {
10321: return ('write error',$result);
10322: }
10323: if ($deloutcome eq 'ok') {
10324: return 'ok';
10325: } else {
10326: return ('delete error',$deloutcome);
10327: }
10328: }
10329: }
10330:
1.679 raeburn 10331: sub modify_group_roles {
1.957 raeburn 10332: my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
1.679 raeburn 10333: my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
10334: my $role = 'gr/'.&escape($userprivs);
10335: my ($uname,$udom) = split(/:/,$user);
1.957 raeburn 10336: my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
1.684 raeburn 10337: if ($result eq 'ok') {
10338: &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
10339: }
1.679 raeburn 10340: return $result;
10341: }
10342:
10343: sub modify_coursegroup_membership {
10344: my ($cdom,$cnum,$membership) = @_;
10345: my $result = &put('groupmembership',$membership,$cdom,$cnum);
10346: return $result;
10347: }
10348:
1.682 raeburn 10349: sub get_active_groups {
10350: my ($udom,$uname,$cdom,$cnum) = @_;
10351: my $now = time;
10352: my %groups = ();
10353: foreach my $key (keys(%env)) {
1.811 albertel 10354: if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682 raeburn 10355: my ($start,$end) = split(/\./,$env{$key});
10356: if (($end!=0) && ($end<$now)) { next; }
10357: if (($start!=0) && ($start>$now)) { next; }
10358: if ($1 eq $cdom && $2 eq $cnum) {
10359: $groups{$3} = $env{$key} ;
10360: }
10361: }
10362: }
10363: return %groups;
10364: }
10365:
1.683 raeburn 10366: sub get_group_membership {
10367: my ($cdom,$cnum,$group) = @_;
10368: return(&dump('groupmembership',$cdom,$cnum,$group));
10369: }
10370:
10371: sub get_users_groups {
10372: my ($udom,$uname,$courseid) = @_;
1.733 raeburn 10373: my @usersgroups;
1.683 raeburn 10374: my $cachetime=1800;
10375:
10376: my $hashid="$udom:$uname:$courseid";
1.733 raeburn 10377: my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
10378: if (defined($cached)) {
1.734 albertel 10379: @usersgroups = split(/:/,$grouplist);
1.733 raeburn 10380: } else {
10381: $grouplist = '';
1.816 raeburn 10382: my $courseurl = &courseid_to_courseurl($courseid);
1.1166 raeburn 10383: my %roleshash = &dump('roles',$udom,$uname,$courseurl);
1.817 raeburn 10384: my $access_end = $env{'course.'.$courseid.
10385: '.default_enrollment_end_date'};
10386: my $now = time;
10387: foreach my $key (keys(%roleshash)) {
10388: if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
10389: my $group = $1;
10390: if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
10391: my $start = $2;
10392: my $end = $1;
10393: if ($start == -1) { next; } # deleted from group
10394: if (($start!=0) && ($start>$now)) { next; }
10395: if (($end!=0) && ($end<$now)) {
10396: if ($access_end && $access_end < $now) {
10397: if ($access_end - $end < 86400) {
10398: push(@usersgroups,$group);
1.733 raeburn 10399: }
10400: }
1.817 raeburn 10401: next;
1.733 raeburn 10402: }
1.817 raeburn 10403: push(@usersgroups,$group);
1.683 raeburn 10404: }
10405: }
10406: }
1.817 raeburn 10407: @usersgroups = &sort_course_groups($courseid,@usersgroups);
10408: $grouplist = join(':',@usersgroups);
10409: &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683 raeburn 10410: }
1.733 raeburn 10411: return @usersgroups;
1.683 raeburn 10412: }
10413:
10414: sub devalidate_getgroups_cache {
10415: my ($udom,$uname,$cdom,$cnum)=@_;
10416: my $courseid = $cdom.'_'.$cnum;
1.807 albertel 10417:
1.683 raeburn 10418: my $hashid="$udom:$uname:$courseid";
10419: &devalidate_cache_new('getgroups',$hashid);
10420: }
10421:
1.12 www 10422: # ------------------------------------------------------------------ Plain Text
10423:
10424: sub plaintext {
1.988 raeburn 10425: my ($short,$type,$cid,$forcedefault) = @_;
1.1046 raeburn 10426: if ($short =~ m{^cr/}) {
1.758 albertel 10427: return (split('/',$short))[-1];
10428: }
1.742 raeburn 10429: if (!defined($cid)) {
10430: $cid = $env{'request.course.id'};
10431: }
10432: my %rolenames = (
1.1008 raeburn 10433: Course => 'std',
10434: Community => 'alt1',
1.742 raeburn 10435: );
1.1037 raeburn 10436: if ($cid ne '') {
10437: if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
10438: unless ($forcedefault) {
10439: my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'};
10440: &Apache::lonlocal::mt_escape(\$roletext);
10441: return &Apache::lonlocal::mt($roletext);
10442: }
10443: }
10444: }
10445: if ((defined($type)) && (defined($rolenames{$type})) &&
10446: (defined($rolenames{$type})) &&
10447: (defined($prp{$short}{$rolenames{$type}}))) {
1.742 raeburn 10448: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
1.1037 raeburn 10449: } elsif ($cid ne '') {
10450: my $crstype = $env{'course.'.$cid.'.type'};
10451: if (($crstype ne '') && (defined($rolenames{$crstype})) &&
10452: (defined($prp{$short}{$rolenames{$crstype}}))) {
10453: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
10454: }
1.742 raeburn 10455: }
1.1037 raeburn 10456: return &Apache::lonlocal::mt($prp{$short}{'std'});
1.12 www 10457: }
10458:
10459: # ----------------------------------------------------------------- Assign Role
10460:
10461: sub assignrole {
1.957 raeburn 10462: my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
10463: $context)=@_;
1.1172.2.146. .22(raeb 10464:-24): my ($mrole,$rolelogcontext);
1.21 www 10465: if ($role =~ /^cr\//) {
1.393 www 10466: my $cwosec=$url;
1.811 albertel 10467: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.393 www 10468: unless (&allowed('ccr',$cwosec)) {
1.1026 raeburn 10469: my $refused = 1;
10470: if ($context eq 'requestcourses') {
10471: if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
10472: if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
10473: if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
10474: my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10475: my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10476: if ($crsenv{'internal.courseowner'} eq
10477: $env{'user.name'}.':'.$env{'user.domain'}) {
10478: $refused = '';
10479: }
10480: }
10481: }
10482: }
10483: }
10484: if ($refused) {
10485: &logthis('Refused custom assignrole: '.
10486: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
10487: ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
10488: return 'refused';
10489: }
1.104 www 10490: }
1.21 www 10491: $mrole='cr';
1.678 raeburn 10492: } elsif ($role =~ /^gr\//) {
10493: my $cwogrp=$url;
1.811 albertel 10494: $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.678 raeburn 10495: unless (&allowed('mdg',$cwogrp)) {
10496: &logthis('Refused group assignrole: '.
10497: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
10498: $env{'user.name'}.' at '.$env{'user.domain'});
10499: return 'refused';
10500: }
10501: $mrole='gr';
1.21 www 10502: } else {
1.82 www 10503: my $cwosec=$url;
1.811 albertel 10504: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.932 raeburn 10505: if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
10506: my $refused;
10507: if (($env{'request.course.sec'} ne '') && ($role eq 'st')) {
10508: if (!(&allowed('c'.$role,$url))) {
10509: $refused = 1;
10510: }
10511: } else {
10512: $refused = 1;
10513: }
1.947 raeburn 10514: if ($refused) {
1.1045 raeburn 10515: my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10516: if (!$selfenroll && $context eq 'course') {
10517: my %crsenv;
10518: if ($role eq 'cc' || $role eq 'co') {
10519: %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10520: if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
10521: if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
10522: if ($crsenv{'internal.courseowner'} eq
10523: $env{'user.name'}.':'.$env{'user.domain'}) {
10524: $refused = '';
10525: }
10526: }
10527: } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) {
10528: if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
10529: if ($crsenv{'internal.courseowner'} eq
10530: $env{'user.name'}.':'.$env{'user.domain'}) {
10531: $refused = '';
10532: }
10533: }
10534: }
10535: }
1.1172.2.146. .13(raeb 10536:-23): } elsif (($selfenroll == 1) && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
10537:-23): if ($role eq 'st') {
10538:-23): $refused = '';
10539:-23): } elsif (($context eq 'ltienroll') && ($env{'request.lti.login'})) {
10540:-23): $refused = '';
10541:-23): }
1.1017 raeburn 10542: } elsif ($context eq 'requestcourses') {
1.1041 raeburn 10543: my @possroles = ('st','ta','ep','in','cc','co');
1.1026 raeburn 10544: if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
1.1041 raeburn 10545: my $wrongcc;
10546: if ($cnum =~ /^$match_community$/) {
10547: $wrongcc = 1 if ($role eq 'cc');
10548: } else {
10549: $wrongcc = 1 if ($role eq 'co');
10550: }
10551: unless ($wrongcc) {
10552: my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10553: if ($crsenv{'internal.courseowner'} eq
10554: $env{'user.name'}.':'.$env{'user.domain'}) {
10555: $refused = '';
10556: }
1.1017 raeburn 10557: }
10558: }
1.1172.2.9 raeburn 10559: } elsif ($context eq 'requestauthor') {
10560: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
10561: ($url eq '/'.$udom.'/') && ($role eq 'au')) {
10562: if ($env{'environment.requestauthor'} eq 'automatic') {
10563: $refused = '';
10564: } else {
10565: my %domdefaults = &get_domain_defaults($udom);
10566: if (ref($domdefaults{'requestauthor'}) eq 'HASH') {
10567: my $checkbystatus;
10568: if ($env{'user.adv'}) {
10569: my $disposition = $domdefaults{'requestauthor'}{'_LC_adv'};
10570: if ($disposition eq 'automatic') {
10571: $refused = '';
10572: } elsif ($disposition eq '') {
10573: $checkbystatus = 1;
10574: }
10575: } else {
10576: $checkbystatus = 1;
10577: }
10578: if ($checkbystatus) {
10579: if ($env{'environment.inststatus'}) {
10580: my @inststatuses = split(/,/,$env{'environment.inststatus'});
10581: foreach my $type (@inststatuses) {
10582: if (($type ne '') &&
10583: ($domdefaults{'requestauthor'}{$type} eq 'automatic')) {
10584: $refused = '';
10585: }
10586: }
10587: } elsif ($domdefaults{'requestauthor'}{'default'} eq 'automatic') {
10588: $refused = '';
10589: }
10590: }
10591: }
10592: }
10593: }
1.1172.2.146. .22(raeb 10594:-24): } elsif (($context eq 'author') && (($role eq 'ca' || $role eq 'aa'))) {
10595:-24): if ($url =~ m{^/($match_domain)/($match_username)$}) {
10596:-24): my ($audom,$auname) = ($1,$2);
10597:-24): if ((&Apache::lonnet::allowed('v'.$role,"$audom/$auname")) &&
10598:-24): ($env{"environment.internal.manager.$url"})) {
10599:-24): $refused = '';
10600:-24): $rolelogcontext = 'coauthor';
10601:-24): }
10602:-24): }
1.1017 raeburn 10603: }
10604: if ($refused) {
1.947 raeburn 10605: &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
10606: ' '.$role.' '.$end.' '.$start.' by '.
10607: $env{'user.name'}.' at '.$env{'user.domain'});
10608: return 'refused';
10609: }
1.932 raeburn 10610: }
1.1131 raeburn 10611: } elsif ($role eq 'au') {
10612: if ($url ne '/'.$udom.'/') {
10613: &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
10614: ' to assign author role for '.$uname.':'.$udom.
10615: ' in domain: '.$url.' refused (wrong domain).');
10616: return 'refused';
10617: }
1.104 www 10618: }
1.21 www 10619: $mrole=$role;
10620: }
1.620 albertel 10621: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21 www 10622: "$udom:$uname:$url".'_'."$mrole=$role";
1.81 www 10623: if ($end) { $command.='_'.$end; }
1.21 www 10624: if ($start) {
10625: if ($end) {
1.81 www 10626: $command.='_'.$start;
1.21 www 10627: } else {
1.81 www 10628: $command.='_0_'.$start;
1.21 www 10629: }
10630: }
1.739 raeburn 10631: my $origstart = $start;
10632: my $origend = $end;
1.957 raeburn 10633: my $delflag;
1.357 www 10634: # actually delete
10635: if ($deleteflag) {
1.373 www 10636: if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357 www 10637: # modify command to delete the role
1.620 albertel 10638: $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357 www 10639: "$udom:$uname:$url".'_'."$mrole";
1.620 albertel 10640: &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom");
1.357 www 10641: # set start and finish to negative values for userrolelog
10642: $start=-1;
10643: $end=-1;
1.957 raeburn 10644: $delflag = 1;
1.357 www 10645: }
10646: }
10647: # send command
1.349 www 10648: my $answer=&reply($command,&homeserver($uname,$udom));
1.357 www 10649: # log new user role if status is ok
1.349 www 10650: if ($answer eq 'ok') {
1.663 raeburn 10651: &userrolelog($role,$uname,$udom,$url,$start,$end);
1.1172.2.9 raeburn 10652: if (($role eq 'cc') || ($role eq 'in') ||
10653: ($role eq 'ep') || ($role eq 'ad') ||
10654: ($role eq 'ta') || ($role eq 'st') ||
10655: ($role=~/^cr/) || ($role eq 'gr') ||
10656: ($role eq 'co')) {
1.1172.2.13 raeburn 10657: # for course roles, perform group memberships changes triggered by role change.
10658: unless ($role =~ /^gr/) {
10659: &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
10660: $origstart,$selfenroll,$context);
10661: }
1.1172.2.9 raeburn 10662: &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10663: $selfenroll,$context);
10664: } elsif (($role eq 'li') || ($role eq 'dg') || ($role eq 'sc') ||
1.1172.2.90 raeburn 10665: ($role eq 'au') || ($role eq 'dc') || ($role eq 'dh') ||
10666: ($role eq 'da')) {
1.1172.2.9 raeburn 10667: &domainrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10668: $context);
10669: } elsif (($role eq 'ca') || ($role eq 'aa')) {
1.1172.2.146. .22(raeb 10670:-24): if ($rolelogcontext eq '') {
10671:-24): $rolelogcontext = $context;
10672:-24): }
1.1172.2.9 raeburn 10673: &coauthorrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
1.1172.2.146. .22(raeb 10674:-24): $rolelogcontext);
1.1172.2.9 raeburn 10675: }
1.1053 raeburn 10676: if ($role eq 'cc') {
10677: &autoupdate_coowners($url,$end,$start,$uname,$udom);
10678: }
1.349 www 10679: }
10680: return $answer;
1.169 harris41 10681: }
10682:
1.1053 raeburn 10683: sub autoupdate_coowners {
10684: my ($url,$end,$start,$uname,$udom) = @_;
10685: my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
10686: if (($cdom ne '') && ($cnum ne '')) {
10687: my $now = time;
10688: my %domdesign = &Apache::loncommon::get_domainconf($cdom);
10689: if ($domdesign{$cdom.'.autoassign.co-owners'}) {
10690: my %coursehash = &coursedescription($cdom.'_'.$cnum);
10691: my $instcode = $coursehash{'internal.coursecode'};
1.1172.2.141 raeburn 10692: my $xlists = $coursehash{'internal.crosslistings'};
1.1053 raeburn 10693: if ($instcode ne '') {
1.1056 raeburn 10694: if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
10695: unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
1.1053 raeburn 10696: my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
1.1056 raeburn 10697: my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
1.1172.2.141 raeburn 10698: unless ($result eq 'valid') {
10699: if ($xlists ne '') {
10700: foreach my $xlist (split(',',$xlists)) {
10701: my ($inst_crosslist,$lcsec) = split(':',$xlist);
10702: $result =
10703: &auto_validate_inst_crosslist($cnum,$cdom,$instcode,
10704: $inst_crosslist,$uname.':'.$udom);
10705: last if ($result eq 'valid');
10706: }
10707: }
10708: }
1.1056 raeburn 10709: if ($result eq 'valid') {
10710: if ($coursehash{'internal.co-owners'}) {
1.1053 raeburn 10711: foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10712: push(@newcoowners,$coowner);
10713: }
10714: unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
10715: push(@newcoowners,$uname.':'.$udom);
10716: }
10717: @newcoowners = sort(@newcoowners);
10718: } else {
10719: push(@newcoowners,$uname.':'.$udom);
10720: }
1.1172.2.141 raeburn 10721: } elsif ($coursehash{'internal.co-owners'}) {
10722: foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10723: unless ($coowner eq $uname.':'.$udom) {
10724: push(@newcoowners,$coowner);
1.1053 raeburn 10725: }
10726: }
1.1172.2.141 raeburn 10727: unless (@newcoowners > 0) {
10728: $delcoowners = 1;
10729: $coowners = '';
10730: }
1.1053 raeburn 10731: }
10732: if (@newcoowners || $delcoowners) {
10733: &store_coowners($cdom,$cnum,$coursehash{'home'},
10734: $delcoowners,@newcoowners);
10735: }
10736: }
10737: }
10738: }
10739: }
10740: }
10741: }
10742:
10743: sub store_coowners {
10744: my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
10745: my $cid = $cdom.'_'.$cnum;
10746: my ($coowners,$delresult,$putresult);
10747: if (@newcoowners) {
10748: $coowners = join(',',@newcoowners);
10749: my %coownershash = (
10750: 'internal.co-owners' => $coowners,
10751: );
10752: $putresult = &put('environment',\%coownershash,$cdom,$cnum);
10753: if ($putresult eq 'ok') {
10754: if ($env{'course.'.$cid.'.num'} eq $cnum) {
10755: &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
10756: }
10757: }
10758: }
10759: if ($delcoowners) {
10760: $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
10761: if ($delresult eq 'ok') {
10762: if ($env{'course.'.$cid.'.internal.co-owners'}) {
10763: &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
10764: }
10765: }
10766: }
10767: if (($putresult eq 'ok') || ($delresult eq 'ok')) {
10768: my %crsinfo =
10769: &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
10770: if (ref($crsinfo{$cid}) eq 'HASH') {
10771: $crsinfo{$cid}{'co-owners'} = \@newcoowners;
10772: my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
10773: }
10774: }
10775: }
10776:
1.169 harris41 10777: # -------------------------------------------------- Modify user authentication
1.197 www 10778: # Overrides without validation
10779:
1.169 harris41 10780: sub modifyuserauth {
10781: my ($udom,$uname,$umode,$upass)=@_;
10782: my $uhome=&homeserver($uname,$udom);
1.1172.2.115 raeburn 10783: my $allowed;
10784: if (&allowed('mau',$udom)) {
10785: $allowed = 1;
10786: } elsif (($umode eq 'internal') && ($udom eq $env{'user.domain'}) &&
10787: ($env{'request.course.id'}) && (&allowed('mip',$env{'request.course.id'})) &&
10788: (!$env{'course.'.$env{'request.course.id'}.'.internal.nopasswdchg'})) {
10789: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
10790: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
10791: if (($cdom ne '') && ($cnum ne '')) {
10792: my $is_owner = &is_course_owner($cdom,$cnum);
10793: if ($is_owner) {
10794: $allowed = 1;
10795: }
10796: }
10797: }
10798: unless ($allowed) { return 'refused'; }
1.197 www 10799: &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620 albertel 10800: $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10801: ' in domain '.$env{'request.role.domain'});
1.169 harris41 10802: my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
10803: &escape($upass),$uhome);
1.1172.2.134 raeburn 10804: my $ip = &get_requestor_ip();
1.620 albertel 10805: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197 www 10806: 'Authentication changed for '.$udom.', '.$uname.', '.$umode.
1.1172.2.134 raeburn 10807: '(Remote '.$ip.'): '.$reply);
1.197 www 10808: &log($udom,,$uname,$uhome,
1.620 albertel 10809: 'Authentication changed by '.$env{'user.domain'}.', '.
10810: $env{'user.name'}.', '.$umode.
1.1172.2.134 raeburn 10811: '(Remote '.$ip.'): '.$reply);
1.169 harris41 10812: unless ($reply eq 'ok') {
1.197 www 10813: &logthis('Authentication mode error: '.$reply);
1.169 harris41 10814: return 'error: '.$reply;
10815: }
1.170 harris41 10816: return 'ok';
1.80 www 10817: }
10818:
1.81 www 10819: # --------------------------------------------------------------- Modify a user
1.80 www 10820:
1.81 www 10821: sub modifyuser {
1.206 matthew 10822: my ($udom, $uname, $uid,
10823: $umode, $upass, $first,
10824: $middle, $last, $gene,
1.1058 raeburn 10825: $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
1.807 albertel 10826: $udom= &LONCAPA::clean_domain($udom);
10827: $uname=&LONCAPA::clean_username($uname);
1.1059 raeburn 10828: my $showcandelete = 'none';
10829: if (ref($candelete) eq 'ARRAY') {
10830: if (@{$candelete} > 0) {
10831: $showcandelete = join(', ',@{$candelete});
10832: }
10833: }
1.81 www 10834: &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80 www 10835: $umode.', '.$first.', '.$middle.', '.
1.1059 raeburn 10836: $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
1.206 matthew 10837: (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
10838: ' desiredhome not specified').
1.620 albertel 10839: ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10840: ' in domain '.$env{'request.role.domain'});
1.230 stredwic 10841: my $uhome=&homeserver($uname,$udom,'true');
1.1075 raeburn 10842: my $newuser;
10843: if ($uhome eq 'no_host') {
10844: $newuser = 1;
1.1172.2.146. .13(raeb 10845:-23): unless (($umode && ($upass ne '')) || ($umode eq 'localauth') ||
10846:-23): ($umode eq 'lti')) {
10847:-23): return 'error: more information needed to create new user';
10848:-23): }
1.1075 raeburn 10849: }
1.80 www 10850: # ----------------------------------------------------------------- Create User
1.406 albertel 10851: if (($uhome eq 'no_host') &&
1.1172.2.146. .13(raeb 10852:-23): (($umode && $upass) || ($umode eq 'localauth') || ($umode eq 'lti'))) {
1.80 www 10853: my $unhome='';
1.844 albertel 10854: if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) {
1.209 matthew 10855: $unhome = $desiredhome;
1.620 albertel 10856: } elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
10857: $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209 matthew 10858: } else { # load balancing routine for determining $unhome
1.81 www 10859: my $loadm=10000000;
1.841 albertel 10860: my %servers = &get_servers($udom,'library');
10861: foreach my $tryserver (keys(%servers)) {
10862: my $answer=reply('load',$tryserver);
10863: if (($answer=~/\d+/) && ($answer<$loadm)) {
10864: $loadm=$answer;
10865: $unhome=$tryserver;
10866: }
1.80 www 10867: }
10868: }
10869: if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206 matthew 10870: return 'error: unable to find a home server for '.$uname.
10871: ' in domain '.$udom;
1.80 www 10872: }
10873: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
10874: &escape($upass),$unhome);
10875: unless ($reply eq 'ok') {
10876: return 'error: '.$reply;
10877: }
1.230 stredwic 10878: $uhome=&homeserver($uname,$udom,'true');
1.80 www 10879: if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386 matthew 10880: return 'error: unable verify users home machine.';
1.80 www 10881: }
1.209 matthew 10882: } # End of creation of new user
1.80 www 10883: # ---------------------------------------------------------------------- Add ID
10884: if ($uid) {
10885: $uid=~tr/A-Z/a-z/;
10886: my %uidhash=&idrget($udom,$uname);
1.196 www 10887: if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/)
10888: && (!$forceid)) {
1.80 www 10889: unless ($uid eq $uidhash{$uname}) {
1.386 matthew 10890: return 'error: user id "'.$uid.'" does not match '.
10891: 'current user id "'.$uidhash{$uname}.'".';
1.80 www 10892: }
10893: } else {
10894: &idput($udom,($uname => $uid));
10895: }
10896: }
10897: # -------------------------------------------------------------- Add names, etc
1.313 matthew 10898: my @tmp=&get('environment',
1.899 raeburn 10899: ['firstname','middlename','lastname','generation','id',
1.963 raeburn 10900: 'permanentemail','inststatus'],
1.134 albertel 10901: $udom,$uname);
1.1075 raeburn 10902: my (%names,%oldnames);
1.313 matthew 10903: if ($tmp[0] =~ m/^error:.*/) {
10904: %names=();
10905: } else {
10906: %names = @tmp;
1.1075 raeburn 10907: %oldnames = %names;
1.313 matthew 10908: }
1.388 www 10909: #
1.1058 raeburn 10910: # If name, email and/or uid are blank (e.g., because an uploaded file
10911: # of users did not contain them), do not overwrite existing values
10912: # unless field is in $candelete array ref.
10913: #
10914:
10915: my @fields = ('firstname','middlename','lastname','generation',
10916: 'permanentemail','id');
10917: my %newvalues;
10918: if (ref($candelete) eq 'ARRAY') {
10919: foreach my $field (@fields) {
10920: if (grep(/^\Q$field\E$/,@{$candelete})) {
10921: if ($field eq 'firstname') {
10922: $names{$field} = $first;
10923: } elsif ($field eq 'middlename') {
10924: $names{$field} = $middle;
10925: } elsif ($field eq 'lastname') {
10926: $names{$field} = $last;
10927: } elsif ($field eq 'generation') {
10928: $names{$field} = $gene;
10929: } elsif ($field eq 'permanentemail') {
10930: $names{$field} = $email;
10931: } elsif ($field eq 'id') {
10932: $names{$field} = $uid;
10933: }
10934: }
10935: }
10936: }
1.388 www 10937: if ($first) { $names{'firstname'} = $first; }
1.385 matthew 10938: if (defined($middle)) { $names{'middlename'} = $middle; }
1.388 www 10939: if ($last) { $names{'lastname'} = $last; }
1.385 matthew 10940: if (defined($gene)) { $names{'generation'} = $gene; }
1.592 www 10941: if ($email) {
10942: $email=~s/[^\w\@\.\-\,]//gs;
1.963 raeburn 10943: if ($email=~/\@/) { $names{'permanentemail'} = $email; }
1.592 www 10944: }
1.899 raeburn 10945: if ($uid) { $names{'id'} = $uid; }
1.989 raeburn 10946: if (defined($inststatus)) {
10947: $names{'inststatus'} = '';
10948: my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
10949: if (ref($usertypes) eq 'HASH') {
10950: my @okstatuses;
10951: foreach my $item (split(/:/,$inststatus)) {
10952: if (defined($usertypes->{$item})) {
10953: push(@okstatuses,$item);
10954: }
10955: }
10956: if (@okstatuses) {
10957: $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
10958: }
10959: }
10960: }
1.1075 raeburn 10961: my $logmsg = $udom.', '.$uname.', '.$uid.', '.
1.963 raeburn 10962: $umode.', '.$first.', '.$middle.', '.
1.1075 raeburn 10963: $last.', '.$gene.', '.$email.', '.$inststatus;
1.963 raeburn 10964: if ($env{'user.name'} ne '' && $env{'user.domain'}) {
10965: $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
10966: } else {
10967: $logmsg .= ' during self creation';
10968: }
1.1075 raeburn 10969: my $changed;
10970: if ($newuser) {
10971: $changed = 1;
10972: } else {
10973: foreach my $field (@fields) {
10974: if ($names{$field} ne $oldnames{$field}) {
10975: $changed = 1;
10976: last;
10977: }
10978: }
10979: }
10980: unless ($changed) {
10981: $logmsg = 'No changes in user information needed for: '.$logmsg;
10982: &logthis($logmsg);
10983: return 'ok';
10984: }
10985: my $reply = &put('environment', \%names, $udom,$uname);
10986: if ($reply ne 'ok') {
10987: return 'error: '.$reply;
10988: }
1.1087 raeburn 10989: if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
10990: &Apache::lonnet::devalidate_cache_new('emailscache',$uname.':'.$udom);
10991: }
1.1075 raeburn 10992: my $sqlresult = &update_allusers_table($uname,$udom,\%names);
10993: &devalidate_cache_new('namescache',$uname.':'.$udom);
10994: $logmsg = 'Success modifying user '.$logmsg;
1.963 raeburn 10995: &logthis($logmsg);
1.134 albertel 10996: return 'ok';
1.80 www 10997: }
10998:
1.81 www 10999: # -------------------------------------------------------------- Modify student
1.80 www 11000:
1.81 www 11001: sub modifystudent {
11002: my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.957 raeburn 11003: $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
1.1172.2.76 raeburn 11004: $selfenroll,$context,$inststatus,$credits,$instsec)=@_;
1.455 albertel 11005: if (!$cid) {
1.620 albertel 11006: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 11007: return 'not_in_class';
11008: }
1.80 www 11009: }
11010: # --------------------------------------------------------------- Make the user
1.81 www 11011: my $reply=&modifyuser
1.209 matthew 11012: ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.990 raeburn 11013: $desiredhome,$email,$inststatus);
1.80 www 11014: unless ($reply eq 'ok') { return $reply; }
1.297 matthew 11015: # This will cause &modify_student_enrollment to get the uid from the
1.1172.2.31 raeburn 11016: # student's environment
1.297 matthew 11017: $uid = undef if (!$forceid);
1.455 albertel 11018: $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.1172.2.19 raeburn 11019: $gene,$usec,$end,$start,$type,$locktype,
1.1172.2.76 raeburn 11020: $cid,$selfenroll,$context,$credits,$instsec);
1.297 matthew 11021: return $reply;
11022: }
11023:
11024: sub modify_student_enrollment {
1.1172.2.23 raeburn 11025: my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,
1.1172.2.76 raeburn 11026: $locktype,$cid,$selfenroll,$context,$credits,$instsec) = @_;
1.455 albertel 11027: my ($cdom,$cnum,$chome);
11028: if (!$cid) {
1.620 albertel 11029: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 11030: return 'not_in_class';
11031: }
1.620 albertel 11032: $cdom=$env{'course.'.$cid.'.domain'};
11033: $cnum=$env{'course.'.$cid.'.num'};
1.455 albertel 11034: } else {
11035: ($cdom,$cnum)=split(/_/,$cid);
11036: }
1.620 albertel 11037: $chome=$env{'course.'.$cid.'.home'};
1.455 albertel 11038: if (!$chome) {
1.457 raeburn 11039: $chome=&homeserver($cnum,$cdom);
1.297 matthew 11040: }
1.455 albertel 11041: if (!$chome) { return 'unknown_course'; }
1.297 matthew 11042: # Make sure the user exists
1.81 www 11043: my $uhome=&homeserver($uname,$udom);
11044: if (($uhome eq '') || ($uhome eq 'no_host')) {
11045: return 'error: no such user';
11046: }
1.297 matthew 11047: # Get student data if we were not given enough information
11048: if (!defined($first) || $first eq '' ||
11049: !defined($last) || $last eq '' ||
11050: !defined($uid) || $uid eq '' ||
11051: !defined($middle) || $middle eq '' ||
11052: !defined($gene) || $gene eq '') {
1.294 matthew 11053: # They did not supply us with enough data to enroll the student, so
11054: # we need to pick up more information.
1.297 matthew 11055: my %tmp = &get('environment',
1.294 matthew 11056: ['firstname','middlename','lastname', 'generation','id']
1.297 matthew 11057: ,$udom,$uname);
11058:
1.800 albertel 11059: #foreach my $key (keys(%tmp)) {
11060: # &logthis("key $key = ".$tmp{$key});
1.455 albertel 11061: #}
1.294 matthew 11062: $first = $tmp{'firstname'} if (!defined($first) || $first eq '');
11063: $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
11064: $last = $tmp{'lastname'} if (!defined($last) || $last eq '');
1.297 matthew 11065: $gene = $tmp{'generation'} if (!defined($gene) || $gene eq '');
1.294 matthew 11066: $uid = $tmp{'id'} if (!defined($uid) || $uid eq '');
11067: }
1.556 albertel 11068: my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.1148 raeburn 11069: my $user = "$uname:$udom";
11070: my %old_entry = &Apache::lonnet::get('classlist',[$user],$cdom,$cnum);
1.487 albertel 11071: my $reply=cput('classlist',
1.1148 raeburn 11072: {$user =>
1.1172.2.76 raeburn 11073: join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype,$credits,$instsec) },
1.487 albertel 11074: $cdom,$cnum);
1.1148 raeburn 11075: if (($reply eq 'ok') || ($reply eq 'delayed')) {
11076: &devalidate_getsection_cache($udom,$uname,$cid);
11077: } else {
1.81 www 11078: return 'error: '.$reply;
11079: }
1.297 matthew 11080: # Add student role to user
1.83 www 11081: my $uurl='/'.$cid;
1.81 www 11082: $uurl=~s/\_/\//g;
11083: if ($usec) {
11084: $uurl.='/'.$usec;
11085: }
1.1148 raeburn 11086: my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
11087: $selfenroll,$context);
11088: if ($result ne 'ok') {
11089: if ($old_entry{$user} ne '') {
11090: $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
11091: } else {
11092: $reply = &del('classlist',[$user],$cdom,$cnum);
11093: }
11094: }
11095: return $result;
1.21 www 11096: }
11097:
1.556 albertel 11098: sub format_name {
11099: my ($firstname,$middlename,$lastname,$generation,$first)=@_;
11100: my $name;
11101: if ($first ne 'lastname') {
11102: $name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
11103: } else {
11104: if ($lastname=~/\S/) {
11105: $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
11106: $name=~s/\s+,/,/;
11107: } else {
11108: $name.= $firstname.' '.$middlename.' '.$generation;
11109: }
11110: }
11111: $name=~s/^\s+//;
11112: $name=~s/\s+$//;
11113: $name=~s/\s+/ /g;
11114: return $name;
11115: }
11116:
1.84 www 11117: # ------------------------------------------------- Write to course preferences
11118:
11119: sub writecoursepref {
11120: my ($courseid,%prefs)=@_;
11121: $courseid=~s/^\///;
11122: $courseid=~s/\_/\//g;
11123: my ($cdomain,$cnum)=split(/\//,$courseid);
11124: my $chome=homeserver($cnum,$cdomain);
11125: if (($chome eq '') || ($chome eq 'no_host')) {
11126: return 'error: no such course';
11127: }
11128: my $cstring='';
1.800 albertel 11129: foreach my $pref (keys(%prefs)) {
11130: $cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191 harris41 11131: }
1.84 www 11132: $cstring=~s/\&$//;
11133: return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
11134: }
11135:
11136: # ---------------------------------------------------------- Make/modify course
11137:
11138: sub createcourse {
1.741 raeburn 11139: my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
1.1172.2.146. .1(raebu 11140:22): $course_owner,$crstype,$cnum,$context,$category,$callercontext)=@_;
1.84 www 11141: $url=&declutter($url);
11142: my $cid='';
1.1028 raeburn 11143: if ($context eq 'requestcourses') {
11144: my $can_create = 0;
11145: my ($ownername,$ownerdom) = split(':',$course_owner);
11146: if ($udom eq $ownerdom) {
1.1172.2.146. .1(raebu 11147:22): my $reload;
11148:22): if (($callercontext eq 'auto') &&
11149:22): ($ownerdom eq $env{'user.domain'}) && ($ownername eq $env{'user.name'})) {
11150:22): $reload = 'reload';
11151:22): }
11152:22): if (&usertools_access($ownername,$ownerdom,$category,$reload,
1.1028 raeburn 11153: $context)) {
11154: $can_create = 1;
11155: }
11156: } else {
11157: my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
11158: $category);
11159: if ($userenv{'reqcrsotherdom.'.$category} ne '') {
11160: my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
11161: if (@curr > 0) {
11162: my @options = qw(approval validate autolimit);
11163: my $optregex = join('|',@options);
11164: if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
11165: $can_create = 1;
11166: }
11167: }
11168: }
11169: }
11170: if ($can_create) {
11171: unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
11172: unless (&allowed('ccc',$udom)) {
11173: return 'refused';
11174: }
1.1017 raeburn 11175: }
11176: } else {
11177: return 'refused';
11178: }
1.1028 raeburn 11179: } elsif (!&allowed('ccc',$udom)) {
11180: return 'refused';
1.84 www 11181: }
1.1011 raeburn 11182: # --------------------------------------------------------------- Get Unique ID
11183: my $uname;
11184: if ($cnum =~ /^$match_courseid$/) {
11185: my $chome=&homeserver($cnum,$udom,'true');
11186: if (($chome eq '') || ($chome eq 'no_host')) {
11187: $uname = $cnum;
11188: } else {
1.1038 raeburn 11189: $uname = &generate_coursenum($udom,$crstype);
1.1011 raeburn 11190: }
11191: } else {
1.1038 raeburn 11192: $uname = &generate_coursenum($udom,$crstype);
1.1011 raeburn 11193: }
11194: return $uname if ($uname =~ /^error/);
11195: # -------------------------------------------------- Check supplied server name
1.1052 raeburn 11196: if (!defined($course_server)) {
11197: if (defined(&domain($udom,'primary'))) {
11198: $course_server = &domain($udom,'primary');
11199: } else {
11200: $course_server = $env{'user.home'};
11201: }
11202: }
11203: my %host_servers =
11204: &Apache::lonnet::get_servers($udom,'library');
11205: unless ($host_servers{$course_server}) {
11206: return 'error: invalid home server for course: '.$course_server;
1.264 matthew 11207: }
1.84 www 11208: # ------------------------------------------------------------- Make the course
11209: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264 matthew 11210: $course_server);
1.84 www 11211: unless ($reply eq 'ok') { return 'error: '.$reply; }
1.1011 raeburn 11212: my $uhome=&homeserver($uname,$udom,'true');
1.84 www 11213: if (($uhome eq '') || ($uhome eq 'no_host')) {
11214: return 'error: no such course';
11215: }
1.271 www 11216: # ----------------------------------------------------------------- Course made
1.516 raeburn 11217: # log existence
1.1029 raeburn 11218: my $now = time;
1.918 raeburn 11219: my $newcourse = {
11220: $udom.'_'.$uname => {
1.921 raeburn 11221: description => $description,
11222: inst_code => $inst_code,
11223: owner => $course_owner,
11224: type => $crstype,
1.1029 raeburn 11225: creator => $env{'user.name'}.':'.
11226: $env{'user.domain'},
11227: created => $now,
11228: context => $context,
1.918 raeburn 11229: },
11230: };
1.921 raeburn 11231: &courseidput($udom,$newcourse,$uhome,'notime');
1.358 www 11232: # set toplevel url
1.271 www 11233: my $topurl=$url;
11234: unless ($nonstandard) {
11235: # ------------------------------------------ For standard courses, make top url
11236: my $mapurl=&clutter($url);
1.278 www 11237: if ($mapurl eq '/res/') { $mapurl=''; }
1.620 albertel 11238: $env{'form.initmap'}=(<<ENDINITMAP);
1.271 www 11239: <map>
11240: <resource id="1" type="start"></resource>
11241: <resource id="2" src="$mapurl"></resource>
11242: <resource id="3" type="finish"></resource>
11243: <link index="1" from="1" to="2"></link>
11244: <link index="2" from="2" to="3"></link>
11245: </map>
11246: ENDINITMAP
11247: $topurl=&declutter(
1.638 albertel 11248: &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271 www 11249: );
11250: }
11251: # ----------------------------------------------------------- Write preferences
1.84 www 11252: &writecoursepref($udom.'_'.$uname,
1.1056 raeburn 11253: ('description' => $description,
11254: 'url' => $topurl,
11255: 'internal.creator' => $env{'user.name'}.':'.
11256: $env{'user.domain'},
11257: 'internal.created' => $now,
11258: 'internal.creationcontext' => $context)
11259: );
1.84 www 11260: return '/'.$udom.'/'.$uname;
11261: }
11262:
1.1011 raeburn 11263: # ------------------------------------------------------------------- Create ID
11264: sub generate_coursenum {
1.1038 raeburn 11265: my ($udom,$crstype) = @_;
1.1011 raeburn 11266: my $domdesc = &domain($udom);
11267: return 'error: invalid domain' if ($domdesc eq '');
1.1038 raeburn 11268: my $first;
11269: if ($crstype eq 'Community') {
11270: $first = '0';
11271: } else {
11272: $first = int(1+rand(9));
11273: }
11274: my $uname=$first.
1.1011 raeburn 11275: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
11276: substr($$.time,0,5).unpack("H8",pack("I32",time)).
11277: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
11278: # ----------------------------------------------- Make sure that does not exist
11279: my $uhome=&homeserver($uname,$udom,'true');
11280: unless (($uhome eq '') || ($uhome eq 'no_host')) {
1.1038 raeburn 11281: if ($crstype eq 'Community') {
11282: $first = '0';
11283: } else {
11284: $first = int(1+rand(9));
11285: }
11286: $uname=$first.
1.1011 raeburn 11287: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
11288: substr($$.time,0,5).unpack("H8",pack("I32",time)).
11289: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
11290: $uhome=&homeserver($uname,$udom,'true');
11291: unless (($uhome eq '') || ($uhome eq 'no_host')) {
11292: return 'error: unable to generate unique course-ID';
11293: }
11294: }
11295: return $uname;
11296: }
11297:
1.813 albertel 11298: sub is_course {
1.1167 droeschl 11299: my ($cdom, $cnum) = scalar(@_) == 1 ?
11300: ($_[0] =~ /^($match_domain)_($match_courseid)$/) : @_;
1.1172.2.95 raeburn 11301: return unless (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/));
11302: my $uhome=&homeserver($cnum,$cdom);
11303: my $iscourse;
11304: if (grep { $_ eq $uhome } current_machine_ids()) {
11305: $iscourse = &LONCAPA::Lond::is_course($cdom,$cnum);
11306: } else {
11307: my $hashid = $cdom.':'.$cnum;
11308: ($iscourse,my $cached) = &is_cached_new('iscourse',$hashid);
11309: unless (defined($cached)) {
11310: my %courses = &courseiddump($cdom, '.', 1, '.', '.',
11311: $cnum,undef,undef,'.');
11312: $iscourse = 0;
11313: if (exists($courses{$cdom.'_'.$cnum})) {
11314: $iscourse = 1;
11315: }
11316: &do_cache_new('iscourse',$hashid,$iscourse,3600);
11317: }
11318: }
11319: return unless($iscourse);
1.1167 droeschl 11320: return wantarray ? ($cdom, $cnum) : $cdom.'_'.$cnum;
1.813 albertel 11321: }
11322:
1.1015 raeburn 11323: sub store_userdata {
11324: my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
1.1013 raeburn 11325: my $result;
1.1016 raeburn 11326: if ($datakey ne '') {
1.1013 raeburn 11327: if (ref($storehash) eq 'HASH') {
1.1017 raeburn 11328: if ($udom eq '' || $uname eq '') {
11329: $udom = $env{'user.domain'};
11330: $uname = $env{'user.name'};
11331: }
11332: my $uhome=&homeserver($uname,$udom);
1.1013 raeburn 11333: if (($uhome eq '') || ($uhome eq 'no_host')) {
11334: $result = 'error: no_host';
11335: } else {
1.1172.2.138 raeburn 11336: $storehash->{'ip'} = &get_requestor_ip();
1.1013 raeburn 11337: $storehash->{'host'} = $perlvar{'lonHostID'};
11338:
11339: my $namevalue='';
11340: foreach my $key (keys(%{$storehash})) {
11341: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
11342: }
11343: $namevalue=~s/\&$//;
1.1172.2.26 raeburn 11344: unless ($namespace eq 'courserequests') {
11345: $datakey = &escape($datakey);
11346: }
1.1105 raeburn 11347: $result = &reply("store:$udom:$uname:$namespace:$datakey:".
11348: $namevalue,$uhome);
1.1013 raeburn 11349: }
11350: } else {
11351: $result = 'error: data to store was not a hash reference';
11352: }
11353: } else {
11354: $result= 'error: invalid requestkey';
11355: }
11356: return $result;
11357: }
11358:
1.21 www 11359: # ---------------------------------------------------------- Assign Custom Role
11360:
11361: sub assigncustomrole {
1.957 raeburn 11362: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
1.21 www 11363: return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.957 raeburn 11364: $end,$start,$deleteflag,$selfenroll,$context);
1.21 www 11365: }
11366:
11367: # ----------------------------------------------------------------- Revoke Role
11368:
11369: sub revokerole {
1.957 raeburn 11370: my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
1.21 www 11371: my $now=time;
1.965 raeburn 11372: return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
1.21 www 11373: }
11374:
11375: # ---------------------------------------------------------- Revoke Custom Role
11376:
11377: sub revokecustomrole {
1.957 raeburn 11378: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
1.21 www 11379: my $now=time;
1.357 www 11380: return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
1.957 raeburn 11381: $deleteflag,$selfenroll,$context);
1.17 www 11382: }
11383:
1.533 banghart 11384: # ------------------------------------------------------------ Disk usage
1.535 albertel 11385: sub diskusage {
1.955 raeburn 11386: my ($udom,$uname,$directorypath,$getpropath)=@_;
11387: $directorypath =~ s/\/$//;
11388: my $listing=&reply('du2:'.&escape($directorypath).':'
11389: .&escape($getpropath).':'.&escape($uname).':'
11390: .&escape($udom),homeserver($uname,$udom));
11391: if ($listing eq 'unknown_cmd') {
11392: if ($getpropath) {
11393: $directorypath = &propath($udom,$uname).'/'.$directorypath;
11394: }
11395: $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
11396: }
1.514 albertel 11397: return $listing;
1.512 banghart 11398: }
11399:
1.566 banghart 11400: sub is_locked {
1.1096 raeburn 11401: my ($file_name, $domain, $user, $which) = @_;
1.566 banghart 11402: my @check;
11403: my $is_locked;
1.1093 raeburn 11404: push (@check,$file_name);
1.613 albertel 11405: my %locked = &get('file_permissions',\@check,
1.620 albertel 11406: $env{'user.domain'},$env{'user.name'});
1.615 albertel 11407: my ($tmp)=keys(%locked);
11408: if ($tmp=~/^error:/) { undef(%locked); }
1.745 raeburn 11409:
1.566 banghart 11410: if (ref($locked{$file_name}) eq 'ARRAY') {
1.745 raeburn 11411: $is_locked = 'false';
11412: foreach my $entry (@{$locked{$file_name}}) {
1.1096 raeburn 11413: if (ref($entry) eq 'ARRAY') {
1.746 raeburn 11414: $is_locked = 'true';
1.1096 raeburn 11415: if (ref($which) eq 'ARRAY') {
11416: push(@{$which},$entry);
11417: } else {
11418: last;
11419: }
1.745 raeburn 11420: }
11421: }
1.566 banghart 11422: } else {
11423: $is_locked = 'false';
11424: }
1.1093 raeburn 11425: return $is_locked;
1.566 banghart 11426: }
11427:
1.759 albertel 11428: sub declutter_portfile {
11429: my ($file) = @_;
1.833 albertel 11430: $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759 albertel 11431: return $file;
11432: }
11433:
1.559 banghart 11434: # ------------------------------------------------------------- Mark as Read Only
11435:
11436: sub mark_as_readonly {
11437: my ($domain,$user,$files,$what) = @_;
1.613 albertel 11438: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 11439: my ($tmp)=keys(%current_permissions);
11440: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560 banghart 11441: foreach my $file (@{$files}) {
1.759 albertel 11442: $file = &declutter_portfile($file);
1.561 banghart 11443: push(@{$current_permissions{$file}},$what);
1.559 banghart 11444: }
1.613 albertel 11445: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 11446: return;
11447: }
11448:
1.572 banghart 11449: # ------------------------------------------------------------Save Selected Files
11450:
11451: sub save_selected_files {
11452: my ($user, $path, @files) = @_;
11453: my $filename = $user."savedfiles";
1.573 banghart 11454: my @other_files = &files_not_in_path($user, $path);
1.1172.2.96 raeburn 11455: open (OUT,'>',LONCAPA::tempdir().$filename);
1.573 banghart 11456: foreach my $file (@files) {
1.620 albertel 11457: print (OUT $env{'form.currentpath'}.$file."\n");
1.573 banghart 11458: }
11459: foreach my $file (@other_files) {
1.574 banghart 11460: print (OUT $file."\n");
1.572 banghart 11461: }
1.574 banghart 11462: close (OUT);
1.572 banghart 11463: return 'ok';
11464: }
11465:
1.574 banghart 11466: sub clear_selected_files {
11467: my ($user) = @_;
11468: my $filename = $user."savedfiles";
1.1172.2.96 raeburn 11469: open (OUT,'>',LONCAPA::tempdir().$filename);
1.574 banghart 11470: print (OUT undef);
11471: close (OUT);
11472: return ("ok");
11473: }
11474:
1.572 banghart 11475: sub files_in_path {
11476: my ($user, $path) = @_;
11477: my $filename = $user."savedfiles";
11478: my %return_files;
1.1172.2.96 raeburn 11479: open (IN,'<',LONCAPA::tempdir().$filename);
1.573 banghart 11480: while (my $line_in = <IN>) {
1.574 banghart 11481: chomp ($line_in);
11482: my @paths_and_file = split (m!/!, $line_in);
11483: my $file_part = pop (@paths_and_file);
11484: my $path_part = join ('/', @paths_and_file);
1.573 banghart 11485: $path_part.='/';
11486: my $path_and_file = $path_part.$file_part;
11487: if ($path_part eq $path) {
11488: $return_files{$file_part}= 'selected';
11489: }
11490: }
1.574 banghart 11491: close (IN);
11492: return (\%return_files);
1.572 banghart 11493: }
11494:
11495: # called in portfolio select mode, to show files selected NOT in current directory
11496: sub files_not_in_path {
11497: my ($user, $path) = @_;
11498: my $filename = $user."savedfiles";
11499: my @return_files;
11500: my $path_part;
1.1172.2.96 raeburn 11501: open(IN, '<',LONCAPA::tempdir().$filename);
1.800 albertel 11502: while (my $line = <IN>) {
1.572 banghart 11503: #ok, I know it's clunky, but I want it to work
1.800 albertel 11504: my @paths_and_file = split(m|/|, $line);
11505: my $file_part = pop(@paths_and_file);
11506: chomp($file_part);
11507: my $path_part = join('/', @paths_and_file);
1.572 banghart 11508: $path_part .= '/';
11509: my $path_and_file = $path_part.$file_part;
11510: if ($path_part ne $path) {
1.800 albertel 11511: push(@return_files, ($path_and_file));
1.572 banghart 11512: }
11513: }
1.800 albertel 11514: close(OUT);
1.574 banghart 11515: return (@return_files);
1.572 banghart 11516: }
11517:
1.745 raeburn 11518: #----------------------------------------------Get portfolio file permissions
1.629 banghart 11519:
1.745 raeburn 11520: sub get_portfile_permissions {
11521: my ($domain,$user) = @_;
1.613 albertel 11522: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 11523: my ($tmp)=keys(%current_permissions);
11524: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745 raeburn 11525: return \%current_permissions;
11526: }
11527:
11528: #---------------------------------------------Get portfolio file access controls
11529:
1.749 raeburn 11530: sub get_access_controls {
1.745 raeburn 11531: my ($current_permissions,$group,$file) = @_;
1.769 albertel 11532: my %access;
11533: my $real_file = $file;
11534: $file =~ s/\.meta$//;
1.745 raeburn 11535: if (defined($file)) {
1.749 raeburn 11536: if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
11537: foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769 albertel 11538: $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749 raeburn 11539: }
11540: }
1.745 raeburn 11541: } else {
1.749 raeburn 11542: foreach my $key (keys(%{$current_permissions})) {
11543: if ($key =~ /\0accesscontrol$/) {
11544: if (defined($group)) {
11545: if ($key !~ m-^\Q$group\E/-) {
11546: next;
11547: }
11548: }
11549: my ($fullpath) = split(/\0/,$key);
11550: if (ref($$current_permissions{$key}) eq 'HASH') {
11551: foreach my $control (keys(%{$$current_permissions{$key}})) {
11552: $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
11553: }
11554: }
11555: }
11556: }
11557: }
11558: return %access;
11559: }
11560:
11561: sub modify_access_controls {
11562: my ($file_name,$changes,$domain,$user)=@_;
11563: my ($outcome,$deloutcome);
11564: my %store_permissions;
11565: my %new_values;
11566: my %new_control;
11567: my %translation;
11568: my @deletions = ();
11569: my $now = time;
11570: if (exists($$changes{'activate'})) {
11571: if (ref($$changes{'activate'}) eq 'HASH') {
11572: my @newitems = sort(keys(%{$$changes{'activate'}}));
11573: my $numnew = scalar(@newitems);
11574: for (my $i=0; $i<$numnew; $i++) {
11575: my $newkey = $newitems[$i];
11576: my $newid = &Apache::loncommon::get_cgi_id();
1.797 raeburn 11577: if ($newkey =~ /^\d+:/) {
11578: $newkey =~ s/^(\d+)/$newid/;
11579: $translation{$1} = $newid;
11580: } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
11581: $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
11582: $translation{$1} = $newid;
11583: }
1.749 raeburn 11584: $new_values{$file_name."\0".$newkey} =
11585: $$changes{'activate'}{$newitems[$i]};
11586: $new_control{$newkey} = $now;
11587: }
11588: }
11589: }
11590: my %todelete;
11591: my %changed_items;
11592: foreach my $action ('delete','update') {
11593: if (exists($$changes{$action})) {
11594: if (ref($$changes{$action}) eq 'HASH') {
11595: foreach my $key (keys(%{$$changes{$action}})) {
11596: my ($itemnum) = ($key =~ /^([^:]+):/);
11597: if ($action eq 'delete') {
11598: $todelete{$itemnum} = 1;
11599: } else {
11600: $changed_items{$itemnum} = $key;
11601: }
11602: }
1.745 raeburn 11603: }
11604: }
1.749 raeburn 11605: }
11606: # get lock on access controls for file.
11607: my $lockhash = {
11608: $file_name."\0".'locked_access_records' => $env{'user.name'}.
11609: ':'.$env{'user.domain'},
11610: };
11611: my $tries = 0;
11612: my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
11613:
1.1172.2.79 raeburn 11614: while (($gotlock ne 'ok') && $tries < 10) {
1.749 raeburn 11615: $tries ++;
1.1172.2.79 raeburn 11616: sleep(0.1);
1.749 raeburn 11617: $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
11618: }
11619: if ($gotlock eq 'ok') {
11620: my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
11621: my ($tmp)=keys(%curr_permissions);
11622: if ($tmp=~/^error:/) { undef(%curr_permissions); }
11623: if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
11624: my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
11625: if (ref($curr_controls) eq 'HASH') {
11626: foreach my $control_item (keys(%{$curr_controls})) {
11627: my ($itemnum) = ($control_item =~ /^([^:]+):/);
11628: if (defined($todelete{$itemnum})) {
11629: push(@deletions,$file_name."\0".$control_item);
11630: } else {
11631: if (defined($changed_items{$itemnum})) {
11632: $new_control{$changed_items{$itemnum}} = $now;
11633: push(@deletions,$file_name."\0".$control_item);
11634: $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
11635: } else {
11636: $new_control{$control_item} = $$curr_controls{$control_item};
11637: }
11638: }
1.745 raeburn 11639: }
11640: }
11641: }
1.970 raeburn 11642: my ($group);
11643: if (&is_course($domain,$user)) {
11644: ($group,my $file) = split(/\//,$file_name,2);
11645: }
1.749 raeburn 11646: $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
11647: $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
11648: $outcome = &put('file_permissions',\%new_values,$domain,$user);
11649: # remove lock
11650: my @del_lock = ($file_name."\0".'locked_access_records');
11651: my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818 raeburn 11652: my $sqlresult =
1.970 raeburn 11653: &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
1.818 raeburn 11654: $group);
1.749 raeburn 11655: } else {
11656: $outcome = "error: could not obtain lockfile\n";
1.745 raeburn 11657: }
1.749 raeburn 11658: return ($outcome,$deloutcome,\%new_values,\%translation);
1.745 raeburn 11659: }
11660:
1.827 raeburn 11661: sub make_public_indefinitely {
11662: my ($requrl) = @_;
11663: my $now = time;
11664: my $action = 'activate';
11665: my $aclnum = 0;
11666: if (&is_portfolio_url($requrl)) {
11667: my (undef,$udom,$unum,$file_name,$group) =
11668: &parse_portfolio_url($requrl);
11669: my $current_perms = &get_portfile_permissions($udom,$unum);
11670: my %access_controls = &get_access_controls($current_perms,
11671: $group,$file_name);
11672: foreach my $key (keys(%{$access_controls{$file_name}})) {
11673: my ($num,$scope,$end,$start) =
11674: ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
11675: if ($scope eq 'public') {
11676: if ($start <= $now && $end == 0) {
11677: $action = 'none';
11678: } else {
11679: $action = 'update';
11680: $aclnum = $num;
11681: }
11682: last;
11683: }
11684: }
11685: if ($action eq 'none') {
11686: return 'ok';
11687: } else {
11688: my %changes;
11689: my $newend = 0;
11690: my $newstart = $now;
11691: my $newkey = $aclnum.':public_'.$newend.'_'.$newstart;
11692: $changes{$action}{$newkey} = {
11693: type => 'public',
11694: time => {
11695: start => $newstart,
11696: end => $newend,
11697: },
11698: };
11699: my ($outcome,$deloutcome,$new_values,$translation) =
11700: &modify_access_controls($file_name,\%changes,$udom,$unum);
11701: return $outcome;
11702: }
11703: } else {
11704: return 'invalid';
11705: }
11706: }
11707:
1.745 raeburn 11708: #------------------------------------------------------Get Marked as Read Only
11709:
11710: sub get_marked_as_readonly {
11711: my ($domain,$user,$what,$group) = @_;
11712: my $current_permissions = &get_portfile_permissions($domain,$user);
1.563 banghart 11713: my @readonly_files;
1.629 banghart 11714: my $cmp1=$what;
11715: if (ref($what)) { $cmp1=join('',@{$what}) };
1.745 raeburn 11716: while (my ($file_name,$value) = each(%{$current_permissions})) {
11717: if (defined($group)) {
11718: if ($file_name !~ m-^\Q$group\E/-) {
11719: next;
11720: }
11721: }
1.561 banghart 11722: if (ref($value) eq "ARRAY"){
11723: foreach my $stored_what (@{$value}) {
1.629 banghart 11724: my $cmp2=$stored_what;
1.759 albertel 11725: if (ref($stored_what) eq 'ARRAY') {
1.746 raeburn 11726: $cmp2=join('',@{$stored_what});
1.745 raeburn 11727: }
1.629 banghart 11728: if ($cmp1 eq $cmp2) {
1.561 banghart 11729: push(@readonly_files, $file_name);
1.745 raeburn 11730: last;
1.563 banghart 11731: } elsif (!defined($what)) {
11732: push(@readonly_files, $file_name);
1.745 raeburn 11733: last;
1.561 banghart 11734: }
11735: }
1.745 raeburn 11736: }
1.561 banghart 11737: }
11738: return @readonly_files;
11739: }
1.577 banghart 11740: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561 banghart 11741:
1.577 banghart 11742: sub get_marked_as_readonly_hash {
1.745 raeburn 11743: my ($current_permissions,$group,$what) = @_;
1.577 banghart 11744: my %readonly_files;
1.745 raeburn 11745: while (my ($file_name,$value) = each(%{$current_permissions})) {
11746: if (defined($group)) {
11747: if ($file_name !~ m-^\Q$group\E/-) {
11748: next;
11749: }
11750: }
1.577 banghart 11751: if (ref($value) eq "ARRAY"){
11752: foreach my $stored_what (@{$value}) {
1.745 raeburn 11753: if (ref($stored_what) eq 'ARRAY') {
1.750 banghart 11754: foreach my $lock_descriptor(@{$stored_what}) {
11755: if ($lock_descriptor eq 'graded') {
11756: $readonly_files{$file_name} = 'graded';
11757: } elsif ($lock_descriptor eq 'handback') {
11758: $readonly_files{$file_name} = 'handback';
11759: } else {
11760: if (!exists($readonly_files{$file_name})) {
11761: $readonly_files{$file_name} = 'locked';
11762: }
11763: }
1.745 raeburn 11764: }
1.750 banghart 11765: }
1.577 banghart 11766: }
11767: }
11768: }
11769: return %readonly_files;
11770: }
1.559 banghart 11771: # ------------------------------------------------------------ Unmark as Read Only
11772:
11773: sub unmark_as_readonly {
1.629 banghart 11774: # unmarks $file_name (if $file_name is defined), or all files locked by $what
11775: # for portfolio submissions, $what contains [$symb,$crsid]
1.745 raeburn 11776: my ($domain,$user,$what,$file_name,$group) = @_;
1.759 albertel 11777: $file_name = &declutter_portfile($file_name);
1.634 albertel 11778: my $symb_crs = $what;
11779: if (ref($what)) { $symb_crs=join('',@$what); }
1.745 raeburn 11780: my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615 albertel 11781: my ($tmp)=keys(%current_permissions);
11782: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745 raeburn 11783: my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650 albertel 11784: foreach my $file (@readonly_files) {
1.759 albertel 11785: my $clean_file = &declutter_portfile($file);
11786: if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650 albertel 11787: my $current_locks = $current_permissions{$file};
1.563 banghart 11788: my @new_locks;
11789: my @del_keys;
11790: if (ref($current_locks) eq "ARRAY"){
11791: foreach my $locker (@{$current_locks}) {
1.632 albertel 11792: my $compare=$locker;
1.749 raeburn 11793: if (ref($locker) eq 'ARRAY') {
1.745 raeburn 11794: $compare=join('',@{$locker});
1.746 raeburn 11795: if ($compare ne $symb_crs) {
11796: push(@new_locks, $locker);
11797: }
1.563 banghart 11798: }
11799: }
1.650 albertel 11800: if (scalar(@new_locks) > 0) {
1.563 banghart 11801: $current_permissions{$file} = \@new_locks;
11802: } else {
11803: push(@del_keys, $file);
1.613 albertel 11804: &del('file_permissions',\@del_keys, $domain, $user);
1.650 albertel 11805: delete($current_permissions{$file});
1.563 banghart 11806: }
11807: }
1.561 banghart 11808: }
1.613 albertel 11809: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 11810: return;
11811: }
1.512 banghart 11812:
1.17 www 11813: # ------------------------------------------------------------ Directory lister
11814:
11815: sub dirlist {
1.955 raeburn 11816: my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
1.18 www 11817: $uri=~s/^\///;
11818: $uri=~s/\/$//;
1.253 stredwic 11819: my ($udom, $uname);
1.955 raeburn 11820: if ($getuserdir) {
1.253 stredwic 11821: $udom = $userdomain;
11822: $uname = $username;
1.955 raeburn 11823: } else {
11824: (undef,$udom,$uname)=split(/\//,$uri);
11825: if(defined($userdomain)) {
11826: $udom = $userdomain;
11827: }
11828: if(defined($username)) {
11829: $uname = $username;
11830: }
1.253 stredwic 11831: }
1.955 raeburn 11832: my ($dirRoot,$listing,@listing_results);
1.253 stredwic 11833:
1.955 raeburn 11834: $dirRoot = $perlvar{'lonDocRoot'};
11835: if (defined($getpropath)) {
11836: $dirRoot = &propath($udom,$uname);
1.253 stredwic 11837: $dirRoot =~ s/\/$//;
1.955 raeburn 11838: } elsif (defined($getuserdir)) {
11839: my $subdir=$uname.'__';
11840: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
11841: $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
11842: ."/$udom/$subdir/$uname";
11843: } elsif (defined($alternateRoot)) {
11844: $dirRoot = $alternateRoot;
1.751 banghart 11845: }
1.253 stredwic 11846:
11847: if($udom) {
11848: if($uname) {
1.1135 raeburn 11849: my $uhome = &homeserver($uname,$udom);
1.1136 raeburn 11850: if ($uhome eq 'no_host') {
11851: return ([],'no_host');
11852: }
1.955 raeburn 11853: $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
1.956 raeburn 11854: .$getuserdir.':'.&escape($dirRoot)
1.1135 raeburn 11855: .':'.&escape($uname).':'.&escape($udom),$uhome);
1.955 raeburn 11856: if ($listing eq 'unknown_cmd') {
1.1135 raeburn 11857: $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
1.955 raeburn 11858: } else {
11859: @listing_results = map { &unescape($_); } split(/:/,$listing);
11860: }
1.605 matthew 11861: if ($listing eq 'unknown_cmd') {
1.1135 raeburn 11862: $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
1.605 matthew 11863: @listing_results = split(/:/,$listing);
11864: } else {
11865: @listing_results = map { &unescape($_); } split(/:/,$listing);
11866: }
1.1135 raeburn 11867: if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
1.1136 raeburn 11868: ($listing eq 'rejected') || ($listing eq 'refused') ||
11869: ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
11870: return ([],$listing);
11871: } else {
11872: return (\@listing_results);
1.1135 raeburn 11873: }
1.955 raeburn 11874: } elsif(!$alternateRoot) {
1.1136 raeburn 11875: my (%allusers,%listerror);
1.841 albertel 11876: my %servers = &get_servers($udom,'library');
1.955 raeburn 11877: foreach my $tryserver (keys(%servers)) {
11878: $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
11879: &escape($udom),$tryserver);
11880: if ($listing eq 'unknown_cmd') {
11881: $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
11882: $udom, $tryserver);
11883: } else {
11884: @listing_results = map { &unescape($_); } split(/:/,$listing);
11885: }
1.841 albertel 11886: if ($listing eq 'unknown_cmd') {
11887: $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
11888: $udom, $tryserver);
11889: @listing_results = split(/:/,$listing);
11890: } else {
11891: @listing_results =
11892: map { &unescape($_); } split(/:/,$listing);
11893: }
1.1136 raeburn 11894: if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
11895: ($listing eq 'rejected') || ($listing eq 'refused') ||
11896: ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
11897: $listerror{$tryserver} = $listing;
11898: } else {
1.841 albertel 11899: foreach my $line (@listing_results) {
11900: my ($entry) = split(/&/,$line,2);
11901: $allusers{$entry} = 1;
11902: }
11903: }
1.253 stredwic 11904: }
1.1136 raeburn 11905: my @alluserslist=();
1.800 albertel 11906: foreach my $user (sort(keys(%allusers))) {
1.1136 raeburn 11907: push(@alluserslist,$user.'&user');
1.253 stredwic 11908: }
1.1172.2.80 raeburn 11909: if (!%listerror) {
11910: # no errors
11911: return (\@alluserslist);
11912: } elsif (scalar(keys(%servers)) == 1) {
11913: # one library server, one error
11914: my ($key) = keys(%listerror);
11915: return (\@alluserslist, $listerror{$key});
11916: } elsif ( grep { $_ eq 'con_lost' } values(%listerror) ) {
11917: # con_lost indicates that we might miss data from at least one
11918: # library server
11919: return (\@alluserslist, 'con_lost');
11920: } else {
11921: # multiple library servers and no con_lost -> data should be
11922: # complete.
11923: return (\@alluserslist);
11924: }
11925:
1.253 stredwic 11926: } else {
1.1136 raeburn 11927: return ([],'missing username');
1.253 stredwic 11928: }
1.955 raeburn 11929: } elsif(!defined($getpropath)) {
1.1136 raeburn 11930: my $path = $perlvar{'lonDocRoot'}.'/res/';
11931: my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
11932: return (\@all_domains);
1.955 raeburn 11933: } else {
1.1136 raeburn 11934: return ([],'missing domain');
1.275 stredwic 11935: }
11936: }
11937:
11938: # --------------------------------------------- GetFileTimestamp
11939: # This function utilizes dirlist and returns the date stamp for
11940: # when it was last modified. It will also return an error of -1
11941: # if an error occurs
11942:
11943: sub GetFileTimestamp {
1.955 raeburn 11944: my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
1.807 albertel 11945: $studentDomain = &LONCAPA::clean_domain($studentDomain);
11946: $studentName = &LONCAPA::clean_username($studentName);
1.1136 raeburn 11947: my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
11948: undef,$getuserdir);
11949: if (($error eq 'empty') || ($error eq 'no_such_dir')) {
11950: return -1;
11951: }
11952: if (ref($fileref) eq 'ARRAY') {
11953: my @stats = split('&',$fileref->[0]);
1.375 matthew 11954: # @stats contains first the filename, then the stat output
11955: return $stats[10]; # so this is 10 instead of 9.
1.275 stredwic 11956: } else {
11957: return -1;
1.253 stredwic 11958: }
1.26 www 11959: }
11960:
1.712 albertel 11961: sub stat_file {
11962: my ($uri) = @_;
1.787 albertel 11963: $uri = &clutter_with_no_wrapper($uri);
1.722 albertel 11964:
1.955 raeburn 11965: my ($udom,$uname,$file);
1.712 albertel 11966: if ($uri =~ m-^/(uploaded|editupload)/-) {
11967: ($udom,$uname,$file) =
1.811 albertel 11968: ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712 albertel 11969: $file = 'userfiles/'.$file;
11970: }
11971: if ($uri =~ m-^/res/-) {
11972: ($udom,$uname) =
1.807 albertel 11973: ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712 albertel 11974: $file = $uri;
11975: }
11976:
11977: if (!$udom || !$uname || !$file) {
11978: # unable to handle the uri
11979: return ();
11980: }
1.956 raeburn 11981: my $getpropath;
11982: if ($file =~ /^userfiles\//) {
11983: $getpropath = 1;
11984: }
1.1136 raeburn 11985: my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
11986: if (($error eq 'empty') || ($error eq 'no_such_dir')) {
11987: return ();
11988: } else {
11989: if (ref($listref) eq 'ARRAY') {
11990: my @stats = split('&',$listref->[0]);
11991: shift(@stats); #filename is first
11992: return @stats;
11993: }
1.712 albertel 11994: }
11995: return ();
11996: }
11997:
1.26 www 11998: # -------------------------------------------------------- Value of a Condition
11999:
1.713 albertel 12000: # gets the value of a specific preevaluated condition
12001: # stored in the string $env{user.state.<cid>}
12002: # or looks up a condition reference in the bighash and if if hasn't
12003: # already been evaluated recurses into docondval to get the value of
12004: # the condition, then memoizing it to
12005: # $env{user.state.<cid>.<condition>}
1.40 www 12006: sub directcondval {
12007: my $number=shift;
1.620 albertel 12008: if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555 albertel 12009: &Apache::lonuserstate::evalstate();
12010: }
1.713 albertel 12011: if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
12012: return $env{'user.state.'.$env{'request.course.id'}.".$number"};
12013: } elsif ($number =~ /^_/) {
12014: my $sub_condition;
12015: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
12016: &GDBM_READER(),0640)) {
12017: $sub_condition=$bighash{'conditions'.$number};
12018: untie(%bighash);
12019: }
12020: my $value = &docondval($sub_condition);
1.949 raeburn 12021: &appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
1.713 albertel 12022: return $value;
12023: }
1.620 albertel 12024: if ($env{'user.state.'.$env{'request.course.id'}}) {
12025: return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40 www 12026: } else {
12027: return 2;
12028: }
12029: }
12030:
1.713 albertel 12031: # get the collection of conditions for this resource
1.26 www 12032: sub condval {
12033: my $condidx=shift;
1.54 www 12034: my $allpathcond='';
1.713 albertel 12035: foreach my $cond (split(/\|/,$condidx)) {
12036: if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
12037: $allpathcond.=
12038: '('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
12039: }
1.191 harris41 12040: }
1.54 www 12041: $allpathcond=~s/\|$//;
1.713 albertel 12042: return &docondval($allpathcond);
12043: }
12044:
12045: #evaluates an expression of conditions
12046: sub docondval {
12047: my ($allpathcond) = @_;
12048: my $result=0;
12049: if ($env{'request.course.id'}
12050: && defined($allpathcond)) {
12051: my $operand='|';
12052: my @stack;
12053: foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
12054: if ($chunk eq '(') {
12055: push @stack,($operand,$result);
12056: } elsif ($chunk eq ')') {
12057: my $before=pop @stack;
12058: if (pop @stack eq '&') {
12059: $result=$result>$before?$before:$result;
12060: } else {
12061: $result=$result>$before?$result:$before;
12062: }
12063: } elsif (($chunk eq '&') || ($chunk eq '|')) {
12064: $operand=$chunk;
12065: } else {
12066: my $new=directcondval($chunk);
12067: if ($operand eq '&') {
12068: $result=$result>$new?$new:$result;
12069: } else {
12070: $result=$result>$new?$result:$new;
12071: }
12072: }
12073: }
1.26 www 12074: }
12075: return $result;
1.421 albertel 12076: }
12077:
12078: # ---------------------------------------------------- Devalidate courseresdata
12079:
12080: sub devalidatecourseresdata {
12081: my ($coursenum,$coursedomain)=@_;
12082: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 12083: &devalidate_cache_new('courseres',$hashid);
1.28 www 12084: }
12085:
1.763 www 12086:
1.200 www 12087: # --------------------------------------------------- Course Resourcedata Query
1.878 foxr 12088: #
12089: # Parameters:
12090: # $coursenum - Number of the course.
12091: # $coursedomain - Domain at which the course was created.
12092: # Returns:
12093: # A hash of the course parameters along (I think) with timestamps
12094: # and version info.
1.877 foxr 12095:
1.624 albertel 12096: sub get_courseresdata {
12097: my ($coursenum,$coursedomain)=@_;
1.200 www 12098: my $coursehom=&homeserver($coursenum,$coursedomain);
12099: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 12100: my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624 albertel 12101: my %dumpreply;
1.417 albertel 12102: unless (defined($cached)) {
1.624 albertel 12103: %dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417 albertel 12104: $result=\%dumpreply;
1.251 albertel 12105: my ($tmp) = keys(%dumpreply);
12106: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599 albertel 12107: &do_cache_new('courseres',$hashid,$result,600);
1.306 albertel 12108: } elsif ($tmp =~ /^(con_lost|no_such_host)/) {
12109: return $tmp;
1.416 albertel 12110: } elsif ($tmp =~ /^(error)/) {
1.417 albertel 12111: $result=undef;
1.599 albertel 12112: &do_cache_new('courseres',$hashid,$result,600);
1.250 albertel 12113: }
12114: }
1.624 albertel 12115: return $result;
12116: }
12117:
1.633 albertel 12118: sub devalidateuserresdata {
12119: my ($uname,$udom)=@_;
12120: my $hashid="$udom:$uname";
12121: &devalidate_cache_new('userres',$hashid);
12122: }
12123:
1.624 albertel 12124: sub get_userresdata {
12125: my ($uname,$udom)=@_;
12126: #most student don\'t have any data set, check if there is some data
12127: if (&EXT_cache_status($udom,$uname)) { return undef; }
12128:
12129: my $hashid="$udom:$uname";
12130: my ($result,$cached)=&is_cached_new('userres',$hashid);
12131: if (!defined($cached)) {
12132: my %resourcedata=&dump('resourcedata',$udom,$uname);
12133: $result=\%resourcedata;
12134: &do_cache_new('userres',$hashid,$result,600);
12135: }
12136: my ($tmp)=keys(%$result);
12137: if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
12138: return $result;
12139: }
12140: #error 2 occurs when the .db doesn't exist
12141: if ($tmp!~/error: 2 /) {
1.1172.2.71 raeburn 12142: if ((!defined($cached)) || ($tmp ne 'con_lost')) {
12143: &logthis("<font color=\"blue\">WARNING:".
12144: " Trying to get resource data for ".
12145: $uname." at ".$udom.": ".
12146: $tmp."</font>");
12147: }
1.624 albertel 12148: } elsif ($tmp=~/error: 2 /) {
1.633 albertel 12149: #&EXT_cache_set($udom,$uname);
12150: &do_cache_new('userres',$hashid,undef,600);
1.636 albertel 12151: undef($tmp); # not really an error so don't send it back
1.624 albertel 12152: }
12153: return $tmp;
12154: }
1.879 foxr 12155: #----------------------------------------------- resdata - return resource data
12156: # Purpose:
12157: # Return resource data for either users or for a course.
12158: # Parameters:
12159: # $name - Course/user name.
12160: # $domain - Name of the domain the user/course is registered on.
12161: # $type - Type of thing $name is (must be 'course' or 'user'
12162: # @which - Array of names of resources desired.
12163: # Returns:
12164: # The value of the first reasource in @which that is found in the
12165: # resource hash.
12166: # Exceptional Conditions:
12167: # If the $type passed in is not valid (not the string 'course' or
12168: # 'user', an undefined reference is returned.
12169: # If none of the resources are found, an undef is returned
1.624 albertel 12170: sub resdata {
12171: my ($name,$domain,$type,@which)=@_;
12172: my $result;
12173: if ($type eq 'course') {
12174: $result=&get_courseresdata($name,$domain);
12175: } elsif ($type eq 'user') {
12176: $result=&get_userresdata($name,$domain);
12177: }
12178: if (!ref($result)) { return $result; }
1.251 albertel 12179: foreach my $item (@which) {
1.927 albertel 12180: if (defined($result->{$item->[0]})) {
12181: return [$result->{$item->[0]},$item->[1]];
1.251 albertel 12182: }
1.250 albertel 12183: }
1.291 albertel 12184: return undef;
1.200 www 12185: }
12186:
1.1172.2.146. .1(raebu 12187:22): sub get_domain_lti {
12188:22): my ($cdom,$context) = @_;
.4(raebu 12189:22): my ($name,$cachename,%lti);
.1(raebu 12190:22): if ($context eq 'consumer') {
12191:22): $name = 'ltitools';
12192:22): } elsif ($context eq 'provider') {
12193:22): $name = 'lti';
.4(raebu 12194:22): } elsif ($context eq 'linkprot') {
12195:22): $name = 'ltisec';
.1(raebu 12196:22): } else {
12197:22): return %lti;
12198:22): }
.4(raebu 12199:22): if ($context eq 'linkprot') {
12200:22): $cachename = $context;
12201:22): } else {
12202:22): $cachename = $name;
12203:22): }
12204:22): my ($result,$cached)=&is_cached_new($cachename,$cdom);
.1(raebu 12205:22): if (defined($cached)) {
12206:22): if (ref($result) eq 'HASH') {
12207:22): %lti = %{$result};
12208:22): }
12209:22): } else {
12210:22): my %domconfig = &get_dom('configuration',[$name],$cdom);
12211:22): if (ref($domconfig{$name}) eq 'HASH') {
.4(raebu 12212:22): if ($context eq 'linkprot') {
12213:22): if (ref($domconfig{$name}{'linkprot'}) eq 'HASH') {
12214:22): %lti = %{$domconfig{$name}{'linkprot'}};
12215:22): }
12216:22): } else {
12217:22): %lti = %{$domconfig{$name}};
12218:22): }
.1(raebu 12219:22): }
12220:22): my $cachetime = 24*60*60;
.4(raebu 12221:22): &do_cache_new($cachename,$cdom,\%lti,$cachetime);
.1(raebu 12222:22): }
12223:22): return %lti;
12224:22): }
12225:22):
12226:22): sub get_course_lti {
.13(raeb 12227:-23): my ($cnum,$cdom,$context) = @_;
12228:-23): my ($name,$cachename,%lti);
12229:-23): if ($context eq 'consumer') {
12230:-23): $name = 'ltitools';
12231:-23): $cachename = 'courseltitools';
12232:-23): } elsif ($context eq 'provider') {
12233:-23): $name = 'lti';
12234:-23): $cachename = 'courselti';
12235:-23): } else {
12236:-23): return %lti;
12237:-23): }
.1(raebu 12238:22): my $hashid=$cdom.'_'.$cnum;
.13(raeb 12239:-23): my ($result,$cached)=&is_cached_new($cachename,$hashid);
.1(raebu 12240:22): if (defined($cached)) {
12241:22): if (ref($result) eq 'HASH') {
.13(raeb 12242:-23): %lti = %{$result};
.1(raebu 12243:22): }
12244:22): } else {
.13(raeb 12245:-23): %lti = &dump($name,$cdom,$cnum,undef,undef,undef,1);
.1(raebu 12246:22): my $cachetime = 24*60*60;
.13(raeb 12247:-23): &do_cache_new($cachename,$hashid,\%lti,$cachetime);
.1(raebu 12248:22): }
.13(raeb 12249:-23): return %lti;
.1(raebu 12250:22): }
12251:22):
.3(raebu 12252:22): sub courselti_itemid {
12253:22): my ($cnum,$cdom,$url,$method,$params,$context) = @_;
12254:22): my ($chome,$itemid);
12255:22): $chome = &homeserver($cnum,$cdom);
12256:22): return if ($chome eq 'no_host');
12257:22): if (ref($params) eq 'HASH') {
12258:22): my $rep;
12259:22): if (grep { $_ eq $chome } current_machine_ids()) {
12260:22): $rep = LONCAPA::Lond::crslti_itemid($cdom,$cnum,$url,$method,$params,$perlvar{'lonVersion'});
12261:22): } else {
12262:22): my $escurl = &escape($url);
12263:22): my $escmethod = &escape($method);
12264:22): my $items = &freeze_escape($params);
12265:22): $rep = &reply("encrypt:lti:$cdom:$cnum:$context:$escurl:$escmethod:$items",$chome);
12266:22): }
12267:22): unless (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
12268:22): ($rep eq 'unknown_cmd')) {
12269:22): $itemid = $rep;
12270:22): }
12271:22): }
12272:22): return $itemid;
12273:22): }
12274:22):
12275:22): sub domainlti_itemid {
12276:22): my ($cdom,$url,$method,$params,$context) = @_;
12277:22): my ($primary_id,$itemid);
12278:22): $primary_id = &domain($cdom,'primary');
12279:22): return if ($primary_id eq '');
12280:22): if (ref($params) eq 'HASH') {
12281:22): my $rep;
12282:22): if (grep { $_ eq $primary_id } current_machine_ids()) {
12283:22): $rep = LONCAPA::Lond::domlti_itemid($cdom,$context,$url,$method,$params,$perlvar{'lonVersion'});
12284:22): } else {
12285:22): my $cnum = '';
12286:22): my $escurl = &escape($url);
12287:22): my $escmethod = &escape($method);
12288:22): my $items = &freeze_escape($params);
12289:22): $rep = &reply("encrypt:lti:$cdom:$cnum:$context:$escurl:$escmethod:$items",$primary_id);
12290:22): }
12291:22): unless (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
12292:22): ($rep eq 'unknown_cmd')) {
12293:22): $itemid = $rep;
12294:22): }
12295:22): }
12296:22): return $itemid;
12297:22): }
12298:22):
.15(raeb 12299:-23): sub get_ltitools_id {
12300:-23): my ($context,$cdom,$cnum,$title) = @_;
12301:-23): my ($lockhash,$tries,$gotlock,$id,$error);
12302:-23):
12303:-23): # get lock on ltitools db
12304:-23): $lockhash = {
12305:-23): lock => $env{'user.name'}.
12306:-23): ':'.$env{'user.domain'},
12307:-23): };
12308:-23): $tries = 0;
12309:-23): if ($context eq 'domain') {
12310:-23): $gotlock = &newput_dom('ltitools',$lockhash,$cdom);
12311:-23): } else {
12312:-23): $gotlock = &newput('ltitools',$lockhash,$cdom,$cnum);
12313:-23): }
12314:-23): while (($gotlock ne 'ok') && ($tries<10)) {
12315:-23): $tries ++;
12316:-23): sleep (0.1);
12317:-23): if ($context eq 'domain') {
12318:-23): $gotlock = &newput_dom('ltitools',$lockhash,$cdom);
12319:-23): } else {
12320:-23): $gotlock = &newput('ltitools',$lockhash,$cdom,$cnum);
12321:-23): }
12322:-23): }
12323:-23): if ($gotlock eq 'ok') {
12324:-23): my %currids;
12325:-23): if ($context eq 'domain') {
12326:-23): %currids = &dump_dom('ltitools',$cdom);
12327:-23): } else {
12328:-23): %currids = &dump('ltitools',$cdom,$cnum);
12329:-23): }
12330:-23): if ($currids{'lock'}) {
12331:-23): delete($currids{'lock'});
12332:-23): if (keys(%currids)) {
12333:-23): my @curr = sort { $a <=> $b } keys(%currids);
12334:-23): if ($curr[-1] =~ /^\d+$/) {
12335:-23): $id = 1 + $curr[-1];
12336:-23): }
12337:-23): } else {
12338:-23): $id = 1;
12339:-23): }
12340:-23): if ($id) {
12341:-23): if ($context eq 'domain') {
12342:-23): unless (&newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
12343:-23): $error = 'nostore';
12344:-23): }
12345:-23): } else {
12346:-23): unless (&newput('ltitools',{ $id => $title },$cdom,$cnum) eq 'ok') {
12347:-23): $error = 'nostore';
12348:-23): }
12349:-23): }
12350:-23): } else {
12351:-23): $error = 'nonumber';
12352:-23): }
12353:-23): }
12354:-23): my $dellockoutcome;
12355:-23): if ($context eq 'domain') {
12356:-23): $dellockoutcome = &del_dom('ltitools',['lock'],$cdom);
12357:-23): } else {
12358:-23): $dellockoutcome = &del('ltitools',['lock'],$cdom,$cnum);
12359:-23): }
12360:-23): } else {
12361:-23): $error = 'nolock';
12362:-23): }
12363:-23): return ($id,$error);
12364:-23): }
12365:-23):
.9(raebu 12366:23): sub count_supptools {
12367:23): my ($cnum,$cdom,$ignorecache,$reload)=@_;
1.1172.2.31 raeburn 12368: my $hashid=$cnum.':'.$cdom;
1.1172.2.146. .9(raebu 12369:23): my ($numexttools,$cached);
1.1172.2.31 raeburn 12370: unless ($ignorecache) {
1.1172.2.146. .9(raebu 12371:23): ($numexttools,$cached) = &is_cached_new('supptools',$hashid);
1.1172.2.31 raeburn 12372: }
12373: unless (defined($cached)) {
12374: my $chome=&homeserver($cnum,$cdom);
1.1172.2.146. .9(raebu 12375:23): $numexttools = 0;
1.1172.2.31 raeburn 12376: unless ($chome eq 'no_host') {
1.1172.2.146. .9(raebu 12377:23): my ($supplemental) = &Apache::loncommon::get_supplemental($cnum,$cdom,$reload);
12378:23): if (ref($supplemental) eq 'HASH') {
12379:23): if ((ref($supplemental->{'ids'}) eq 'HASH') && (ref($supplemental->{'hidden'}) eq 'HASH')) {
12380:23): foreach my $key (keys(%{$supplemental->{'ids'}})) {
12381:23): if ($key =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
12382:23): $numexttools ++;
12383:23): }
12384:23): }
12385:23): }
12386:23): }
1.1172.2.31 raeburn 12387: }
1.1172.2.146. .9(raebu 12388:23): &do_cache_new('supptools',$hashid,$numexttools,600);
1.1172.2.31 raeburn 12389: }
1.1172.2.146. .9(raebu 12390:23): return $numexttools;
12391:23): }
12392:23):
12393:23): sub has_unhidden_suppfiles {
12394:23): my ($cnum,$cdom,$ignorecache,$possdel)=@_;
12395:23): my $hashid=$cnum.':'.$cdom;
12396:23): my ($showsupp,$cached);
12397:23): unless ($ignorecache) {
12398:23): ($showsupp,$cached) = &is_cached_new('showsupp',$hashid);
12399:23): }
12400:23): unless (defined($cached)) {
12401:23): my $chome=&homeserver($cnum,$cdom);
12402:23): unless ($chome eq 'no_host') {
12403:23): my ($supplemental) = &Apache::loncommon::get_supplemental($cnum,$cdom,$ignorecache,$possdel);
12404:23): if (ref($supplemental) eq 'HASH') {
12405:23): if ((ref($supplemental->{'ids'}) eq 'HASH') && (ref($supplemental->{'hidden'}) eq 'HASH')) {
12406:23): foreach my $key (keys(%{$supplemental->{'ids'}})) {
12407:23): next if ($key =~ /\.sequence$/);
12408:23): if (ref($supplemental->{'ids'}->{$key}) eq 'ARRAY') {
12409:23): foreach my $id (@{$supplemental->{'ids'}->{$key}}) {
12410:23): unless ($supplemental->{'hidden'}->{$id}) {
12411:23): $showsupp = 1;
12412:23): last;
12413:23): }
12414:23): }
12415:23): }
12416:23): last if ($showsupp);
12417:23): }
12418:23): }
12419:23): }
12420:23): }
12421:23): &do_cache_new('showsupp',$hashid,$showsupp,600);
12422:23): }
12423:23): return $showsupp;
1.1172.2.31 raeburn 12424: }
12425:
1.379 matthew 12426: #
12427: # EXT resource caching routines
12428: #
12429:
1.1172.2.146. .1(raebu 12430:22): {
12431:22): # Cache (5 seconds) of map hierarchy for speedup of navmaps display
12432:22): #
12433:22): # The course for which we cache
12434:22): my $cachedmapkey='';
12435:22): # The cached recursive maps for this course
12436:22): my %cachedmaps=();
12437:22): # When this was last done
12438:22): my $cachedmaptime='';
12439:22):
1.379 matthew 12440: sub clear_EXT_cache_status {
1.383 albertel 12441: &delenv('cache.EXT.');
1.379 matthew 12442: }
12443:
12444: sub EXT_cache_status {
12445: my ($target_domain,$target_user) = @_;
1.383 albertel 12446: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620 albertel 12447: if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379 matthew 12448: # We know already the user has no data
12449: return 1;
12450: } else {
12451: return 0;
12452: }
12453: }
12454:
12455: sub EXT_cache_set {
12456: my ($target_domain,$target_user) = @_;
1.383 albertel 12457: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.949 raeburn 12458: #&appenv({$cachename => time});
1.379 matthew 12459: }
12460:
1.28 www 12461: # --------------------------------------------------------- Value of a Variable
1.58 www 12462: sub EXT {
1.715 albertel 12463:
1.1172.2.28 raeburn 12464: my ($varname,$symbparm,$udom,$uname,$usection,$recurse,$cid)=@_;
1.68 www 12465: unless ($varname) { return ''; }
1.218 albertel 12466: #get real user name/domain, courseid and symb
12467: my $courseid;
1.359 albertel 12468: my $publicuser;
1.427 www 12469: if ($symbparm) {
12470: $symbparm=&get_symb_from_alias($symbparm);
12471: }
1.218 albertel 12472: if (!($uname && $udom)) {
1.790 albertel 12473: (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218 albertel 12474: if (!$symbparm) { $symbparm=$cursymb; }
12475: } else {
1.620 albertel 12476: $courseid=$env{'request.course.id'};
1.218 albertel 12477: }
1.48 www 12478: my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
12479: my $rest;
1.320 albertel 12480: if (defined($therest[0])) {
1.48 www 12481: $rest=join('.',@therest);
12482: } else {
12483: $rest='';
12484: }
1.320 albertel 12485:
1.57 www 12486: my $qualifierrest=$qualifier;
12487: if ($rest) { $qualifierrest.='.'.$rest; }
12488: my $spacequalifierrest=$space;
12489: if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28 www 12490: if ($realm eq 'user') {
1.48 www 12491: # --------------------------------------------------------------- user.resource
12492: if ($space eq 'resource') {
1.651 albertel 12493: if ( (defined($Apache::lonhomework::parsing_a_problem)
12494: || defined($Apache::lonhomework::parsing_a_task))
12495: &&
1.1172.2.142 raeburn 12496: ($symbparm eq &symbread()) ) {
1.744 albertel 12497: # if we are in the middle of processing the resource the
12498: # get the value we are planning on committing
12499: if (defined($Apache::lonhomework::results{$qualifierrest})) {
12500: return $Apache::lonhomework::results{$qualifierrest};
12501: } else {
12502: return $Apache::lonhomework::history{$qualifierrest};
12503: }
1.335 albertel 12504: } else {
1.359 albertel 12505: my %restored;
1.620 albertel 12506: if ($publicuser || $env{'request.state'} eq 'construct') {
1.359 albertel 12507: %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
12508: } else {
12509: %restored=&restore($symbparm,$courseid,$udom,$uname);
12510: }
1.335 albertel 12511: return $restored{$qualifierrest};
12512: }
1.48 www 12513: # ----------------------------------------------------------------- user.access
12514: } elsif ($space eq 'access') {
1.218 albertel 12515: # FIXME - not supporting calls for a specific user
1.48 www 12516: return &allowed($qualifier,$rest);
12517: # ------------------------------------------ user.preferences, user.environment
12518: } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620 albertel 12519: if (($uname eq $env{'user.name'}) &&
12520: ($udom eq $env{'user.domain'})) {
12521: return $env{join('.',('environment',$qualifierrest))};
1.218 albertel 12522: } else {
1.359 albertel 12523: my %returnhash;
12524: if (!$publicuser) {
12525: %returnhash=&userenvironment($udom,$uname,
12526: $qualifierrest);
12527: }
1.218 albertel 12528: return $returnhash{$qualifierrest};
12529: }
1.48 www 12530: # ----------------------------------------------------------------- user.course
12531: } elsif ($space eq 'course') {
1.218 albertel 12532: # FIXME - not supporting calls for a specific user
1.620 albertel 12533: return $env{join('.',('request.course',$qualifier))};
1.48 www 12534: # ------------------------------------------------------------------- user.role
12535: } elsif ($space eq 'role') {
1.218 albertel 12536: # FIXME - not supporting calls for a specific user
1.620 albertel 12537: my ($role,$where)=split(/\./,$env{'request.role'});
1.48 www 12538: if ($qualifier eq 'value') {
12539: return $role;
12540: } elsif ($qualifier eq 'extent') {
12541: return $where;
12542: }
12543: # ----------------------------------------------------------------- user.domain
12544: } elsif ($space eq 'domain') {
1.218 albertel 12545: return $udom;
1.48 www 12546: # ------------------------------------------------------------------- user.name
12547: } elsif ($space eq 'name') {
1.218 albertel 12548: return $uname;
1.48 www 12549: # ---------------------------------------------------- Any other user namespace
1.29 www 12550: } else {
1.359 albertel 12551: my %reply;
12552: if (!$publicuser) {
12553: %reply=&get($space,[$qualifierrest],$udom,$uname);
12554: }
12555: return $reply{$qualifierrest};
1.48 www 12556: }
1.236 www 12557: } elsif ($realm eq 'query') {
12558: # ---------------------------------------------- pull stuff out of query string
1.384 albertel 12559: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
12560: [$spacequalifierrest]);
1.620 albertel 12561: return $env{'form.'.$spacequalifierrest};
1.236 www 12562: } elsif ($realm eq 'request') {
1.48 www 12563: # ------------------------------------------------------------- request.browser
12564: if ($space eq 'browser') {
1.1145 bisitz 12565: return $env{'browser.'.$qualifier};
1.57 www 12566: # ------------------------------------------------------------ request.filename
12567: } else {
1.620 albertel 12568: return $env{'request.'.$spacequalifierrest};
1.29 www 12569: }
1.28 www 12570: } elsif ($realm eq 'course') {
1.48 www 12571: # ---------------------------------------------------------- course.description
1.620 albertel 12572: return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57 www 12573: } elsif ($realm eq 'resource') {
1.165 www 12574:
1.620 albertel 12575: if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539 albertel 12576: if (!$symbparm) { $symbparm=&symbread(); }
12577: }
1.693 albertel 12578:
1.1172.2.30 raeburn 12579: if ($qualifier eq '') {
12580: if ($space eq 'title') {
12581: if (!$symbparm) { $symbparm = $env{'request.filename'}; }
12582: return &gettitle($symbparm);
12583: }
1.693 albertel 12584:
1.1172.2.30 raeburn 12585: if ($space eq 'map') {
12586: my ($map) = &decode_symb($symbparm);
12587: return &symbread($map);
12588: }
12589: if ($space eq 'maptitle') {
12590: my ($map) = &decode_symb($symbparm);
12591: return &gettitle($map);
12592: }
12593: if ($space eq 'filename') {
12594: if ($symbparm) {
12595: return &clutter((&decode_symb($symbparm))[2]);
12596: }
12597: return &hreflocation('',$env{'request.filename'});
1.905 albertel 12598: }
1.1172.2.30 raeburn 12599:
12600: if ((defined($courseid)) && ($courseid eq $env{'request.course.id'}) && $symbparm) {
12601: if ($space eq 'visibleparts') {
12602: my $navmap = Apache::lonnavmaps::navmap->new();
12603: my $item;
12604: if (ref($navmap)) {
12605: my $res = $navmap->getBySymb($symbparm);
12606: my $parts = $res->parts();
12607: if (ref($parts) eq 'ARRAY') {
12608: $item = join(',',@{$parts});
12609: }
12610: undef($navmap);
12611: }
12612: return $item;
12613: }
12614: }
12615: }
1.693 albertel 12616:
12617: my ($section, $group, @groups);
1.593 albertel 12618: my ($courselevelm,$courselevel);
1.1172.2.28 raeburn 12619: if (($courseid eq '') && ($cid)) {
12620: $courseid = $cid;
12621: }
12622: if (($symbparm && $courseid) &&
12623: (($courseid eq $env{'request.course.id'}) || ($courseid eq $cid))) {
1.165 www 12624:
1.218 albertel 12625: #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165 www 12626:
1.60 www 12627: # ----------------------------------------------------- Cascading lookup scheme
1.218 albertel 12628: my $symbp=$symbparm;
1.735 albertel 12629: my $mapp=&deversion((&decode_symb($symbp))[0]);
1.218 albertel 12630:
12631: my $symbparm=$symbp.'.'.$spacequalifierrest;
12632: my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
12633:
1.620 albertel 12634: if (($env{'user.name'} eq $uname) &&
12635: ($env{'user.domain'} eq $udom)) {
12636: $section=$env{'request.course.sec'};
1.733 raeburn 12637: @groups = split(/:/,$env{'request.course.groups'});
12638: @groups=&sort_course_groups($courseid,@groups);
1.218 albertel 12639: } else {
1.539 albertel 12640: if (! defined($usection)) {
1.551 albertel 12641: $section=&getsection($udom,$uname,$courseid);
1.539 albertel 12642: } else {
12643: $section = $usection;
12644: }
1.733 raeburn 12645: @groups = &get_users_groups($udom,$uname,$courseid);
1.218 albertel 12646: }
12647:
12648: my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
12649: my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
12650: my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
12651:
1.593 albertel 12652: $courselevel=$courseid.'.'.$spacequalifierrest;
1.218 albertel 12653: my $courselevelr=$courseid.'.'.$symbparm;
1.593 albertel 12654: $courselevelm=$courseid.'.'.$mapparm;
1.69 www 12655:
1.60 www 12656: # ----------------------------------------------------------- first, check user
1.624 albertel 12657:
12658: my $userreply=&resdata($uname,$udom,'user',
1.927 albertel 12659: ([$courselevelr,'resource'],
12660: [$courselevelm,'map' ],
12661: [$courselevel, 'course' ]));
1.931 albertel 12662: if (defined($userreply)) { return &get_reply($userreply); }
1.95 www 12663:
1.594 albertel 12664: # ------------------------------------------------ second, check some of course
1.684 raeburn 12665: my $coursereply;
1.691 raeburn 12666: if (@groups > 0) {
12667: $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
12668: $mapparm,$spacequalifierrest);
1.927 albertel 12669: if (defined($coursereply)) { return &get_reply($coursereply); }
1.684 raeburn 12670: }
1.96 www 12671:
1.684 raeburn 12672: $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.927 albertel 12673: $env{'course.'.$courseid.'.domain'},
12674: 'course',
12675: ([$seclevelr, 'resource'],
12676: [$seclevelm, 'map' ],
12677: [$seclevel, 'course' ],
12678: [$courselevelr,'resource']));
12679: if (defined($coursereply)) { return &get_reply($coursereply); }
1.200 www 12680:
1.60 www 12681: # ------------------------------------------------------ third, check map parms
1.218 albertel 12682: my %parmhash=();
12683: my $thisparm='';
12684: if (tie(%parmhash,'GDBM_File',
1.620 albertel 12685: $env{'request.course.fn'}.'_parms.db',
1.256 albertel 12686: &GDBM_READER(),0640)) {
1.218 albertel 12687: $thisparm=$parmhash{$symbparm};
12688: untie(%parmhash);
12689: }
1.927 albertel 12690: if ($thisparm) { return &get_reply([$thisparm,'resource']); }
1.218 albertel 12691: }
1.594 albertel 12692: # ------------------------------------------ fourth, look in resource metadata
1.71 www 12693:
1.1172.2.146. .13(raeb 12694:-23): my $what = $spacequalifierrest;
12695:-23): $what=~s/\./\_/;
12696:-23): my $filename;
1.282 albertel 12697: if (!$symbparm) { $symbparm=&symbread(); }
12698: if ($symbparm) {
1.409 www 12699: $filename=(&decode_symb($symbparm))[2];
1.282 albertel 12700: } else {
1.620 albertel 12701: $filename=$env{'request.filename'};
1.282 albertel 12702: }
1.1172.2.146. .13(raeb 12703:-23): my $toolsymb;
12704:-23): if (($filename =~ /ext\.tool$/) && ($what ne '0_gradable')) {
12705:-23): $toolsymb = $symbparm;
12706:-23): }
12707:-23): my $metadata=&metadata($filename,$what,$toolsymb);
1.927 albertel 12708: if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.1172.2.146. .13(raeb 12709:-23): $metadata=&metadata($filename,'parameter_'.$what,$toolsymb);
1.927 albertel 12710: if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.142 www 12711:
1.1172.2.146. .13(raeb 12712:-23): # ----------------------------------------------- fifth, look in rest of course
1.593 albertel 12713: if ($symbparm && defined($courseid) &&
1.620 albertel 12714: $courseid eq $env{'request.course.id'}) {
1.624 albertel 12715: my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
12716: $env{'course.'.$courseid.'.domain'},
12717: 'course',
1.927 albertel 12718: ([$courselevelm,'map' ],
12719: [$courselevel, 'course']));
12720: if (defined($coursereply)) { return &get_reply($coursereply); }
1.593 albertel 12721: }
1.145 www 12722: # ------------------------------------------------------------------ Cascade up
1.218 albertel 12723: unless ($space eq '0') {
1.336 albertel 12724: my @parts=split(/_/,$space);
12725: my $id=pop(@parts);
12726: my $part=join('_',@parts);
12727: if ($part eq '') { $part='0'; }
1.927 albertel 12728: my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395 albertel 12729: $symbparm,$udom,$uname,$section,1);
1.938 raeburn 12730: if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
1.218 albertel 12731: }
1.395 albertel 12732: if ($recurse) { return undef; }
1.1172.2.146. .13(raeb 12733:-23): my $pack_def=&packages_tab_default($filename,$varname,$toolsymb);
1.927 albertel 12734: if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
1.48 www 12735: # ---------------------------------------------------- Any other user namespace
12736: } elsif ($realm eq 'environment') {
12737: # ----------------------------------------------------------------- environment
1.620 albertel 12738: if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
12739: return $env{'environment.'.$spacequalifierrest};
1.219 albertel 12740: } else {
1.770 albertel 12741: if ($uname eq 'anonymous' && $udom eq '') {
12742: return '';
12743: }
1.219 albertel 12744: my %returnhash=&userenvironment($udom,$uname,
12745: $spacequalifierrest);
12746: return $returnhash{$spacequalifierrest};
12747: }
1.28 www 12748: } elsif ($realm eq 'system') {
1.48 www 12749: # ----------------------------------------------------------------- system.time
12750: if ($space eq 'time') {
12751: return time;
12752: }
1.696 albertel 12753: } elsif ($realm eq 'server') {
12754: # ----------------------------------------------------------------- system.time
12755: if ($space eq 'name') {
12756: return $ENV{'SERVER_NAME'};
12757: }
1.1172.2.146. .1(raebu 12758:22): } elsif ($realm eq 'client') {
12759:22): if ($space eq 'remote_addr') {
12760:22): return &get_requestor_ip();
12761:22): }
1.28 www 12762: }
1.48 www 12763: return '';
1.61 www 12764: }
12765:
1.927 albertel 12766: sub get_reply {
12767: my ($reply_value) = @_;
1.940 raeburn 12768: if (ref($reply_value) eq 'ARRAY') {
12769: if (wantarray) {
12770: return @$reply_value;
12771: }
12772: return $reply_value->[0];
12773: } else {
12774: return $reply_value;
1.927 albertel 12775: }
12776: }
12777:
1.691 raeburn 12778: sub check_group_parms {
12779: my ($courseid,$groups,$symbparm,$mapparm,$what) = @_;
12780: my @groupitems = ();
12781: my $resultitem;
1.927 albertel 12782: my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$what,'course']);
1.691 raeburn 12783: foreach my $group (@{$groups}) {
12784: foreach my $level (@levels) {
1.927 albertel 12785: my $item = $courseid.'.['.$group.'].'.$level->[0];
12786: push(@groupitems,[$item,$level->[1]]);
1.691 raeburn 12787: }
12788: }
12789: my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
12790: $env{'course.'.$courseid.'.domain'},
12791: 'course',@groupitems);
12792: return $coursereply;
12793: }
12794:
1.1172.2.146. .1(raebu 12795:22): sub get_map_hierarchy {
12796:22): my ($mapname,$courseid) = @_;
12797:22): my @recurseup = ();
12798:22): if ($mapname) {
12799:22): if (($cachedmapkey eq $courseid) &&
12800:22): (abs($cachedmaptime-time)<5)) {
12801:22): if (ref($cachedmaps{$mapname}) eq 'ARRAY') {
12802:22): return @{$cachedmaps{$mapname}};
12803:22): }
12804:22): }
12805:22): my $navmap = Apache::lonnavmaps::navmap->new();
12806:22): if (ref($navmap)) {
12807:22): @recurseup = $navmap->recurseup_maps($mapname);
12808:22): undef($navmap);
12809:22): $cachedmaps{$mapname} = \@recurseup;
12810:22): $cachedmaptime=time;
12811:22): $cachedmapkey=$courseid;
12812:22): }
12813:22): }
12814:22): return @recurseup;
12815:22): }
12816:22):
.2(raebu 12817:22): }
.1(raebu 12818:22):
1.691 raeburn 12819: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733 raeburn 12820: my ($courseid,@groups) = @_;
12821: @groups = sort(@groups);
1.691 raeburn 12822: return @groups;
12823: }
12824:
1.395 albertel 12825: sub packages_tab_default {
1.1172.2.146. .13(raeb 12826:-23): my ($uri,$varname,$toolsymb)=@_;
1.395 albertel 12827: my (undef,$part,$name)=split(/\./,$varname);
1.738 albertel 12828:
12829: my (@extension,@specifics,$do_default);
1.1172.2.146. .13(raeb 12830:-23): foreach my $package (split(/,/,&metadata($uri,'packages',$toolsymb))) {
1.395 albertel 12831: my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738 albertel 12832: if ($pack_type eq 'default') {
12833: $do_default=1;
12834: } elsif ($pack_type eq 'extension') {
12835: push(@extension,[$package,$pack_type,$pack_part]);
1.885 albertel 12836: } elsif ($pack_part eq $part || $pack_type eq 'part') {
1.848 albertel 12837: # only look at packages defaults for packages that this id is
1.738 albertel 12838: push(@specifics,[$package,$pack_type,$pack_part]);
12839: }
12840: }
12841: # first look for a package that matches the requested part id
12842: foreach my $package (@specifics) {
12843: my (undef,$pack_type,$pack_part)=@{$package};
12844: next if ($pack_part ne $part);
12845: if (defined($packagetab{"$pack_type&$name&default"})) {
12846: return $packagetab{"$pack_type&$name&default"};
12847: }
12848: }
12849: # look for any possible matching non extension_ package
12850: foreach my $package (@specifics) {
12851: my (undef,$pack_type,$pack_part)=@{$package};
1.468 albertel 12852: if (defined($packagetab{"$pack_type&$name&default"})) {
12853: return $packagetab{"$pack_type&$name&default"};
12854: }
1.585 albertel 12855: if ($pack_type eq 'part') { $pack_part='0'; }
1.468 albertel 12856: if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
12857: return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395 albertel 12858: }
12859: }
1.738 albertel 12860: # look for any posible extension_ match
12861: foreach my $package (@extension) {
12862: my ($package,$pack_type)=@{$package};
12863: if (defined($packagetab{"$pack_type&$name&default"})) {
12864: return $packagetab{"$pack_type&$name&default"};
12865: }
12866: if (defined($packagetab{$package."&$name&default"})) {
12867: return $packagetab{$package."&$name&default"};
12868: }
12869: }
12870: # look for a global default setting
12871: if ($do_default && defined($packagetab{"default&$name&default"})) {
12872: return $packagetab{"default&$name&default"};
12873: }
1.395 albertel 12874: return undef;
12875: }
12876:
1.334 albertel 12877: sub add_prefix_and_part {
12878: my ($prefix,$part)=@_;
12879: my $keyroot;
12880: if (defined($prefix) && $prefix !~ /^__/) {
12881: # prefix that has a part already
12882: $keyroot=$prefix;
12883: } elsif (defined($prefix)) {
12884: # prefix that is missing a part
12885: if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
12886: } else {
12887: # no prefix at all
12888: if (defined($part)) { $keyroot='_'.$part; }
12889: }
12890: return $keyroot;
12891: }
12892:
1.71 www 12893: # ---------------------------------------------------------------- Get metadata
12894:
1.599 albertel 12895: my %metaentry;
1.1070 www 12896: my %importedpartids;
1.1172.2.99 raeburn 12897: my %importedrespids;
1.71 www 12898: sub metadata {
1.1172.2.146. .13(raeb 12899:-23): my ($uri,$what,$toolsymb,$liburi,$prefix,$depthcount)=@_;
1.71 www 12900: $uri=&declutter($uri);
1.288 albertel 12901: # if it is a non metadata possible uri return quickly
1.529 albertel 12902: if (($uri eq '') ||
12903: (($uri =~ m|^/*adm/|) &&
1.1172.2.146. .1(raebu 12904:22): ($uri !~ m|^adm/includes|) && ($uri !~ m{/(smppg|bulletinboard|ext\.tool)$})) ||
1.1108 raeburn 12905: ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
1.924 albertel 12906: return undef;
12907: }
1.1172.2.49 raeburn 12908: if (($uri =~ /^priv/ || $uri=~m{^home/httpd/html/priv})
1.924 albertel 12909: && &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
1.468 albertel 12910: return undef;
1.288 albertel 12911: }
1.73 www 12912: my $filename=$uri;
12913: $uri=~s/\.meta$//;
1.172 www 12914: #
12915: # Is the metadata already cached?
1.177 www 12916: # Look at timestamp of caching
1.172 www 12917: # Everything is cached by the main uri, libraries are never directly cached
12918: #
1.428 albertel 12919: if (!defined($liburi)) {
1.599 albertel 12920: my ($result,$cached)=&is_cached_new('meta',$uri);
1.428 albertel 12921: if (defined($cached)) { return $result->{':'.$what}; }
12922: }
1.1172.2.146. .13(raeb 12923:-23):
12924:-23): #
12925:-23): # If the uri is for an external tool the file from
12926:-23): # which metadata should be retrieved depends on whether
12927:-23): # the tool had been configured to be gradable (set in the Course
12928:-23): # Editor or Resource Editor).
12929:-23): #
12930:-23): # If a valid symb has been included as the third arg in the call
12931:-23): # to &metadata() that can be used to retrieve the value of
12932:-23): # parameter_0_gradable set for the resource, and included in the
12933:-23): # uploaded map containing the tool. The value is retrieved via
12934:-23): # &EXT(), if a valid symb is available. Otherwise the value of
12935:-23): # gradable in the exttool_$marker.db file for the tool instance
12936:-23): # is retrieved via &get().
12937:-23): #
12938:-23): # When lonuserstate::traceroute() calls lonnet::EXT() for
12939:-23): # hiddenresource and encrypturl (during course initialization)
12940:-23): # the map-level parameter for resource.0.gradable included in the
12941:-23): # uploaded map containing the tool will not yet have been stored
12942:-23): # in the user_course_parms.db file for the user's session, so in
12943:-23): # this case fall back to retrieving gradable status from the
12944:-23): # exttool_$marker.db file.
12945:-23): #
12946:-23): # In order to avoid an infinite loop, &metadata() will return
12947:-23): # before a call to &EXT(), if the uri is for an external tool
12948:-23): # and the $what for which metadata is being requested is
12949:-23): # parameter_0_gradable or 0_gradable.
12950:-23): #
12951:-23):
12952:-23): if ($uri =~ /ext\.tool$/) {
12953:-23): if (($what eq 'parameter_0_gradable') || ($what eq '0_gradable')) {
12954:-23): return;
12955:-23): } else {
12956:-23): my ($checked,$use_passback);
12957:-23): if ($toolsymb ne '') {
12958:-23): (undef,undef,my $tooluri) = &decode_symb($toolsymb);
12959:-23): if (($tooluri eq $uri) && (&EXT('resource.0.gradable',$toolsymb))) {
12960:-23): $checked = 1;
12961:-23): if (&EXT('resource.0.gradable',$toolsymb) =~ /^yes$/i) {
12962:-23): $use_passback = 1;
12963:-23): }
12964:-23): }
12965:-23): }
12966:-23): unless ($checked) {
12967:-23): my ($ignore,$cdom,$cnum,$marker) = split(m{/},$uri);
12968:-23): $marker=~s/\D//g;
12969:-23): if ($marker) {
12970:-23): my %toolsettings=&get('exttool_'.$marker,['gradable'],$cdom,$cnum);
12971:-23): $use_passback = $toolsettings{'gradable'};
12972:-23): }
12973:-23): }
12974:-23): if ($use_passback) {
12975:-23): $filename = '/home/httpd/html/res/lib/templates/LTIpassback.tool';
12976:-23): } else {
12977:-23): $filename = '/home/httpd/html/res/lib/templates/LTIstandard.tool';
12978:-23): }
12979:-23): }
12980:-23): }
12981:-23):
1.428 albertel 12982: {
1.1069 www 12983: # Imported parts would go here
1.1172.2.99 raeburn 12984: my @origfiletagids=();
1.1069 www 12985: my $importedparts=0;
1.1172.2.99 raeburn 12986:
12987: # Imported responseids would go here
12988: my $importedresponses=0;
1.172 www 12989: #
12990: # Is this a recursive call for a library?
12991: #
1.599 albertel 12992: # if (! exists($metacache{$uri})) {
12993: # $metacache{$uri}={};
12994: # }
1.924 albertel 12995: my $cachetime = 60*60;
1.171 www 12996: if ($liburi) {
12997: $liburi=&declutter($liburi);
12998: $filename=$liburi;
1.401 bowersj2 12999: } else {
1.599 albertel 13000: &devalidate_cache_new('meta',$uri);
13001: undef(%metaentry);
1.401 bowersj2 13002: }
1.140 www 13003: my %metathesekeys=();
1.73 www 13004: unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489 albertel 13005: my $metastring;
1.1140 www 13006: if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
1.929 albertel 13007: my $which = &hreflocation('','/'.($liburi || $uri));
1.924 albertel 13008: $metastring =
1.929 albertel 13009: &Apache::lonnet::ssi_body($which,
1.924 albertel 13010: ('grade_target' => 'meta'));
13011: $cachetime = 1; # only want this cached in the child not long term
1.1108 raeburn 13012: } elsif (($uri !~ m -^(editupload)/-) &&
13013: ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
1.543 albertel 13014: my $file=&filelocation('',&clutter($filename));
1.599 albertel 13015: #push(@{$metaentry{$uri.'.file'}},$file);
1.543 albertel 13016: $metastring=&getfile($file);
1.489 albertel 13017: }
1.208 albertel 13018: my $parser=HTML::LCParser->new(\$metastring);
1.71 www 13019: my $token;
1.140 www 13020: undef %metathesekeys;
1.71 www 13021: while ($token=$parser->get_token) {
1.339 albertel 13022: if ($token->[0] eq 'S') {
13023: if (defined($token->[2]->{'package'})) {
1.172 www 13024: #
13025: # This is a package - get package info
13026: #
1.339 albertel 13027: my $package=$token->[2]->{'package'};
13028: my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
13029: if (defined($token->[2]->{'id'})) {
13030: $keyroot.='_'.$token->[2]->{'id'};
13031: }
1.599 albertel 13032: if ($metaentry{':packages'}) {
13033: $metaentry{':packages'}.=','.$package.$keyroot;
1.339 albertel 13034: } else {
1.599 albertel 13035: $metaentry{':packages'}=$package.$keyroot;
1.339 albertel 13036: }
1.736 albertel 13037: foreach my $pack_entry (keys(%packagetab)) {
1.432 albertel 13038: my $part=$keyroot;
13039: $part=~s/^\_//;
1.736 albertel 13040: if ($pack_entry=~/^\Q$package\E\&/ ||
13041: $pack_entry=~/^\Q$package\E_0\&/) {
13042: my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395 albertel 13043: # ignore package.tab specified default values
13044: # here &package_tab_default() will fetch those
13045: if ($subp eq 'default') { next; }
1.736 albertel 13046: my $value=$packagetab{$pack_entry};
1.432 albertel 13047: my $unikey;
13048: if ($pack =~ /_0$/) {
13049: $unikey='parameter_0_'.$name;
13050: $part=0;
13051: } else {
13052: $unikey='parameter'.$keyroot.'_'.$name;
13053: }
1.339 albertel 13054: if ($subp eq 'display') {
13055: $value.=' [Part: '.$part.']';
13056: }
1.599 albertel 13057: $metaentry{':'.$unikey.'.part'}=$part;
1.395 albertel 13058: $metathesekeys{$unikey}=1;
1.599 albertel 13059: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
13060: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.339 albertel 13061: }
1.599 albertel 13062: if (defined($metaentry{':'.$unikey.'.default'})) {
13063: $metaentry{':'.$unikey}=
13064: $metaentry{':'.$unikey.'.default'};
1.356 albertel 13065: }
1.339 albertel 13066: }
13067: }
13068: } else {
1.172 www 13069: #
13070: # This is not a package - some other kind of start tag
1.339 albertel 13071: #
13072: my $entry=$token->[1];
1.1068 www 13073: my $unikey='';
1.175 www 13074:
1.339 albertel 13075: if ($entry eq 'import') {
1.175 www 13076: #
13077: # Importing a library here
1.339 albertel 13078: #
1.1067 www 13079: my $location=$parser->get_text('/import');
13080: my $dir=$filename;
13081: $dir=~s|[^/]*$||;
13082: $location=&filelocation($dir,$location);
1.1172.2.99 raeburn 13083:
13084: my $importid=$token->[2]->{'id'};
1.1068 www 13085: my $importmode=$token->[2]->{'importmode'};
1.1172.2.99 raeburn 13086: #
13087: # Check metadata for imported file to
13088: # see if it contained response items
13089: #
13090: my %currmetaentry = %metaentry;
13091: my $libresponseorder = &metadata($location,'responseorder');
13092: my $origfile;
13093: if ($libresponseorder ne '') {
13094: if ($#origfiletagids<0) {
13095: undef(%importedrespids);
13096: undef(%importedpartids);
13097: }
13098: @{$importedrespids{$importid}} = split(/\s*,\s*/,$libresponseorder);
13099: if (@{$importedrespids{$importid}} > 0) {
13100: $importedresponses = 1;
13101: # We need to get the original file and the imported file to get the response order correct
13102: # Load and inspect original file
13103: if ($#origfiletagids<0) {
13104: my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
13105: $origfile=&getfile($origfilelocation);
13106: @origfiletagids=($origfile=~/<((?:\w+)response|import|part)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
13107: }
13108: }
13109: }
13110: # Do not overwrite contents of %metaentry hash for resource itself with
13111: # hash populated for imported library file
13112: %metaentry = %currmetaentry;
13113: undef(%currmetaentry);
1.1068 www 13114: if ($importmode eq 'problem') {
1.1069 www 13115: # Import as problem/response
1.1068 www 13116: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
13117: } elsif ($importmode eq 'part') {
13118: # Import as part(s)
1.1069 www 13119: $importedparts=1;
13120: # We need to get the original file and the imported file to get the part order correct
13121: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
1.1172.2.99 raeburn 13122: # Load and inspect original file if we didn't do that already
13123: if ($#origfiletagids<0) {
13124: undef(%importedrespids);
13125: undef(%importedpartids);
13126: if ($origfile eq '') {
13127: my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
13128: $origfile=&getfile($origfilelocation);
13129: @origfiletagids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
13130: }
1.1070 www 13131: }
13132:
1.1069 www 13133: # Load and inspect imported file
13134: my $impfile=&getfile($location);
13135: my @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
13136: if ($#impfilepartids>=0) {
13137: # This problem had parts
1.1070 www 13138: $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
1.1069 www 13139: } else {
13140: # Importing by turning a single problem into a problem part
13141: # It gets the import-tags ID as part-ID
13142: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
1.1070 www 13143: $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
1.1069 www 13144: }
1.1068 www 13145: } else {
13146: # Normal import
13147: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
13148: if (defined($token->[2]->{'id'})) {
13149: $unikey.='_'.$token->[2]->{'id'};
13150: }
1.1067 www 13151: }
13152:
1.339 albertel 13153: if ($depthcount<20) {
1.736 albertel 13154: my $metadata =
1.1172.2.146. .13(raeb 13155:-23): &metadata($uri,'keys',$toolsymb,$location,$unikey,
1.736 albertel 13156: $depthcount+1);
13157: foreach my $meta (split(',',$metadata)) {
13158: $metaentry{':'.$meta}=$metaentry{':'.$meta};
13159: $metathesekeys{$meta}=1;
1.339 albertel 13160: }
1.1068 www 13161:
13162: }
1.1067 www 13163: } else {
13164: #
13165: # Not importing, some other kind of non-package, non-library start tag
13166: #
13167: $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
13168: if (defined($token->[2]->{'id'})) {
13169: $unikey.='_'.$token->[2]->{'id'};
13170: }
1.339 albertel 13171: if (defined($token->[2]->{'name'})) {
13172: $unikey.='_'.$token->[2]->{'name'};
13173: }
13174: $metathesekeys{$unikey}=1;
1.736 albertel 13175: foreach my $param (@{$token->[3]}) {
13176: $metaentry{':'.$unikey.'.'.$param} =
13177: $token->[2]->{$param};
1.339 albertel 13178: }
13179: my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599 albertel 13180: my $default=$metaentry{':'.$unikey.'.default'};
1.339 albertel 13181: if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
13182: # only ws inside the tag, and not in default, so use default
13183: # as value
1.599 albertel 13184: $metaentry{':'.$unikey}=$default;
1.908 albertel 13185: } elsif ( $internaltext =~ /\S/ ) {
13186: # something interesting inside the tag
13187: $metaentry{':'.$unikey}=$internaltext;
1.339 albertel 13188: } else {
1.908 albertel 13189: # no interesting values, don't set a default
1.339 albertel 13190: }
1.172 www 13191: # end of not-a-package not-a-library import
1.339 albertel 13192: }
1.172 www 13193: # end of not-a-package start tag
1.339 albertel 13194: }
1.172 www 13195: # the next is the end of "start tag"
1.339 albertel 13196: }
13197: }
1.483 albertel 13198: my ($extension) = ($uri =~ /\.(\w+)$/);
1.883 albertel 13199: $extension = lc($extension);
13200: if ($extension eq 'htm') { $extension='html'; }
13201:
1.737 albertel 13202: foreach my $key (keys(%packagetab)) {
1.483 albertel 13203: #no specific packages #how's our extension
13204: if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488 albertel 13205: &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483 albertel 13206: \%metathesekeys);
13207: }
1.883 albertel 13208:
13209: if (!exists($metaentry{':packages'})
13210: || $packagetab{"import_defaults&extension_$extension"}) {
1.737 albertel 13211: foreach my $key (keys(%packagetab)) {
1.483 albertel 13212: #no specific packages well let's get default then
13213: if ($key!~/^default&/) { next; }
1.488 albertel 13214: &metadata_create_package_def($uri,$key,'default',
1.483 albertel 13215: \%metathesekeys);
13216: }
13217: }
1.338 www 13218: # are there custom rights to evaluate
1.599 albertel 13219: if ($metaentry{':copyright'} eq 'custom') {
1.339 albertel 13220:
1.338 www 13221: #
13222: # Importing a rights file here
1.339 albertel 13223: #
13224: unless ($depthcount) {
1.599 albertel 13225: my $location=$metaentry{':customdistributionfile'};
1.339 albertel 13226: my $dir=$filename;
13227: $dir=~s|[^/]*$||;
13228: $location=&filelocation($dir,$location);
1.736 albertel 13229: my $rights_metadata =
1.1172.2.146. .13(raeb 13230:-23): &metadata($uri,'keys',$toolsymb,$location,'_rights',
1.736 albertel 13231: $depthcount+1);
13232: foreach my $rights (split(',',$rights_metadata)) {
13233: #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
13234: $metathesekeys{$rights}=1;
1.339 albertel 13235: }
13236: }
13237: }
1.737 albertel 13238: # uniqifiy package listing
13239: my %seen;
13240: my @uniq_packages =
13241: grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
13242: $metaentry{':packages'} = join(',',@uniq_packages);
13243:
1.1172.2.99 raeburn 13244: if (($importedresponses) || ($importedparts)) {
13245: if ($importedparts) {
1.1070 www 13246: # We had imported parts and need to rebuild partorder
1.1172.2.99 raeburn 13247: $metaentry{':partorder'}='';
13248: $metathesekeys{'partorder'}=1;
13249: }
13250: if ($importedresponses) {
13251: # We had imported responses and need to rebuild responseorder
13252: $metaentry{':responseorder'}='';
13253: $metathesekeys{'responseorder'}=1;
13254: }
13255: for (my $index=0;$index<$#origfiletagids;$index+=2) {
13256: my $origid = $origfiletagids[$index+1];
13257: if ($origfiletagids[$index] eq 'part') {
13258: # Original part, part of the problem
13259: if ($importedparts) {
13260: $metaentry{':partorder'}.=','.$origid;
13261: }
13262: } elsif ($origfiletagids[$index] eq 'import') {
13263: if ($importedparts) {
13264: # We have imported parts at this position
13265: $metaentry{':partorder'}.=','.$importedpartids{$origid};
13266: }
13267: if ($importedresponses) {
13268: # We have imported responses at this position
13269: if (ref($importedrespids{$origid}) eq 'ARRAY') {
13270: $metaentry{':responseorder'}.=','.join(',',map { $origid.'_'.$_ } @{$importedrespids{$origid}});
13271: }
13272: }
13273: } else {
13274: # Original response item, part of the problem
13275: if ($importedresponses) {
13276: $metaentry{':responseorder'}.=','.$origid;
13277: }
13278: }
13279: }
13280: if ($importedparts) {
13281: $metaentry{':partorder'}=~s/^\,//;
13282: }
13283: if ($importedresponses) {
13284: $metaentry{':responseorder'}=~s/^\,//;
13285: }
1.1070 www 13286: }
13287:
1.737 albertel 13288: $metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599 albertel 13289: &metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
1.1172.2.58 raeburn 13290: $metaentry{':allpossiblekeys'}=join(',',keys(%metathesekeys));
1.924 albertel 13291: &do_cache_new('meta',$uri,\%metaentry,$cachetime);
1.177 www 13292: # this is the end of "was not already recently cached
1.71 www 13293: }
1.599 albertel 13294: return $metaentry{':'.$what};
1.261 albertel 13295: }
13296:
1.488 albertel 13297: sub metadata_create_package_def {
1.483 albertel 13298: my ($uri,$key,$package,$metathesekeys)=@_;
13299: my ($pack,$name,$subp)=split(/\&/,$key);
13300: if ($subp eq 'default') { next; }
13301:
1.599 albertel 13302: if (defined($metaentry{':packages'})) {
13303: $metaentry{':packages'}.=','.$package;
1.483 albertel 13304: } else {
1.599 albertel 13305: $metaentry{':packages'}=$package;
1.483 albertel 13306: }
13307: my $value=$packagetab{$key};
13308: my $unikey;
13309: $unikey='parameter_0_'.$name;
1.599 albertel 13310: $metaentry{':'.$unikey.'.part'}=0;
1.483 albertel 13311: $$metathesekeys{$unikey}=1;
1.599 albertel 13312: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
13313: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.483 albertel 13314: }
1.599 albertel 13315: if (defined($metaentry{':'.$unikey.'.default'})) {
13316: $metaentry{':'.$unikey}=
13317: $metaentry{':'.$unikey.'.default'};
1.483 albertel 13318: }
13319: }
13320:
1.261 albertel 13321: sub metadata_generate_part0 {
13322: my ($metadata,$metacache,$uri) = @_;
13323: my %allnames;
1.737 albertel 13324: foreach my $metakey (keys(%$metadata)) {
1.261 albertel 13325: if ($metakey=~/^parameter\_(.*)/) {
1.428 albertel 13326: my $part=$$metacache{':'.$metakey.'.part'};
13327: my $name=$$metacache{':'.$metakey.'.name'};
1.356 albertel 13328: if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261 albertel 13329: $allnames{$name}=$part;
13330: }
13331: }
13332: }
13333: foreach my $name (keys(%allnames)) {
13334: $$metadata{"parameter_0_$name"}=1;
1.428 albertel 13335: my $key=":parameter_0_$name";
1.261 albertel 13336: $$metacache{"$key.part"}='0';
13337: $$metacache{"$key.name"}=$name;
1.428 albertel 13338: $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261 albertel 13339: $allnames{$name}.'_'.$name.
13340: '.type'};
1.428 albertel 13341: my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261 albertel 13342: '.display'};
1.644 www 13343: my $expr='[Part: '.$allnames{$name}.']';
1.479 albertel 13344: $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261 albertel 13345: $$metacache{"$key.display"}=$olddis;
13346: }
1.71 www 13347: }
13348:
1.764 albertel 13349: # ------------------------------------------------------ Devalidate title cache
13350:
13351: sub devalidate_title_cache {
13352: my ($url)=@_;
13353: if (!$env{'request.course.id'}) { return; }
13354: my $symb=&symbread($url);
13355: if (!$symb) { return; }
13356: my $key=$env{'request.course.id'}."\0".$symb;
13357: &devalidate_cache_new('title',$key);
13358: }
13359:
1.1014 droeschl 13360: # ------------------------------------------------- Get the title of a course
13361:
13362: sub current_course_title {
13363: return $env{ 'course.' . $env{'request.course.id'} . '.description' };
13364: }
1.301 www 13365: # ------------------------------------------------- Get the title of a resource
13366:
13367: sub gettitle {
13368: my $urlsymb=shift;
13369: my $symb=&symbread($urlsymb);
1.534 albertel 13370: if ($symb) {
1.620 albertel 13371: my $key=$env{'request.course.id'}."\0".$symb;
1.599 albertel 13372: my ($result,$cached)=&is_cached_new('title',$key);
1.575 albertel 13373: if (defined($cached)) {
13374: return $result;
13375: }
1.534 albertel 13376: my ($map,$resid,$url)=&decode_symb($symb);
13377: my $title='';
1.907 albertel 13378: if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
13379: $title = $env{'course.'.$env{'request.course.id'}.'.description'};
13380: } else {
13381: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13382: &GDBM_READER(),0640)) {
13383: my $mapid=$bighash{'map_pc_'.&clutter($map)};
13384: $title=$bighash{'title_'.$mapid.'.'.$resid};
13385: untie(%bighash);
13386: }
1.534 albertel 13387: }
13388: $title=~s/\&colon\;/\:/gs;
13389: if ($title) {
1.1159 www 13390: # Remember both $symb and $title for dynamic metadata
13391: $accesshash{$symb.'___crstitle'}=$title;
1.1161 www 13392: $accesshash{&declutter($map).'___'.&declutter($url).'___usage'}=time;
1.1159 www 13393: # Cache this title and then return it
1.599 albertel 13394: return &do_cache_new('title',$key,$title,600);
1.534 albertel 13395: }
13396: $urlsymb=$url;
13397: }
13398: my $title=&metadata($urlsymb,'title');
13399: if (!$title) { $title=(split('/',$urlsymb))[-1]; }
13400: return $title;
1.301 www 13401: }
1.613 albertel 13402:
1.614 albertel 13403: sub get_slot {
13404: my ($which,$cnum,$cdom)=@_;
13405: if (!$cnum || !$cdom) {
1.790 albertel 13406: (undef,my $courseid)=&whichuser();
1.620 albertel 13407: $cdom=$env{'course.'.$courseid.'.domain'};
13408: $cnum=$env{'course.'.$courseid.'.num'};
1.614 albertel 13409: }
1.703 albertel 13410: my $key=join("\0",'slots',$cdom,$cnum,$which);
13411: my %slotinfo;
13412: if (exists($remembered{$key})) {
13413: $slotinfo{$which} = $remembered{$key};
13414: } else {
13415: %slotinfo=&get('slots',[$which],$cdom,$cnum);
13416: &Apache::lonhomework::showhash(%slotinfo);
13417: my ($tmp)=keys(%slotinfo);
13418: if ($tmp=~/^error:/) { return (); }
13419: $remembered{$key} = $slotinfo{$which};
13420: }
1.616 albertel 13421: if (ref($slotinfo{$which}) eq 'HASH') {
13422: return %{$slotinfo{$which}};
13423: }
13424: return $slotinfo{$which};
1.614 albertel 13425: }
1.1150 raeburn 13426:
13427: sub get_reservable_slots {
13428: my ($cnum,$cdom,$uname,$udom) = @_;
13429: my $now = time;
13430: my $reservable_info;
13431: my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
13432: if (exists($remembered{$key})) {
13433: $reservable_info = $remembered{$key};
13434: } else {
13435: my %resv;
13436: ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
13437: &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
13438: $reservable_info = \%resv;
13439: $remembered{$key} = $reservable_info;
13440: }
13441: return $reservable_info;
13442: }
13443:
13444: sub get_course_slots {
13445: my ($cnum,$cdom) = @_;
13446: my $hashid=$cnum.':'.$cdom;
13447: my ($result,$cached) = &Apache::lonnet::is_cached_new('allslots',$hashid);
13448: if (defined($cached)) {
13449: if (ref($result) eq 'HASH') {
13450: return %{$result};
13451: }
13452: } else {
13453: my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
13454: my ($tmp) = keys(%slots);
13455: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.1172.2.23 raeburn 13456: &do_cache_new('allslots',$hashid,\%slots,600);
1.1150 raeburn 13457: return %slots;
13458: }
13459: }
13460: return;
13461: }
13462:
13463: sub devalidate_slots_cache {
13464: my ($cnum,$cdom)=@_;
13465: my $hashid=$cnum.':'.$cdom;
13466: &devalidate_cache_new('allslots',$hashid);
13467: }
13468:
1.1172.2.8 raeburn 13469: sub get_coursechange {
13470: my ($cdom,$cnum) = @_;
13471: if ($cdom eq '' || $cnum eq '') {
13472: return unless ($env{'request.course.id'});
13473: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
13474: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
13475: }
13476: my $hashid=$cdom.'_'.$cnum;
13477: my ($change,$cached)=&is_cached_new('crschange',$hashid);
13478: if ((defined($cached)) && ($change ne '')) {
13479: return $change;
13480: } else {
13481: my %crshash;
13482: %crshash = &get('environment',['internal.contentchange'],$cdom,$cnum);
13483: if ($crshash{'internal.contentchange'} eq '') {
13484: $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
13485: if ($change eq '') {
13486: %crshash = &get('environment',['internal.created'],$cdom,$cnum);
13487: $change = $crshash{'internal.created'};
13488: }
13489: } else {
13490: $change = $crshash{'internal.contentchange'};
13491: }
13492: my $cachetime = 600;
13493: &do_cache_new('crschange',$hashid,$change,$cachetime);
13494: }
13495: return $change;
13496: }
13497:
13498: sub devalidate_coursechange_cache {
1.1172.2.146. .9(raebu 13499:23): my ($cdom,$cnum)=@_;
13500:23): my $hashid=$cdom.'_'.$cnum;
1.1172.2.8 raeburn 13501: &devalidate_cache_new('crschange',$hashid);
13502: }
13503:
1.1172.2.146. .9(raebu 13504:23): sub get_suppchange {
13505:23): my ($cdom,$cnum) = @_;
13506:23): if ($cdom eq '' || $cnum eq '') {
13507:23): return unless ($env{'request.course.id'});
13508:23): $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
13509:23): $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
13510:23): }
13511:23): my $hashid=$cdom.'_'.$cnum;
13512:23): my ($change,$cached)=&is_cached_new('suppchange',$hashid);
13513:23): if ((defined($cached)) && ($change ne '')) {
13514:23): return $change;
13515:23): } else {
13516:23): my %crshash = &get('environment',['internal.supplementalchange'],$cdom,$cnum);
13517:23): if ($crshash{'internal.supplementalchange'} eq '') {
13518:23): $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
13519:23): if ($change eq '') {
13520:23): %crshash = &get('environment',['internal.created'],$cdom,$cnum);
13521:23): $change = $crshash{'internal.created'};
13522:23): }
13523:23): } else {
13524:23): $change = $crshash{'internal.supplementalchange'};
13525:23): }
13526:23): my $cachetime = 600;
13527:23): &do_cache_new('suppchange',$hashid,$change,$cachetime);
13528:23): }
13529:23): return $change;
13530:23): }
13531:23):
13532:23): sub devalidate_suppchange_cache {
13533:23): my ($cdom,$cnum)=@_;
13534:23): my $hashid=$cdom.'_'.$cnum;
13535:23): &devalidate_cache_new('suppchange',$hashid);
13536:23): }
13537:23):
13538:23): sub update_supp_caches {
13539:23): my ($cdom,$cnum) = @_;
13540:23): my %servers = &internet_dom_servers($cdom);
13541:23): my @ids=¤t_machine_ids();
13542:23): foreach my $server (keys(%servers)) {
13543:23): next if (grep(/^\Q$server\E$/,@ids));
13544:23): my $hashid=$cnum.':'.$cdom;
13545:23): my $cachekey = &escape('showsupp').':'.&escape($hashid);
13546:23): &remote_devalidate_cache($server,[$cachekey]);
13547:23): }
13548:23): &has_unhidden_suppfiles($cnum,$cdom,1,1);
13549:23): &count_supptools($cnum,$cdom,1);
13550:23): my $now = time;
13551:23): if ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
13552:23): &Apache::lonnet::appenv({'request.course.suppupdated' => $now});
13553:23): }
13554:23): &put('environment',{'internal.supplementalchange' => $now},
13555:23): $cdom,$cnum);
13556:23): &Apache::lonnet::appenv(
13557:23): {'course.'.$cdom.'_'.$cnum.'.internal.supplementalchange' => $now});
13558:23): &do_cache_new('suppchange',$cdom.'_'.$cnum,$now,600);
13559:23): }
13560:23):
1.31 www 13561: # ------------------------------------------------- Update symbolic store links
13562:
13563: sub symblist {
13564: my ($mapname,%newhash)=@_;
1.438 www 13565: $mapname=&deversion(&declutter($mapname));
1.31 www 13566: my %hash;
1.620 albertel 13567: if (($env{'request.course.fn'}) && (%newhash)) {
13568: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256 albertel 13569: &GDBM_WRCREAT(),0640)) {
1.1000 raeburn 13570: foreach my $url (keys(%newhash)) {
1.711 albertel 13571: next if ($url eq 'last_known'
13572: && $env{'form.no_update_last_known'});
13573: $hash{declutter($url)}=&encode_symb($mapname,
13574: $newhash{$url}->[1],
13575: $newhash{$url}->[0]);
1.191 harris41 13576: }
1.31 www 13577: if (untie(%hash)) {
13578: return 'ok';
13579: }
13580: }
13581: }
13582: return 'error';
1.212 www 13583: }
13584:
13585: # --------------------------------------------------------------- Verify a symb
13586:
13587: sub symbverify {
1.1172.2.11 raeburn 13588: my ($symb,$thisurl,$encstate)=@_;
1.510 www 13589: my $thisfn=$thisurl;
1.439 www 13590: $thisfn=&declutter($thisfn);
1.215 www 13591: # direct jump to resource in page or to a sequence - will construct own symbs
13592: if ($thisfn=~/\.(page|sequence)$/) { return 1; }
13593: # check URL part
1.409 www 13594: my ($map,$resid,$url)=&decode_symb($symb);
1.439 www 13595:
1.431 www 13596: unless ($url eq $thisfn) { return 0; }
1.213 www 13597:
1.216 www 13598: $symb=&symbclean($symb);
1.510 www 13599: $thisurl=&deversion($thisurl);
1.439 www 13600: $thisfn=&deversion($thisfn);
1.213 www 13601:
13602: my %bighash;
13603: my $okay=0;
1.431 www 13604:
1.620 albertel 13605: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 13606: &GDBM_READER(),0640)) {
1.1032 raeburn 13607: if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
13608: $thisurl =~ s/\?.+$//;
1.1172.2.13 raeburn 13609: if ($map =~ m{^uploaded/.+\.page$}) {
13610: $thisurl =~ s{^(/adm/wrapper|)/ext/}{http://};
13611: $thisurl =~ s{^\Qhttp://https://\E}{https://};
13612: }
13613: }
13614: my $ids;
1.1172.2.122 raeburn 13615: if ($map =~ m{^uploaded/.+\.page$}) {
13616: $ids=$bighash{'ids_'.&clutter_with_no_wrapper($thisurl)};
1.1172.2.13 raeburn 13617: } else {
13618: $ids=$bighash{'ids_'.&clutter($thisurl)};
1.1032 raeburn 13619: }
1.1102 raeburn 13620: unless ($ids) {
1.1172.2.13 raeburn 13621: my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;
1.1102 raeburn 13622: $ids=$bighash{$idkey};
1.216 www 13623: }
13624: if ($ids) {
13625: # ------------------------------------------------------------------- Has ID(s)
1.1172.2.13 raeburn 13626: if ($thisfn =~ m{^/adm/wrapper/ext/}) {
13627: $symb =~ s/\?.+$//;
13628: }
1.800 albertel 13629: foreach my $id (split(/\,/,$ids)) {
13630: my ($mapid,$resid)=split(/\./,$id);
1.216 www 13631: if (
13632: &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
1.1172.2.13 raeburn 13633: eq $symb) {
1.1172.2.11 raeburn 13634: if (ref($encstate)) {
13635: $$encstate = $bighash{'encrypted_'.$id};
13636: }
1.1172.2.13 raeburn 13637: if (($env{'request.role.adv'}) ||
13638: ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
1.1101 raeburn 13639: ($thisurl eq '/adm/navmaps')) {
1.1172.2.13 raeburn 13640: $okay=1;
13641: last;
13642: }
13643: }
13644: }
1.216 www 13645: }
1.213 www 13646: untie(%bighash);
13647: }
13648: return $okay;
1.31 www 13649: }
13650:
1.210 www 13651: # --------------------------------------------------------------- Clean-up symb
13652:
13653: sub symbclean {
13654: my $symb=shift;
1.568 albertel 13655: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210 www 13656: # remove version from map
13657: $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215 www 13658:
1.210 www 13659: # remove version from URL
13660: $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213 www 13661:
1.507 www 13662: # remove wrapper
13663:
1.510 www 13664: $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694 albertel 13665: $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210 www 13666: return $symb;
1.409 www 13667: }
13668:
13669: # ---------------------------------------------- Split symb to find map and url
1.429 albertel 13670:
13671: sub encode_symb {
13672: my ($map,$resid,$url)=@_;
13673: return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
13674: }
1.409 www 13675:
13676: sub decode_symb {
1.568 albertel 13677: my $symb=shift;
13678: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
13679: my ($map,$resid,$url)=split(/___/,$symb);
1.413 www 13680: return (&fixversion($map),$resid,&fixversion($url));
13681: }
13682:
13683: sub fixversion {
13684: my $fn=shift;
1.609 banghart 13685: if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435 www 13686: my %bighash;
13687: my $uri=&clutter($fn);
1.620 albertel 13688: my $key=$env{'request.course.id'}.'_'.$uri;
1.440 www 13689: # is this cached?
1.599 albertel 13690: my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440 www 13691: if (defined($cached)) { return $result; }
13692: # unfortunately not cached, or expired
1.620 albertel 13693: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440 www 13694: &GDBM_READER(),0640)) {
13695: if ($bighash{'version_'.$uri}) {
13696: my $version=$bighash{'version_'.$uri};
1.444 www 13697: unless (($version eq 'mostrecent') ||
13698: ($version==&getversion($uri))) {
1.440 www 13699: $uri=~s/\.(\w+)$/\.$version\.$1/;
13700: }
13701: }
13702: untie %bighash;
1.413 www 13703: }
1.599 albertel 13704: return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438 www 13705: }
13706:
13707: sub deversion {
13708: my $url=shift;
13709: $url=~s/\.\d+\.(\w+)$/\.$1/;
13710: return $url;
1.210 www 13711: }
13712:
1.31 www 13713: # ------------------------------------------------------ Return symb list entry
13714:
13715: sub symbread {
1.1172.2.126 raeburn 13716: my ($thisfn,$donotrecurse,$ignorecachednull,$checkforblock,$possibles,
1.1172.2.128 raeburn 13717: $ignoresymbdb,$noenccheck)=@_;
1.1172.2.54 raeburn 13718: my $cache_str='request.symbread.cached.'.$thisfn;
1.1172.2.128 raeburn 13719: if (defined($env{$cache_str})) {
1.1172.2.126 raeburn 13720: unless (ref($possibles) eq 'HASH') {
13721: if ($ignorecachednull) {
13722: return $env{$cache_str} unless ($env{$cache_str} eq '');
13723: } else {
13724: return $env{$cache_str};
13725: }
1.1172.2.66 raeburn 13726: }
13727: }
1.242 www 13728: # no filename provided? try from environment
1.1172.2.54 raeburn 13729: unless ($thisfn) {
1.620 albertel 13730: if ($env{'request.symb'}) {
1.1172.2.128 raeburn 13731: return $env{$cache_str}=&symbclean($env{'request.symb'});
1.1172.2.13 raeburn 13732: }
13733: $thisfn=$env{'request.filename'};
1.44 www 13734: }
1.569 albertel 13735: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242 www 13736: # is that filename actually a symb? Verify, clean, and return
13737: if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539 albertel 13738: if (&symbverify($thisfn,$1)) {
1.1172.2.128 raeburn 13739: return $env{$cache_str}=&symbclean($thisfn);
1.539 albertel 13740: }
1.242 www 13741: }
1.44 www 13742: $thisfn=declutter($thisfn);
1.31 www 13743: my %hash;
1.37 www 13744: my %bighash;
13745: my $syval='';
1.620 albertel 13746: if (($env{'request.course.fn'}) && ($thisfn)) {
1.1172.2.128 raeburn 13747: unless ($ignoresymbdb) {
1.1172.2.126 raeburn 13748: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
13749: &GDBM_READER(),0640)) {
1.1172.2.146. .7(raebu 13750:22): $syval=$hash{$thisfn};
1.1172.2.126 raeburn 13751: untie(%hash);
13752: }
1.1172.2.128 raeburn 13753: if ($syval && $checkforblock) {
13754: my @blockers = &has_comm_blocking('bre',$syval,$thisfn,$ignoresymbdb,$noenccheck);
1.1172.2.126 raeburn 13755: if (@blockers) {
13756: $syval='';
13757: }
13758: }
1.37 www 13759: }
13760: # ---------------------------------------------------------- There was an entry
13761: if ($syval) {
1.601 albertel 13762: #unless ($syval=~/\_\d+$/) {
1.620 albertel 13763: #unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.949 raeburn 13764: #&appenv({'request.ambiguous' => $thisfn});
1.620 albertel 13765: #return $env{$cache_str}='';
1.601 albertel 13766: #}
13767: #$syval.=$1;
13768: #}
1.37 www 13769: } else {
13770: # ------------------------------------------------------- Was not in symb table
1.620 albertel 13771: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 13772: &GDBM_READER(),0640)) {
1.37 www 13773: # ---------------------------------------------- Get ID(s) for current resource
1.280 www 13774: my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65 www 13775: unless ($ids) {
13776: $ids=$bighash{'ids_/'.$thisfn};
1.242 www 13777: }
13778: unless ($ids) {
13779: # alias?
13780: $ids=$bighash{'mapalias_'.$thisfn};
1.65 www 13781: }
1.37 www 13782: if ($ids) {
13783: # ------------------------------------------------------------------- Has ID(s)
13784: my @possibilities=split(/\,/,$ids);
1.39 www 13785: if ($#possibilities==0) {
13786: # ----------------------------------------------- There is only one possibility
1.37 www 13787: my ($mapid,$resid)=split(/\./,$ids);
1.626 albertel 13788: $syval=&encode_symb($bighash{'map_id_'.$mapid},
13789: $resid,$thisfn);
1.1172.2.66 raeburn 13790: if (ref($possibles) eq 'HASH') {
1.1172.2.126 raeburn 13791: unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
13792: $possibles->{$syval} = 1;
13793: }
1.1172.2.66 raeburn 13794: }
13795: if ($checkforblock) {
1.1172.2.126 raeburn 13796: unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
1.1172.2.127 raeburn 13797: my @blockers = &has_comm_blocking('bre',$syval,$bighash{'src_'.$ids},'',$noenccheck);
1.1172.2.126 raeburn 13798: if (@blockers) {
13799: $syval = '';
13800: untie(%bighash);
13801: return $env{$cache_str}='';
13802: }
1.1172.2.66 raeburn 13803: }
13804: }
13805: } elsif ((!$donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
1.39 www 13806: # ------------------------------------------ There is more than one possibility
13807: my $realpossible=0;
1.800 albertel 13808: foreach my $id (@possibilities) {
13809: my $file=$bighash{'src_'.$id};
1.1172.2.66 raeburn 13810: my $canaccess;
13811: if (($donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
13812: $canaccess = 1;
13813: } else {
13814: $canaccess = &allowed('bre',$file);
13815: }
13816: if ($canaccess) {
13817: my ($mapid,$resid)=split(/\./,$id);
13818: if ($bighash{'map_type_'.$mapid} ne 'page') {
13819: my $poss_syval=&encode_symb($bighash{'map_id_'.$mapid},
13820: $resid,$thisfn);
1.1172.2.126 raeburn 13821: next if ($bighash{'randomout_'.$id} && !$env{'request.role.adv'});
1.1172.2.127 raeburn 13822: next unless (($noenccheck) || ($bighash{'encrypted_'.$id} eq $env{'request.enc'}));
1.1172.2.66 raeburn 13823: if ($checkforblock) {
1.1172.2.127 raeburn 13824: my @blockers = &has_comm_blocking('bre',$poss_syval,$file,'',$noenccheck);
1.1172.2.126 raeburn 13825: if (@blockers > 0) {
13826: $syval = '';
13827: } else {
1.1172.2.66 raeburn 13828: $syval = $poss_syval;
13829: $realpossible++;
13830: }
13831: } else {
13832: $syval = $poss_syval;
13833: $realpossible++;
13834: }
1.1172.2.126 raeburn 13835: if ($syval) {
13836: if (ref($possibles) eq 'HASH') {
13837: $possibles->{$syval} = 1;
13838: }
13839: }
1.1172.2.66 raeburn 13840: }
1.39 www 13841: }
1.191 harris41 13842: }
1.39 www 13843: if ($realpossible!=1) { $syval=''; }
1.249 www 13844: } else {
13845: $syval='';
1.37 www 13846: }
13847: }
1.1172.2.66 raeburn 13848: untie(%bighash);
1.481 raeburn 13849: }
1.31 www 13850: }
1.62 www 13851: if ($syval) {
1.1172.2.128 raeburn 13852: return $env{$cache_str}=$syval;
1.62 www 13853: }
1.31 www 13854: }
1.949 raeburn 13855: &appenv({'request.ambiguous' => $thisfn});
1.620 albertel 13856: return $env{$cache_str}='';
1.31 www 13857: }
13858:
13859: # ---------------------------------------------------------- Return random seed
13860:
1.32 www 13861: sub numval {
13862: my $txt=shift;
13863: $txt=~tr/A-J/0-9/;
13864: $txt=~tr/a-j/0-9/;
13865: $txt=~tr/K-T/0-9/;
13866: $txt=~tr/k-t/0-9/;
13867: $txt=~tr/U-Z/0-5/;
13868: $txt=~tr/u-z/0-5/;
13869: $txt=~s/\D//g;
1.564 albertel 13870: if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32 www 13871: return int($txt);
1.368 albertel 13872: }
13873:
1.484 albertel 13874: sub numval2 {
13875: my $txt=shift;
13876: $txt=~tr/A-J/0-9/;
13877: $txt=~tr/a-j/0-9/;
13878: $txt=~tr/K-T/0-9/;
13879: $txt=~tr/k-t/0-9/;
13880: $txt=~tr/U-Z/0-5/;
13881: $txt=~tr/u-z/0-5/;
13882: $txt=~s/\D//g;
13883: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
13884: my $total;
13885: foreach my $val (@txts) { $total+=$val; }
1.564 albertel 13886: if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484 albertel 13887: return int($total);
13888: }
13889:
1.575 albertel 13890: sub numval3 {
13891: use integer;
13892: my $txt=shift;
13893: $txt=~tr/A-J/0-9/;
13894: $txt=~tr/a-j/0-9/;
13895: $txt=~tr/K-T/0-9/;
13896: $txt=~tr/k-t/0-9/;
13897: $txt=~tr/U-Z/0-5/;
13898: $txt=~tr/u-z/0-5/;
13899: $txt=~s/\D//g;
13900: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
13901: my $total;
13902: foreach my $val (@txts) { $total+=$val; }
13903: if ($_64bit) { $total=(($total<<32)>>32); }
13904: return $total;
13905: }
13906:
1.675 albertel 13907: sub digest {
13908: my ($data)=@_;
13909: my $digest=&Digest::MD5::md5($data);
13910: my ($a,$b,$c,$d)=unpack("iiii",$digest);
13911: my ($e,$f);
13912: {
13913: use integer;
13914: $e=($a+$b);
13915: $f=($c+$d);
13916: if ($_64bit) {
13917: $e=(($e<<32)>>32);
13918: $f=(($f<<32)>>32);
13919: }
13920: }
13921: if (wantarray) {
13922: return ($e,$f);
13923: } else {
13924: my $g;
13925: {
13926: use integer;
13927: $g=($e+$f);
13928: if ($_64bit) {
13929: $g=(($g<<32)>>32);
13930: }
13931: }
13932: return $g;
13933: }
13934: }
13935:
1.368 albertel 13936: sub latest_rnd_algorithm_id {
1.675 albertel 13937: return '64bit5';
1.366 albertel 13938: }
1.32 www 13939:
1.503 albertel 13940: sub get_rand_alg {
13941: my ($courseid)=@_;
1.790 albertel 13942: if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503 albertel 13943: if ($courseid) {
1.620 albertel 13944: return $env{"course.$courseid.rndseed"};
1.503 albertel 13945: }
13946: return &latest_rnd_algorithm_id();
13947: }
13948:
1.562 albertel 13949: sub validCODE {
13950: my ($CODE)=@_;
13951: if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
13952: return 0;
13953: }
13954:
1.491 albertel 13955: sub getCODE {
1.620 albertel 13956: if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618 albertel 13957: if ( (defined($Apache::lonhomework::parsing_a_problem) ||
13958: defined($Apache::lonhomework::parsing_a_task) ) &&
13959: &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491 albertel 13960: return $Apache::lonhomework::history{'resource.CODE'};
13961: }
13962: return undef;
13963: }
1.1133 foxr 13964: #
13965: # Determines the random seed for a specific context:
13966: #
13967: # parameters:
13968: # symb - in course context the symb for the seed.
13969: # course_id - The course id of the form domain_coursenum.
13970: # domain - Domain for the user.
13971: # course - Course for the user.
13972: # cenv - environment of the course.
13973: #
13974: # NOTE:
13975: # All parameters are picked out of the environment if missing
13976: # or not defined.
13977: # If a symb cannot be determined the current time is used instead.
13978: #
13979: # For a given well defined symb, courside, domain, username,
13980: # and course environment, the seed is reproducible.
13981: #
1.31 www 13982: sub rndseed {
1.1133 foxr 13983: my ($symb,$courseid,$domain,$username, $cenv)=@_;
1.790 albertel 13984: my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.896 albertel 13985: if (!defined($symb)) {
1.366 albertel 13986: unless ($symb=$wsymb) { return time; }
13987: }
1.1146 foxr 13988: if (!defined $courseid) {
13989: $courseid=$wcourseid;
13990: }
13991: if (!defined $domain) { $domain=$wdomain; }
13992: if (!defined $username) { $username=$wusername }
1.1133 foxr 13993:
13994: my $which;
13995: if (defined($cenv->{'rndseed'})) {
13996: $which = $cenv->{'rndseed'};
13997: } else {
13998: $which =&get_rand_alg($courseid);
13999: }
1.491 albertel 14000: if (defined(&getCODE())) {
1.675 albertel 14001: if ($which eq '64bit5') {
14002: return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
14003: } elsif ($which eq '64bit4') {
1.575 albertel 14004: return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
14005: } else {
14006: return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
14007: }
1.675 albertel 14008: } elsif ($which eq '64bit5') {
14009: return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575 albertel 14010: } elsif ($which eq '64bit4') {
14011: return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501 albertel 14012: } elsif ($which eq '64bit3') {
14013: return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443 albertel 14014: } elsif ($which eq '64bit2') {
14015: return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366 albertel 14016: } elsif ($which eq '64bit') {
14017: return &rndseed_64bit($symb,$courseid,$domain,$username);
14018: }
14019: return &rndseed_32bit($symb,$courseid,$domain,$username);
14020: }
14021:
14022: sub rndseed_32bit {
14023: my ($symb,$courseid,$domain,$username)=@_;
14024: {
14025: use integer;
14026: my $symbchck=unpack("%32C*",$symb) << 27;
14027: my $symbseed=numval($symb) << 22;
14028: my $namechck=unpack("%32C*",$username) << 17;
14029: my $nameseed=numval($username) << 12;
14030: my $domainseed=unpack("%32C*",$domain) << 7;
14031: my $courseseed=unpack("%32C*",$courseid);
14032: my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790 albertel 14033: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14034: #&logthis("rndseed :$num:$symb");
1.564 albertel 14035: if ($_64bit) { $num=(($num<<32)>>32); }
1.366 albertel 14036: return $num;
14037: }
14038: }
14039:
14040: sub rndseed_64bit {
14041: my ($symb,$courseid,$domain,$username)=@_;
14042: {
14043: use integer;
14044: my $symbchck=unpack("%32S*",$symb) << 21;
14045: my $symbseed=numval($symb) << 10;
14046: my $namechck=unpack("%32S*",$username);
14047:
14048: my $nameseed=numval($username) << 21;
14049: my $domainseed=unpack("%32S*",$domain) << 10;
14050: my $courseseed=unpack("%32S*",$courseid);
14051:
14052: my $num1=$symbchck+$symbseed+$namechck;
14053: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 14054: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14055: #&logthis("rndseed :$num:$symb");
1.564 albertel 14056: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366 albertel 14057: return "$num1,$num2";
1.155 albertel 14058: }
1.366 albertel 14059: }
14060:
1.443 albertel 14061: sub rndseed_64bit2 {
14062: my ($symb,$courseid,$domain,$username)=@_;
14063: {
14064: use integer;
14065: # strings need to be an even # of cahracters long, it it is odd the
14066: # last characters gets thrown away
14067: my $symbchck=unpack("%32S*",$symb.' ') << 21;
14068: my $symbseed=numval($symb) << 10;
14069: my $namechck=unpack("%32S*",$username.' ');
14070:
14071: my $nameseed=numval($username) << 21;
1.501 albertel 14072: my $domainseed=unpack("%32S*",$domain.' ') << 10;
14073: my $courseseed=unpack("%32S*",$courseid.' ');
14074:
14075: my $num1=$symbchck+$symbseed+$namechck;
14076: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 14077: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14078: #&logthis("rndseed :$num:$symb");
1.803 albertel 14079: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501 albertel 14080: return "$num1,$num2";
14081: }
14082: }
14083:
14084: sub rndseed_64bit3 {
14085: my ($symb,$courseid,$domain,$username)=@_;
14086: {
14087: use integer;
14088: # strings need to be an even # of cahracters long, it it is odd the
14089: # last characters gets thrown away
14090: my $symbchck=unpack("%32S*",$symb.' ') << 21;
14091: my $symbseed=numval2($symb) << 10;
14092: my $namechck=unpack("%32S*",$username.' ');
14093:
14094: my $nameseed=numval2($username) << 21;
1.443 albertel 14095: my $domainseed=unpack("%32S*",$domain.' ') << 10;
14096: my $courseseed=unpack("%32S*",$courseid.' ');
14097:
14098: my $num1=$symbchck+$symbseed+$namechck;
14099: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 14100: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14101: #&logthis("rndseed :$num1:$num2:$_64bit");
1.564 albertel 14102: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110 www 14103:
1.503 albertel 14104: return "$num1:$num2";
1.443 albertel 14105: }
14106: }
14107:
1.575 albertel 14108: sub rndseed_64bit4 {
14109: my ($symb,$courseid,$domain,$username)=@_;
14110: {
14111: use integer;
14112: # strings need to be an even # of cahracters long, it it is odd the
14113: # last characters gets thrown away
14114: my $symbchck=unpack("%32S*",$symb.' ') << 21;
14115: my $symbseed=numval3($symb) << 10;
14116: my $namechck=unpack("%32S*",$username.' ');
14117:
14118: my $nameseed=numval3($username) << 21;
14119: my $domainseed=unpack("%32S*",$domain.' ') << 10;
14120: my $courseseed=unpack("%32S*",$courseid.' ');
14121:
14122: my $num1=$symbchck+$symbseed+$namechck;
14123: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 14124: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14125: #&logthis("rndseed :$num1:$num2:$_64bit");
1.575 albertel 14126: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110 www 14127:
1.575 albertel 14128: return "$num1:$num2";
14129: }
14130: }
14131:
1.675 albertel 14132: sub rndseed_64bit5 {
14133: my ($symb,$courseid,$domain,$username)=@_;
14134: my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
14135: return "$num1:$num2";
14136: }
14137:
1.366 albertel 14138: sub rndseed_CODE_64bit {
14139: my ($symb,$courseid,$domain,$username)=@_;
1.155 albertel 14140: {
1.366 albertel 14141: use integer;
1.443 albertel 14142: my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484 albertel 14143: my $symbseed=numval2($symb);
1.491 albertel 14144: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
14145: my $CODEseed=numval(&getCODE());
1.443 albertel 14146: my $courseseed=unpack("%32S*",$courseid.' ');
1.484 albertel 14147: my $num1=$symbseed+$CODEchck;
14148: my $num2=$CODEseed+$courseseed+$symbchck;
1.790 albertel 14149: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
14150: #&logthis("rndseed :$num1:$num2:$symb");
1.564 albertel 14151: if ($_64bit) { $num1=(($num1<<32)>>32); }
14152: if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503 albertel 14153: return "$num1:$num2";
1.366 albertel 14154: }
14155: }
14156:
1.575 albertel 14157: sub rndseed_CODE_64bit4 {
14158: my ($symb,$courseid,$domain,$username)=@_;
14159: {
14160: use integer;
14161: my $symbchck=unpack("%32S*",$symb.' ') << 16;
14162: my $symbseed=numval3($symb);
14163: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
14164: my $CODEseed=numval3(&getCODE());
14165: my $courseseed=unpack("%32S*",$courseid.' ');
14166: my $num1=$symbseed+$CODEchck;
14167: my $num2=$CODEseed+$courseseed+$symbchck;
1.790 albertel 14168: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
14169: #&logthis("rndseed :$num1:$num2:$symb");
1.575 albertel 14170: if ($_64bit) { $num1=(($num1<<32)>>32); }
14171: if ($_64bit) { $num2=(($num2<<32)>>32); }
14172: return "$num1:$num2";
14173: }
14174: }
14175:
1.675 albertel 14176: sub rndseed_CODE_64bit5 {
14177: my ($symb,$courseid,$domain,$username)=@_;
14178: my $code = &getCODE();
14179: my ($num1,$num2)=&digest("$symb,$courseid,$code");
14180: return "$num1:$num2";
14181: }
14182:
1.366 albertel 14183: sub setup_random_from_rndseed {
14184: my ($rndseed)=@_;
1.503 albertel 14185: if ($rndseed =~/([,:])/) {
1.1172.2.51 raeburn 14186: my ($num1,$num2) = map { abs($_); } (split(/[,:]/,$rndseed));
14187: if ((!$num1) || (!$num2) || ($num1 > 2147483562) || ($num2 > 2147483398)) {
14188: &Math::Random::random_set_seed_from_phrase($rndseed);
14189: } else {
14190: &Math::Random::random_set_seed($num1,$num2);
14191: }
1.366 albertel 14192: } else {
14193: &Math::Random::random_set_seed_from_phrase($rndseed);
1.98 albertel 14194: }
1.36 albertel 14195: }
14196:
1.474 albertel 14197: sub latest_receipt_algorithm_id {
1.835 albertel 14198: return 'receipt3';
1.474 albertel 14199: }
14200:
1.480 www 14201: sub recunique {
14202: my $fucourseid=shift;
14203: my $unique;
1.835 albertel 14204: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
14205: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620 albertel 14206: $unique=$env{"course.$fucourseid.internal.encseed"};
1.480 www 14207: } else {
14208: $unique=$perlvar{'lonReceipt'};
14209: }
14210: return unpack("%32C*",$unique);
14211: }
14212:
14213: sub recprefix {
14214: my $fucourseid=shift;
14215: my $prefix;
1.835 albertel 14216: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
14217: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620 albertel 14218: $prefix=$env{"course.$fucourseid.internal.encpref"};
1.480 www 14219: } else {
14220: $prefix=$perlvar{'lonHostID'};
14221: }
14222: return unpack("%32C*",$prefix);
14223: }
14224:
1.76 www 14225: sub ireceipt {
1.474 albertel 14226: my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835 albertel 14227:
14228: my $return =&recprefix($fucourseid).'-';
14229:
14230: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
14231: $env{'request.state'} eq 'construct') {
14232: $return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
14233: return $return;
14234: }
14235:
1.76 www 14236: my $cuname=unpack("%32C*",$funame);
14237: my $cudom=unpack("%32C*",$fudom);
14238: my $cucourseid=unpack("%32C*",$fucourseid);
14239: my $cusymb=unpack("%32C*",$fusymb);
1.480 www 14240: my $cunique=&recunique($fucourseid);
1.474 albertel 14241: my $cpart=unpack("%32S*",$part);
1.835 albertel 14242: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
14243:
1.790 albertel 14244: #&logthis("doing receipt2 using parts $cpart, uname $cuname and udom $cudom gets ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474 albertel 14245:
14246: $return.= ($cunique%$cuname+
14247: $cunique%$cudom+
14248: $cusymb%$cuname+
14249: $cusymb%$cudom+
14250: $cucourseid%$cuname+
14251: $cucourseid%$cudom+
14252: $cpart%$cuname+
14253: $cpart%$cudom);
14254: } else {
14255: $return.= ($cunique%$cuname+
14256: $cunique%$cudom+
14257: $cusymb%$cuname+
14258: $cusymb%$cudom+
14259: $cucourseid%$cuname+
14260: $cucourseid%$cudom);
14261: }
14262: return $return;
1.76 www 14263: }
14264:
14265: sub receipt {
1.474 albertel 14266: my ($part)=@_;
1.790 albertel 14267: my ($symb,$courseid,$domain,$name) = &whichuser();
1.474 albertel 14268: return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76 www 14269: }
1.260 ng 14270:
1.790 albertel 14271: sub whichuser {
14272: my ($passedsymb)=@_;
14273: my ($symb,$courseid,$domain,$name,$publicuser);
14274: if (defined($env{'form.grade_symb'})) {
14275: my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
14276: my $allowed=&allowed('vgr',$tmp_courseid);
14277: if (!$allowed &&
14278: exists($env{'request.course.sec'}) &&
14279: $env{'request.course.sec'} !~ /^\s*$/) {
14280: $allowed=&allowed('vgr',$tmp_courseid.
14281: '/'.$env{'request.course.sec'});
14282: }
14283: if ($allowed) {
14284: ($symb)=&get_env_multiple('form.grade_symb');
14285: $courseid=$tmp_courseid;
14286: ($domain)=&get_env_multiple('form.grade_domain');
14287: ($name)=&get_env_multiple('form.grade_username');
14288: return ($symb,$courseid,$domain,$name,$publicuser);
14289: }
14290: }
14291: if (!$passedsymb) {
14292: $symb=&symbread();
14293: } else {
14294: $symb=$passedsymb;
14295: }
14296: $courseid=$env{'request.course.id'};
14297: $domain=$env{'user.domain'};
14298: $name=$env{'user.name'};
14299: if ($name eq 'public' && $domain eq 'public') {
14300: if (!defined($env{'form.username'})) {
14301: $env{'form.username'}.=time.rand(10000000);
14302: }
14303: $name.=$env{'form.username'};
14304: }
14305: return ($symb,$courseid,$domain,$name,$publicuser);
14306:
14307: }
14308:
1.36 albertel 14309: # ------------------------------------------------------------ Serves up a file
1.472 albertel 14310: # returns either the contents of the file or
14311: # -1 if the file doesn't exist
1.481 raeburn 14312: #
14313: # if the target is a file that was uploaded via DOCS,
14314: # a check will be made to see if a current copy exists on the local server,
14315: # if it does this will be served, otherwise a copy will be retrieved from
14316: # the home server for the course and stored in /home/httpd/html/userfiles on
14317: # the local server.
1.472 albertel 14318:
1.36 albertel 14319: sub getfile {
1.538 albertel 14320: my ($file) = @_;
1.609 banghart 14321: if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538 albertel 14322: &repcopy($file);
14323: return &readfile($file);
14324: }
14325:
14326: sub repcopy_userfile {
14327: my ($file)=@_;
1.1142 raeburn 14328: my $londocroot = $perlvar{'lonDocRoot'};
14329: if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
1.1164 raeburn 14330: if ($file =~ m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
1.538 albertel 14331: my ($cdom,$cnum,$filename) =
1.811 albertel 14332: ($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538 albertel 14333: my $uri="/uploaded/$cdom/$cnum/$filename";
14334: if (-e "$file") {
1.828 www 14335: # we already have a local copy, check it out
1.538 albertel 14336: my @fileinfo = stat($file);
1.828 www 14337: my $rtncode;
14338: my $info;
1.538 albertel 14339: my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482 albertel 14340: if ($lwpresp ne 'ok') {
1.828 www 14341: # there is no such file anymore, even though we had a local copy
1.482 albertel 14342: if ($rtncode eq '404') {
1.538 albertel 14343: unlink($file);
1.482 albertel 14344: }
14345: return -1;
14346: }
14347: if ($info < $fileinfo[9]) {
1.828 www 14348: # nice, the file we have is up-to-date, just say okay
1.607 raeburn 14349: return 'ok';
1.828 www 14350: } else {
14351: # the file is outdated, get rid of it
14352: unlink($file);
1.482 albertel 14353: }
1.828 www 14354: }
14355: # one way or the other, at this point, we don't have the file
14356: # construct the correct path for the file
14357: my @parts = ($cdom,$cnum);
14358: if ($filename =~ m|^(.+)/[^/]+$|) {
14359: push @parts, split(/\//,$1);
14360: }
14361: my $path = $perlvar{'lonDocRoot'}.'/userfiles';
14362: foreach my $part (@parts) {
14363: $path .= '/'.$part;
14364: if (!-e $path) {
14365: mkdir($path,0770);
1.482 albertel 14366: }
14367: }
1.828 www 14368: # now the path exists for sure
14369: # get a user agent
14370: my $ua=new LWP::UserAgent;
14371: my $transferfile=$file.'.in.transfer';
14372: # FIXME: this should flock
14373: if (-e $transferfile) { return 'ok'; }
14374: my $request;
14375: $uri=~s/^\///;
1.980 raeburn 14376: my $homeserver = &homeserver($cnum,$cdom);
1.1172.2.120 raeburn 14377: my $hostname = &hostname($homeserver);
1.980 raeburn 14378: my $protocol = $protocol{$homeserver};
14379: $protocol = 'http' if ($protocol ne 'https');
1.1172.2.120 raeburn 14380: $request=new HTTP::Request('GET',$protocol.'://'.$hostname.'/raw/'.$uri);
1.828 www 14381: my $response=$ua->request($request,$transferfile);
14382: # did it work?
14383: if ($response->is_error()) {
14384: unlink($transferfile);
14385: &logthis("Userfile repcopy failed for $uri");
14386: return -1;
14387: }
14388: # worked, rename the transfer file
14389: rename($transferfile,$file);
1.607 raeburn 14390: return 'ok';
1.481 raeburn 14391: }
14392:
1.517 albertel 14393: sub tokenwrapper {
14394: my $uri=shift;
1.980 raeburn 14395: $uri=~s|^https?\://([^/]+)||;
1.552 albertel 14396: $uri=~s|^/||;
1.620 albertel 14397: $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517 albertel 14398: my $token=$1;
1.552 albertel 14399: my (undef,$udom,$uname,$file)=split('/',$uri,4);
14400: if ($udom && $uname && $file) {
14401: $file=~s|(\?\.*)*$||;
1.949 raeburn 14402: &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
1.980 raeburn 14403: my $homeserver = &homeserver($uname,$udom);
1.1172.2.120 raeburn 14404: my $hostname = &hostname($homeserver);
1.980 raeburn 14405: my $protocol = $protocol{$homeserver};
14406: $protocol = 'http' if ($protocol ne 'https');
1.1172.2.120 raeburn 14407: return $protocol.'://'.$hostname.'/'.$uri.
1.517 albertel 14408: (($uri=~/\?/)?'&':'?').'token='.$token.
14409: '&tokenissued='.$perlvar{'lonHostID'};
14410: } else {
14411: return '/adm/notfound.html';
14412: }
14413: }
14414:
1.828 www 14415: # call with reqtype HEAD: get last modification time
14416: # call with reqtype GET: get the file contents
14417: # Do not call this with reqtype GET for large files! It loads everything into memory
14418: #
1.481 raeburn 14419: sub getuploaded {
14420: my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
14421: $uri=~s/^\///;
1.980 raeburn 14422: my $homeserver = &homeserver($cnum,$cdom);
1.1172.2.120 raeburn 14423: my $hostname = &hostname($homeserver);
1.980 raeburn 14424: my $protocol = $protocol{$homeserver};
14425: $protocol = 'http' if ($protocol ne 'https');
1.1172.2.120 raeburn 14426: $uri = $protocol.'://'.$hostname.'/raw/'.$uri;
1.481 raeburn 14427: my $ua=new LWP::UserAgent;
14428: my $request=new HTTP::Request($reqtype,$uri);
14429: my $response=$ua->request($request);
14430: $$rtncode = $response->code;
1.482 albertel 14431: if (! $response->is_success()) {
14432: return 'failed';
14433: }
14434: if ($reqtype eq 'HEAD') {
1.486 www 14435: $$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482 albertel 14436: } elsif ($reqtype eq 'GET') {
14437: $$info = $response->content;
1.472 albertel 14438: }
1.482 albertel 14439: return 'ok';
1.36 albertel 14440: }
14441:
1.481 raeburn 14442: sub readfile {
14443: my $file = shift;
14444: if ( (! -e $file ) || ($file eq '') ) { return -1; };
14445: my $fh;
1.1172.2.96 raeburn 14446: open($fh,"<",$file);
1.481 raeburn 14447: my $a='';
1.800 albertel 14448: while (my $line = <$fh>) { $a .= $line; }
1.481 raeburn 14449: return $a;
14450: }
14451:
1.36 albertel 14452: sub filelocation {
1.590 banghart 14453: my ($dir,$file) = @_;
14454: my $location;
14455: $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700 albertel 14456:
14457: if ($file =~ m-^/adm/-) {
14458: $file=~s-^/adm/wrapper/-/-;
14459: $file=~s-^/adm/coursedocs/showdoc/-/-;
14460: }
1.882 albertel 14461:
1.1139 www 14462: if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
1.956 raeburn 14463: $location = $file;
1.609 banghart 14464: } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590 banghart 14465: my ($udom,$uname,$filename)=
1.811 albertel 14466: ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590 banghart 14467: my $home=&homeserver($uname,$udom);
14468: my $is_me=0;
14469: my @ids=¤t_machine_ids();
14470: foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
14471: if ($is_me) {
1.1117 foxr 14472: $location=propath($udom,$uname).'/userfiles/'.$filename;
1.590 banghart 14473: } else {
14474: $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
14475: $udom.'/'.$uname.'/'.$filename;
14476: }
1.882 albertel 14477: } elsif ($file =~ m-^/adm/-) {
14478: $location = $perlvar{'lonDocRoot'}.'/'.$file;
1.590 banghart 14479: } else {
14480: $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.1139 www 14481: $file=~s:^/(res|priv)/:/:;
14482: my $space=$1;
1.590 banghart 14483: if ( !( $file =~ m:^/:) ) {
14484: $location = $dir. '/'.$file;
14485: } else {
1.1142 raeburn 14486: $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
1.590 banghart 14487: }
1.59 albertel 14488: }
1.590 banghart 14489: $location=~s://+:/:g; # remove duplicate /
1.930 albertel 14490: while ($location=~m{/\.\./}) {
14491: if ($location =~ m{/[^/]+/\.\./}) {
14492: $location=~ s{/[^/]+/\.\./}{/}g;
14493: } else {
14494: $location=~ s{/\.\./}{/}g;
14495: }
14496: } #remove dir/..
1.590 banghart 14497: while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
14498: return $location;
1.46 www 14499: }
1.36 albertel 14500:
1.46 www 14501: sub hreflocation {
14502: my ($dir,$file)=@_;
1.980 raeburn 14503: unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
1.666 albertel 14504: $file=filelocation($dir,$file);
1.700 albertel 14505: } elsif ($file=~m-^/adm/-) {
14506: $file=~s-^/adm/wrapper/-/-;
14507: $file=~s-^/adm/coursedocs/showdoc/-/-;
1.666 albertel 14508: }
14509: if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
14510: $file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
14511: } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.1143 raeburn 14512: $file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
14513: {/uploaded/$1/$2/}x;
1.46 www 14514: }
1.913 albertel 14515: if ($file=~ m{^/userfiles/}) {
14516: $file =~ s{^/userfiles/}{/uploaded/};
14517: }
1.462 albertel 14518: return $file;
1.465 albertel 14519: }
14520:
1.1139 www 14521:
14522:
14523:
14524:
1.465 albertel 14525: sub current_machine_domains {
1.853 albertel 14526: return &machine_domains(&hostname($perlvar{'lonHostID'}));
14527: }
14528:
14529: sub machine_domains {
14530: my ($hostname) = @_;
1.465 albertel 14531: my @domains;
1.838 albertel 14532: my %hostname = &all_hostnames();
1.465 albertel 14533: while( my($id, $name) = each(%hostname)) {
1.467 matthew 14534: # &logthis("-$id-$name-$hostname-");
1.465 albertel 14535: if ($hostname eq $name) {
1.844 albertel 14536: push(@domains,&host_domain($id));
1.465 albertel 14537: }
14538: }
14539: return @domains;
14540: }
14541:
14542: sub current_machine_ids {
1.853 albertel 14543: return &machine_ids(&hostname($perlvar{'lonHostID'}));
14544: }
14545:
14546: sub machine_ids {
14547: my ($hostname) = @_;
14548: $hostname ||= &hostname($perlvar{'lonHostID'});
1.465 albertel 14549: my @ids;
1.888 albertel 14550: my %name_to_host = &all_names();
1.889 albertel 14551: if (ref($name_to_host{$hostname}) eq 'ARRAY') {
14552: return @{ $name_to_host{$hostname} };
14553: }
14554: return;
1.31 www 14555: }
14556:
1.824 raeburn 14557: sub additional_machine_domains {
14558: my @domains;
1.1172.2.142 raeburn 14559: if (-e "$perlvar{'lonTabDir'}/expected_domains.tab") {
14560: if (open(my $fh,"<","$perlvar{'lonTabDir'}/expected_domains.tab")) {
14561: while( my $line = <$fh>) {
14562: chomp($line);
14563: $line =~ s/\s//g;
14564: push(@domains,$line);
14565: }
14566: close($fh);
14567: }
1.824 raeburn 14568: }
14569: return @domains;
14570: }
14571:
14572: sub default_login_domain {
14573: my $domain = $perlvar{'lonDefDomain'};
14574: my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
14575: foreach my $posdom (¤t_machine_domains(),
14576: &additional_machine_domains()) {
14577: if (lc($posdom) eq lc($testdomain)) {
14578: $domain=$posdom;
14579: last;
14580: }
14581: }
14582: return $domain;
14583: }
14584:
1.1172.2.106 raeburn 14585: sub shared_institution {
1.1172.2.136 raeburn 14586: my ($dom,$lonhost) = @_;
14587: if ($lonhost eq '') {
14588: $lonhost = $perlvar{'lonHostID'};
14589: }
1.1172.2.106 raeburn 14590: my $same_intdom;
1.1172.2.136 raeburn 14591: my $hostintdom = &internet_dom($lonhost);
1.1172.2.106 raeburn 14592: if ($hostintdom ne '') {
14593: my %iphost = &get_iphost();
14594: my $primary_id = &domain($dom,'primary');
14595: my $primary_ip = &get_host_ip($primary_id);
14596: if (ref($iphost{$primary_ip}) eq 'ARRAY') {
14597: foreach my $id (@{$iphost{$primary_ip}}) {
14598: my $intdom = &internet_dom($id);
14599: if ($intdom eq $hostintdom) {
14600: $same_intdom = 1;
14601: last;
14602: }
14603: }
14604: }
14605: }
14606: return $same_intdom;
14607: }
14608:
1.1172.2.120 raeburn 14609: sub uses_sts {
14610: my ($ignore_cache) = @_;
14611: my $lonhost = $perlvar{'lonHostID'};
14612: my $hostname = &hostname($lonhost);
14613: my $sts_on;
14614: if ($protocol{$lonhost} eq 'https') {
14615: my $cachetime = 12*3600;
14616: if (!$ignore_cache) {
14617: ($sts_on,my $cached)=&is_cached_new('stspolicy',$lonhost);
14618: if (defined($cached)) {
14619: return $sts_on;
14620: }
14621: }
1.1172.2.121 raeburn 14622: my $ua=new LWP::UserAgent;
1.1172.2.120 raeburn 14623: my $url = $protocol{$lonhost}.'://'.$hostname.'/index.html';
14624: my $request=new HTTP::Request('HEAD',$url);
1.1172.2.121 raeburn 14625: my $response=$ua->request($request);
1.1172.2.120 raeburn 14626: if ($response->is_success) {
14627: my $has_sts = $response->header('Strict-Transport-Security');
14628: if ($has_sts eq '') {
14629: $sts_on = 0;
14630: } else {
14631: if ($has_sts =~ /\Qmax-age=\E(\d+)/) {
14632: my $maxage = $1;
14633: if ($maxage) {
14634: $sts_on = 1;
14635: } else {
14636: $sts_on = 0;
14637: }
14638: } else {
14639: $sts_on = 0;
14640: }
14641: }
14642: return &do_cache_new('stspolicy',$lonhost,$sts_on,$cachetime);
14643: }
14644: }
14645: return;
14646: }
14647:
1.1172.2.142 raeburn 14648: sub waf_allssl {
14649: my ($host_name) = @_;
14650: my $alias = &get_proxy_alias();
14651: if ($host_name eq '') {
14652: $host_name = $ENV{'SERVER_NAME'};
14653: }
14654: if (($host_name ne '') && ($alias eq $host_name)) {
14655: my $serverhomedom = &host_domain($perlvar{'lonHostID'});
14656: my %defdomdefaults = &get_domain_defaults($serverhomedom);
14657: if ($defdomdefaults{'waf_sslopt'}) {
14658: return $defdomdefaults{'waf_sslopt'};
14659: }
14660: }
14661: return;
14662: }
14663:
1.1172.2.134 raeburn 14664: sub get_requestor_ip {
14665: my ($r,$nolookup,$noproxy) = @_;
14666: my $from_ip;
14667: if (ref($r)) {
1.1172.2.142 raeburn 14668: if ($r->can('useragent_ip')) {
14669: if ($noproxy && $r->can('client_ip')) {
14670: $from_ip = $r->client_ip();
14671: } else {
14672: $from_ip = $r->useragent_ip();
14673: }
14674: } elsif ($r->connection->can('remote_ip')) {
14675: $from_ip = $r->connection->remote_ip();
14676: } else {
14677: $from_ip = $r->get_remote_host($nolookup);
14678: }
1.1172.2.134 raeburn 14679: } else {
14680: $from_ip = $ENV{'REMOTE_ADDR'};
14681: }
1.1172.2.142 raeburn 14682: return $from_ip if ($noproxy);
14683: # Who controls proxy settings for server
14684: my $dom_in_use = $Apache::lonnet::perlvar{'lonDefDomain'};
14685: my $proxyinfo = &get_proxy_settings($dom_in_use);
14686: if ((ref($proxyinfo) eq 'HASH') && ($from_ip)) {
14687: if ($proxyinfo->{'vpnint'}) {
14688: if (&ip_match($from_ip,$proxyinfo->{'vpnint'})) {
14689: return $from_ip;
14690: }
14691: }
14692: if ($proxyinfo->{'trusted'}) {
14693: if (&ip_match($from_ip,$proxyinfo->{'trusted'})) {
14694: my $ipheader = $proxyinfo->{'ipheader'};
14695: my ($ip,$xfor);
14696: if (ref($r)) {
14697: if ($ipheader) {
14698: $ip = $r->headers_in->{$ipheader};
14699: }
14700: $xfor = $r->headers_in->{'X-Forwarded-For'};
14701: } else {
14702: if ($ipheader) {
14703: $ip = $ENV{'HTTP_'.uc($ipheader)};
14704: }
14705: $xfor = $ENV{'HTTP_X_FORWARDED_FOR'};
14706: }
14707: if (($ip eq '') && ($xfor ne '')) {
14708: foreach my $poss_ip (reverse(split(/\s*,\s*/,$xfor))) {
14709: unless (&ip_match($poss_ip,$proxyinfo->{'trusted'})) {
14710: $ip = $poss_ip;
14711: last;
14712: }
14713: }
14714: }
14715: if ($ip ne '') {
14716: return $ip;
14717: }
14718: }
14719: }
14720: }
1.1172.2.134 raeburn 14721: return $from_ip;
14722: }
14723:
1.1172.2.142 raeburn 14724: sub get_proxy_settings {
14725: my ($dom_in_use) = @_;
14726: my %domdefaults = &Apache::lonnet::get_domain_defaults($dom_in_use);
14727: my $proxyinfo = {
14728: ipheader => $domdefaults{'waf_ipheader'},
14729: trusted => $domdefaults{'waf_trusted'},
14730: vpnint => $domdefaults{'waf_vpnint'},
14731: vpnext => $domdefaults{'waf_vpnext'},
14732: sslopt => $domdefaults{'waf_sslopt'},
14733: };
14734: return $proxyinfo;
14735: }
14736:
14737: sub ip_match {
14738: my ($ip,$pattern_str) = @_;
14739: $ip=Net::CIDR::cidrvalidate($ip);
14740: if ($ip) {
14741: return Net::CIDR::cidrlookup($ip,split(/\s*,\s*/,$pattern_str));
14742: }
14743: return;
14744: }
14745:
14746: sub get_proxy_alias {
14747: my ($lonid) = @_;
14748: if ($lonid eq '') {
14749: $lonid = $perlvar{'lonHostID'};
14750: }
14751: if (!defined(&hostname($lonid))) {
14752: return;
14753: }
14754: if ($lonid ne '') {
14755: my ($alias,$cached) = &is_cached_new('proxyalias',$lonid);
14756: if ($cached) {
14757: return $alias;
14758: }
14759: my $dom = &Apache::lonnet::host_domain($lonid);
14760: if ($dom ne '') {
14761: my $cachetime = 60*60*24;
14762: my %domconfig =
14763: &Apache::lonnet::get_dom('configuration',['wafproxy'],$dom);
14764: if (ref($domconfig{'wafproxy'}) eq 'HASH') {
14765: if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
14766: $alias = $domconfig{'wafproxy'}{'alias'}{$lonid};
14767: }
14768: }
14769: return &do_cache_new('proxyalias',$lonid,$alias,$cachetime);
14770: }
14771: }
14772: return;
14773: }
14774:
14775: sub use_proxy_alias {
14776: my ($r,$lonid) = @_;
14777: my $alias = &get_proxy_alias($lonid);
14778: if ($alias) {
14779: my $dom = &host_domain($lonid);
14780: if ($dom ne '') {
14781: my $proxyinfo = &get_proxy_settings($dom);
14782: my ($vpnint,$remote_ip);
14783: if (ref($proxyinfo) eq 'HASH') {
14784: $vpnint = $proxyinfo->{'vpnint'};
14785: if ($vpnint) {
14786: $remote_ip = &get_requestor_ip($r,1,1);
14787: }
14788: }
14789: unless ($vpnint && &ip_match($remote_ip,$vpnint)) {
14790: return $alias;
14791: }
14792: }
14793: }
14794: return;
14795: }
14796:
14797: sub alias_sso {
14798: my ($lonid) = @_;
14799: if ($lonid eq '') {
14800: $lonid = $perlvar{'lonHostID'};
14801: }
14802: if (!defined(&hostname($lonid))) {
14803: return;
14804: }
14805: if ($lonid ne '') {
14806: my ($use_alias,$cached) = &is_cached_new('proxysaml',$lonid);
14807: if ($cached) {
14808: return $use_alias;
14809: }
14810: my $dom = &Apache::lonnet::host_domain($lonid);
14811: if ($dom ne '') {
14812: my $cachetime = 60*60*24;
14813: my %domconfig =
14814: &Apache::lonnet::get_dom('configuration',['wafproxy'],$dom);
14815: if (ref($domconfig{'wafproxy'}) eq 'HASH') {
14816: if (ref($domconfig{'wafproxy'}{'saml'}) eq 'HASH') {
14817: $use_alias = $domconfig{'wafproxy'}{'saml'}{$lonid};
14818: }
14819: }
14820: return &do_cache_new('proxysaml',$lonid,$use_alias,$cachetime);
14821: }
14822: }
14823: return;
14824: }
14825:
14826: sub get_saml_landing {
14827: my ($lonid) = @_;
14828: if ($lonid eq '') {
14829: my $defdom = &default_login_domain();
14830: my @hosts = ¤t_machine_ids();
14831: if (@hosts > 1) {
14832: foreach my $hostid (@hosts) {
14833: if (&host_domain($hostid) eq $defdom) {
14834: $lonid = $hostid;
14835: last;
14836: }
14837: }
14838: } else {
14839: $lonid = $perlvar{'lonHostID'};
14840: }
14841: if ($lonid) {
14842: unless (&Apache::lonnet::host_domain($lonid) eq $defdom) {
14843: return;
14844: }
14845: } else {
14846: return;
14847: }
14848: } elsif (!defined(&hostname($lonid))) {
14849: return;
14850: }
14851: my ($landing,$cached) = &is_cached_new('samllanding',$lonid);
14852: if ($cached) {
14853: return $landing;
14854: }
14855: my $dom = &Apache::lonnet::host_domain($lonid);
14856: if ($dom ne '') {
14857: my $cachetime = 60*60*24;
14858: my %domconfig =
14859: &Apache::lonnet::get_dom('configuration',['login'],$dom);
14860: if (ref($domconfig{'login'}) eq 'HASH') {
14861: if (ref($domconfig{'login'}{'saml'}) eq 'HASH') {
14862: if (ref($domconfig{'login'}{'saml'}{$lonid}) eq 'HASH') {
14863: $landing = 1;
14864: }
14865: }
14866: }
14867: return &do_cache_new('samllanding',$lonid,$landing,$cachetime);
14868: }
14869: return;
14870: }
14871:
1.31 www 14872: # ------------------------------------------------------------- Declutters URLs
14873:
14874: sub declutter {
14875: my $thisfn=shift;
1.569 albertel 14876: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.1172.2.49 raeburn 14877: unless ($thisfn=~m{^/home/httpd/html/priv/}) {
14878: $thisfn=~s{^/home/httpd/html}{};
14879: }
1.31 www 14880: $thisfn=~s/^\///;
1.697 albertel 14881: $thisfn=~s|^adm/wrapper/||;
14882: $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31 www 14883: $thisfn=~s/^res\///;
1.1172 bisitz 14884: $thisfn=~s/^priv\///;
1.1032 raeburn 14885: unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
14886: $thisfn=~s/\?.+$//;
14887: }
1.268 www 14888: return $thisfn;
14889: }
14890:
14891: # ------------------------------------------------------------- Clutter up URLs
14892:
14893: sub clutter {
14894: my $thisfn='/'.&declutter(shift);
1.887 albertel 14895: if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
1.884 albertel 14896: || $thisfn =~ m{^/adm/(includes|pages)} ) {
1.270 www 14897: $thisfn='/res'.$thisfn;
14898: }
1.1031 raeburn 14899: if ($thisfn !~m|^/adm|) {
14900: if ($thisfn =~ m|^/ext/|) {
1.694 albertel 14901: $thisfn='/adm/wrapper'.$thisfn;
1.695 albertel 14902: } else {
14903: my ($ext) = ($thisfn =~ /\.(\w+)$/);
14904: my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698 albertel 14905: if ($embstyle eq 'ssi'
14906: || ($embstyle eq 'hdn')
14907: || ($embstyle eq 'rat')
14908: || ($embstyle eq 'prv')
14909: || ($embstyle eq 'ign')) {
14910: #do nothing with these
14911: } elsif (($embstyle eq 'img')
1.695 albertel 14912: || ($embstyle eq 'emb')
14913: || ($embstyle eq 'wrp')) {
14914: $thisfn='/adm/wrapper'.$thisfn;
1.698 albertel 14915: } elsif ($embstyle eq 'unk'
14916: && $thisfn!~/\.(sequence|page)$/) {
1.695 albertel 14917: $thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698 albertel 14918: } else {
1.718 www 14919: # &logthis("Got a blank emb style");
1.695 albertel 14920: }
1.694 albertel 14921: }
1.1172.2.146. .1(raebu 14922:22): } elsif ($thisfn =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
14923:22): $thisfn='/adm/wrapper'.$thisfn;
1.694 albertel 14924: }
1.31 www 14925: return $thisfn;
1.12 www 14926: }
14927:
1.787 albertel 14928: sub clutter_with_no_wrapper {
14929: my $uri = &clutter(shift);
14930: if ($uri =~ m-^/adm/-) {
14931: $uri =~ s-^/adm/wrapper/-/-;
14932: $uri =~ s-^/adm/coursedocs/showdoc/-/-;
14933: }
14934: return $uri;
14935: }
14936:
1.557 albertel 14937: sub freeze_escape {
14938: my ($value)=@_;
14939: if (ref($value)) {
14940: $value=&nfreeze($value);
14941: return '__FROZEN__'.&escape($value);
14942: }
14943: return &escape($value);
14944: }
14945:
1.11 www 14946:
1.557 albertel 14947: sub thaw_unescape {
14948: my ($value)=@_;
14949: if ($value =~ /^__FROZEN__/) {
14950: substr($value,0,10,undef);
14951: $value=&unescape($value);
14952: return &thaw($value);
14953: }
14954: return &unescape($value);
14955: }
14956:
1.436 albertel 14957: sub correct_line_ends {
14958: my ($result)=@_;
14959: $$result =~s/\r\n/\n/mg;
14960: $$result =~s/\r/\n/mg;
1.415 albertel 14961: }
1.1 albertel 14962: # ================================================================ Main Program
14963:
1.184 www 14964: sub goodbye {
1.204 albertel 14965: &logthis("Starting Shut down");
1.443 albertel 14966: #not converted to using infrastruture and probably shouldn't be
1.870 albertel 14967: &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
1.443 albertel 14968: #converted
1.599 albertel 14969: # &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
1.870 albertel 14970: &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
14971: # &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
14972: # &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
1.425 albertel 14973: #1.1 only
1.870 albertel 14974: # &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
14975: # &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
14976: # &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
14977: # &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
14978: &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
1.599 albertel 14979: &logthis(sprintf("%-20s is %s",'kicks',$kicks));
14980: &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184 www 14981: &flushcourselogs();
14982: &logthis("Shutting down");
14983: }
14984:
1.852 albertel 14985: sub get_dns {
1.1172.2.17 raeburn 14986: my ($url,$func,$ignore_cache,$nocache,$hashref) = @_;
1.869 albertel 14987: if (!$ignore_cache) {
14988: my ($content,$cached)=
14989: &Apache::lonnet::is_cached_new('dns',$url);
14990: if ($cached) {
1.1172.2.17 raeburn 14991: &$func($content,$hashref);
1.869 albertel 14992: return;
14993: }
14994: }
14995:
14996: my %alldns;
1.1172.2.96 raeburn 14997: if (open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab")) {
14998: foreach my $dns (<$config>) {
14999: next if ($dns !~ /^\^(\S*)/x);
15000: my $line = $1;
15001: my ($host,$protocol) = split(/:/,$line);
15002: if ($protocol ne 'https') {
15003: $protocol = 'http';
15004: }
15005: $alldns{$host} = $protocol;
1.979 raeburn 15006: }
1.1172.2.96 raeburn 15007: close($config);
1.869 albertel 15008: }
15009: while (%alldns) {
1.1172.2.52 raeburn 15010: my ($dns) = sort { $b cmp $a } keys(%alldns);
1.1172.2.142 raeburn 15011: my @content;
15012: if ($dns eq Sys::Hostname::FQDN::fqdn()) {
15013: my $command = (split('/',$url))[3];
15014: my ($dir,$file) = &parse_getdns_url($command,$url);
15015: delete($alldns{$dns});
15016: next if (($dir eq '') || ($file eq ''));
15017: if (open(my $config,'<',"$dir/$file")) {
15018: @content = <$config>;
15019: close($config);
15020: }
15021: } else {
15022: my $ua=new LWP::UserAgent;
15023: $ua->timeout(30);
15024: my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
15025: my $response=$ua->request($request);
15026: delete($alldns{$dns});
15027: next if ($response->is_error());
15028: @content = split("\n",$response->content);
15029: }
1.1172.2.17 raeburn 15030: unless ($nocache) {
1.1172.2.23 raeburn 15031: &do_cache_new('dns',$url,\@content,30*24*60*60);
1.1172.2.17 raeburn 15032: }
15033: &$func(\@content,$hashref);
1.869 albertel 15034: return;
1.852 albertel 15035: }
1.871 albertel 15036: my $which = (split('/',$url))[3];
15037: &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
1.1172.2.105 raeburn 15038: if (open(my $config,"<","$perlvar{'lonTabDir'}/dns_$which.tab")) {
15039: my @content = <$config>;
15040: &$func(\@content,$hashref);
15041: }
1.1172.2.17 raeburn 15042: return;
15043: }
15044:
15045: # ------------------------------------------------------Get DNS checksums file
15046: sub parse_dns_checksums_tab {
15047: my ($lines,$hashref) = @_;
1.1172.2.53 raeburn 15048: my $lonhost = $perlvar{'lonHostID'};
15049: my $machine_dom = &Apache::lonnet::host_domain($lonhost);
1.1172.2.17 raeburn 15050: my $loncaparev = &get_server_loncaparev($machine_dom);
1.1172.2.53 raeburn 15051: my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
15052: my $webconfdir = '/etc/httpd/conf';
15053: if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
15054: $webconfdir = '/etc/apache2';
15055: } elsif ($distro =~ /^sles(\d+)$/) {
15056: if ($1 >= 10) {
15057: $webconfdir = '/etc/apache2';
15058: }
15059: } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
15060: if ($1 >= 10.0) {
15061: $webconfdir = '/etc/apache2';
15062: }
15063: }
1.1172.2.17 raeburn 15064: my ($release,$timestamp) = split(/\-/,$loncaparev);
15065: my (%chksum,%revnum);
15066: if (ref($lines) eq 'ARRAY') {
15067: chomp(@{$lines});
1.1172.2.34 raeburn 15068: my $version = shift(@{$lines});
15069: if ($version eq $release) {
1.1172.2.17 raeburn 15070: foreach my $line (@{$lines}) {
1.1172.2.34 raeburn 15071: my ($file,$version,$shasum) = split(/,/,$line);
1.1172.2.53 raeburn 15072: if ($file =~ m{^/etc/httpd/conf}) {
15073: if ($webconfdir eq '/etc/apache2') {
15074: $file =~ s{^\Q/etc/httpd/conf/\E}{$webconfdir/};
15075: }
15076: }
1.1172.2.34 raeburn 15077: $chksum{$file} = $shasum;
15078: $revnum{$file} = $version;
1.1172.2.17 raeburn 15079: }
15080: if (ref($hashref) eq 'HASH') {
15081: %{$hashref} = (
15082: sums => \%chksum,
15083: versions => \%revnum,
15084: );
15085: }
15086: }
15087: }
1.869 albertel 15088: return;
1.852 albertel 15089: }
1.1172.2.17 raeburn 15090:
15091: sub fetch_dns_checksums {
15092: my %checksums;
1.1172.2.34 raeburn 15093: my $machine_dom = &Apache::lonnet::host_domain($perlvar{'lonHostID'});
1.1172.2.48 raeburn 15094: my $loncaparev = &get_server_loncaparev($machine_dom,$perlvar{'lonHostID'});
1.1172.2.34 raeburn 15095: my ($release,$timestamp) = split(/\-/,$loncaparev);
15096: &get_dns("/adm/dns/checksums/$release",\&parse_dns_checksums_tab,1,1,
1.1172.2.17 raeburn 15097: \%checksums);
15098: return \%checksums;
15099: }
15100:
1.1172.2.142 raeburn 15101: sub parse_getdns_url {
15102: my ($command,$url) = @_;
15103: my $dir = $perlvar{'lonTabDir'};
15104: my $file;
15105: if ($command eq 'hosts') {
15106: $file = 'dns_hosts.tab';
15107: } elsif ($command eq 'domain') {
15108: $file = 'dns_domain.tab';
15109: } elsif ($command eq 'checksums') {
15110: my $version = (split('/',$url))[4];
15111: $file = "dns_checksums/$version.tab",
15112: }
15113: return ($dir,$file);
15114: }
15115:
1.327 albertel 15116: # ------------------------------------------------------------ Read domain file
15117: {
1.852 albertel 15118: my $loaded;
1.846 albertel 15119: my %domain;
15120:
1.852 albertel 15121: sub parse_domain_tab {
15122: my ($lines) = @_;
15123: foreach my $line (@$lines) {
15124: next if ($line =~ /^(\#|\s*$ )/x);
1.403 www 15125:
1.846 albertel 15126: chomp($line);
1.852 albertel 15127: my ($name,@elements) = split(/:/,$line,9);
1.846 albertel 15128: my %this_domain;
15129: foreach my $field ('description', 'auth_def', 'auth_arg_def',
15130: 'lang_def', 'city', 'longi', 'lati',
15131: 'primary') {
15132: $this_domain{$field} = shift(@elements);
15133: }
15134: $domain{$name} = \%this_domain;
1.852 albertel 15135: }
15136: }
1.864 albertel 15137:
15138: sub reset_domain_info {
15139: undef($loaded);
15140: undef(%domain);
15141: }
15142:
1.852 albertel 15143: sub load_domain_tab {
1.1172.2.70 raeburn 15144: my ($ignore_cache,$nocache) = @_;
15145: &get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache,$nocache);
1.852 albertel 15146: my $fh;
1.1172.2.96 raeburn 15147: if (open($fh,"<",$perlvar{'lonTabDir'}.'/domain.tab')) {
1.852 albertel 15148: my @lines = <$fh>;
15149: &parse_domain_tab(\@lines);
1.448 albertel 15150: }
1.852 albertel 15151: close($fh);
15152: $loaded = 1;
1.327 albertel 15153: }
1.846 albertel 15154:
15155: sub domain {
1.852 albertel 15156: &load_domain_tab() if (!$loaded);
15157:
1.846 albertel 15158: my ($name,$what) = @_;
15159: return if ( !exists($domain{$name}) );
15160:
15161: if (!$what) {
15162: return $domain{$name}{'description'};
15163: }
15164: return $domain{$name}{$what};
15165: }
1.974 raeburn 15166:
15167: sub domain_info {
15168: &load_domain_tab() if (!$loaded);
15169: return %domain;
15170: }
15171:
1.327 albertel 15172: }
15173:
15174:
1.1 albertel 15175: # ------------------------------------------------------------- Read hosts file
15176: {
1.838 albertel 15177: my %hostname;
1.844 albertel 15178: my %hostdom;
1.845 albertel 15179: my %libserv;
1.852 albertel 15180: my $loaded;
1.888 albertel 15181: my %name_to_host;
1.1074 raeburn 15182: my %internetdom;
1.1107 raeburn 15183: my %LC_dns_serv;
1.852 albertel 15184:
15185: sub parse_hosts_tab {
15186: my ($file) = @_;
15187: foreach my $configline (@$file) {
15188: next if ($configline =~ /^(\#|\s*$ )/x);
1.1107 raeburn 15189: chomp($configline);
15190: if ($configline =~ /^\^/) {
15191: if ($configline =~ /^\^([\w.\-]+)/) {
15192: $LC_dns_serv{$1} = 1;
15193: }
15194: next;
15195: }
1.1074 raeburn 15196: my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
1.852 albertel 15197: $name=~s/\s//g;
15198: if ($id && $domain && $role && $name) {
1.1172.2.96 raeburn 15199: if ((exists($hostname{$id})) && ($hostname{$id} ne '')) {
15200: my $curr = $hostname{$id};
15201: my $skip;
15202: if (ref($name_to_host{$curr}) eq 'ARRAY') {
15203: if (($curr eq $name) && (@{$name_to_host{$curr}} == 1)) {
15204: $skip = 1;
15205: } else {
15206: @{$name_to_host{$curr}} = grep { $_ ne $id } @{$name_to_host{$curr}};
15207: }
15208: }
15209: unless ($skip) {
15210: push(@{$name_to_host{$name}},$id);
15211: }
15212: } else {
15213: push(@{$name_to_host{$name}},$id);
15214: }
1.852 albertel 15215: $hostname{$id}=$name;
15216: $hostdom{$id}=$domain;
15217: if ($role eq 'library') { $libserv{$id}=$name; }
1.969 raeburn 15218: if (defined($protocol)) {
15219: if ($protocol eq 'https') {
15220: $protocol{$id} = $protocol;
15221: } else {
15222: $protocol{$id} = 'http';
15223: }
1.968 raeburn 15224: } else {
1.969 raeburn 15225: $protocol{$id} = 'http';
1.968 raeburn 15226: }
1.1074 raeburn 15227: if (defined($intdom)) {
15228: $internetdom{$id} = $intdom;
15229: }
1.852 albertel 15230: }
15231: }
15232: }
1.864 albertel 15233:
15234: sub reset_hosts_info {
1.897 albertel 15235: &purge_remembered();
1.864 albertel 15236: &reset_domain_info();
15237: &reset_hosts_ip_info();
1.892 albertel 15238: undef(%name_to_host);
1.864 albertel 15239: undef(%hostname);
15240: undef(%hostdom);
15241: undef(%libserv);
15242: undef($loaded);
15243: }
1.1 albertel 15244:
1.852 albertel 15245: sub load_hosts_tab {
1.1172.2.70 raeburn 15246: my ($ignore_cache,$nocache) = @_;
15247: &get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache,$nocache);
1.1172.2.96 raeburn 15248: open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab");
1.852 albertel 15249: my @config = <$config>;
15250: &parse_hosts_tab(\@config);
15251: close($config);
15252: $loaded=1;
1.1 albertel 15253: }
1.852 albertel 15254:
1.838 albertel 15255: sub hostname {
1.852 albertel 15256: &load_hosts_tab() if (!$loaded);
15257:
1.838 albertel 15258: my ($lonid) = @_;
15259: return $hostname{$lonid};
15260: }
1.845 albertel 15261:
1.838 albertel 15262: sub all_hostnames {
1.852 albertel 15263: &load_hosts_tab() if (!$loaded);
15264:
1.838 albertel 15265: return %hostname;
15266: }
1.845 albertel 15267:
1.888 albertel 15268: sub all_names {
1.1172.2.70 raeburn 15269: my ($ignore_cache,$nocache) = @_;
15270: &load_hosts_tab($ignore_cache,$nocache) if (!$loaded);
1.888 albertel 15271:
15272: return %name_to_host;
15273: }
15274:
1.974 raeburn 15275: sub all_host_domain {
15276: &load_hosts_tab() if (!$loaded);
15277: return %hostdom;
15278: }
15279:
1.845 albertel 15280: sub is_library {
1.852 albertel 15281: &load_hosts_tab() if (!$loaded);
15282:
1.845 albertel 15283: return exists($libserv{$_[0]});
15284: }
15285:
15286: sub all_library {
1.852 albertel 15287: &load_hosts_tab() if (!$loaded);
15288:
1.845 albertel 15289: return %libserv;
15290: }
15291:
1.1062 droeschl 15292: sub unique_library {
15293: #2x reverse removes all hostnames that appear more than once
15294: my %unique = reverse &all_library();
15295: return reverse %unique;
15296: }
15297:
1.841 albertel 15298: sub get_servers {
1.852 albertel 15299: &load_hosts_tab() if (!$loaded);
15300:
1.841 albertel 15301: my ($domain,$type) = @_;
15302: my %possible_hosts = ($type eq 'library') ? %libserv
15303: : %hostname;
15304: my %result;
1.842 albertel 15305: if (ref($domain) eq 'ARRAY') {
15306: while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843 albertel 15307: if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842 albertel 15308: $result{$host} = $hostname;
15309: }
15310: }
15311: } else {
15312: while ( my ($host,$hostname) = each(%possible_hosts)) {
15313: if ($hostdom{$host} eq $domain) {
15314: $result{$host} = $hostname;
15315: }
1.841 albertel 15316: }
15317: }
15318: return %result;
15319: }
1.845 albertel 15320:
1.1062 droeschl 15321: sub get_unique_servers {
15322: my %unique = reverse &get_servers(@_);
15323: return reverse %unique;
15324: }
15325:
1.844 albertel 15326: sub host_domain {
1.852 albertel 15327: &load_hosts_tab() if (!$loaded);
15328:
1.844 albertel 15329: my ($lonid) = @_;
15330: return $hostdom{$lonid};
15331: }
15332:
1.841 albertel 15333: sub all_domains {
1.852 albertel 15334: &load_hosts_tab() if (!$loaded);
15335:
1.841 albertel 15336: my %seen;
15337: my @uniq = grep(!$seen{$_}++, values(%hostdom));
15338: return @uniq;
15339: }
1.1074 raeburn 15340:
15341: sub internet_dom {
15342: &load_hosts_tab() if (!$loaded);
15343:
15344: my ($lonid) = @_;
15345: return $internetdom{$lonid};
15346: }
1.1107 raeburn 15347:
15348: sub is_LC_dns {
15349: &load_hosts_tab() if (!$loaded);
15350:
15351: my ($hostname) = @_;
15352: return exists($LC_dns_serv{$hostname});
15353: }
15354:
1.1 albertel 15355: }
15356:
1.847 albertel 15357: {
15358: my %iphost;
1.856 albertel 15359: my %name_to_ip;
15360: my %lonid_to_ip;
1.869 albertel 15361:
1.847 albertel 15362: sub get_hosts_from_ip {
15363: my ($ip) = @_;
15364: my %iphosts = &get_iphost();
15365: if (ref($iphosts{$ip})) {
15366: return @{$iphosts{$ip}};
15367: }
15368: return;
1.839 albertel 15369: }
1.864 albertel 15370:
15371: sub reset_hosts_ip_info {
15372: undef(%iphost);
15373: undef(%name_to_ip);
15374: undef(%lonid_to_ip);
15375: }
1.856 albertel 15376:
15377: sub get_host_ip {
15378: my ($lonid) = @_;
15379: if (exists($lonid_to_ip{$lonid})) {
15380: return $lonid_to_ip{$lonid};
15381: }
15382: my $name=&hostname($lonid);
15383: my $ip = gethostbyname($name);
15384: return if (!$ip || length($ip) ne 4);
15385: $ip=inet_ntoa($ip);
15386: $name_to_ip{$name} = $ip;
15387: $lonid_to_ip{$lonid} = $ip;
15388: return $ip;
15389: }
1.847 albertel 15390:
15391: sub get_iphost {
1.1172.2.70 raeburn 15392: my ($ignore_cache,$nocache) = @_;
1.894 albertel 15393:
1.869 albertel 15394: if (!$ignore_cache) {
15395: if (%iphost) {
15396: return %iphost;
15397: }
15398: my ($ip_info,$cached)=
15399: &Apache::lonnet::is_cached_new('iphost','iphost');
15400: if ($cached) {
15401: %iphost = %{$ip_info->[0]};
15402: %name_to_ip = %{$ip_info->[1]};
15403: %lonid_to_ip = %{$ip_info->[2]};
15404: return %iphost;
15405: }
15406: }
1.894 albertel 15407:
15408: # get yesterday's info for fallback
15409: my %old_name_to_ip;
15410: my ($ip_info,$cached)=
15411: &Apache::lonnet::is_cached_new('iphost','iphost');
15412: if ($cached) {
15413: %old_name_to_ip = %{$ip_info->[1]};
15414: }
15415:
1.1172.2.70 raeburn 15416: my %name_to_host = &all_names($ignore_cache,$nocache);
1.888 albertel 15417: foreach my $name (keys(%name_to_host)) {
1.847 albertel 15418: my $ip;
15419: if (!exists($name_to_ip{$name})) {
15420: $ip = gethostbyname($name);
15421: if (!$ip || length($ip) ne 4) {
1.894 albertel 15422: if (defined($old_name_to_ip{$name})) {
15423: $ip = $old_name_to_ip{$name};
15424: &logthis("Can't find $name defaulting to old $ip");
15425: } else {
15426: &logthis("Name $name no IP found");
15427: next;
15428: }
15429: } else {
15430: $ip=inet_ntoa($ip);
1.847 albertel 15431: }
15432: $name_to_ip{$name} = $ip;
15433: } else {
15434: $ip = $name_to_ip{$name};
1.653 albertel 15435: }
1.888 albertel 15436: foreach my $id (@{ $name_to_host{$name} }) {
15437: $lonid_to_ip{$id} = $ip;
15438: }
15439: push(@{$iphost{$ip}},@{$name_to_host{$name}});
1.598 albertel 15440: }
1.1172.2.70 raeburn 15441: unless ($nocache) {
15442: &do_cache_new('iphost','iphost',
15443: [\%iphost,\%name_to_ip,\%lonid_to_ip],
15444: 48*60*60);
15445: }
1.869 albertel 15446:
1.847 albertel 15447: return %iphost;
1.598 albertel 15448: }
15449:
1.992 raeburn 15450: #
15451: # Given a DNS returns the loncapa host name for that DNS
15452: #
15453: sub host_from_dns {
15454: my ($dns) = @_;
15455: my @hosts;
15456: my $ip;
15457:
1.993 raeburn 15458: if (exists($name_to_ip{$dns})) {
1.992 raeburn 15459: $ip = $name_to_ip{$dns};
15460: }
15461: if (!$ip) {
15462: $ip = gethostbyname($dns); # Initial translation to IP is in net order.
15463: if (length($ip) == 4) {
15464: $ip = &IO::Socket::inet_ntoa($ip);
15465: }
15466: }
15467: if ($ip) {
15468: @hosts = get_hosts_from_ip($ip);
15469: return $hosts[0];
15470: }
15471: return undef;
1.986 foxr 15472: }
1.992 raeburn 15473:
1.1074 raeburn 15474: sub get_internet_names {
15475: my ($lonid) = @_;
15476: return if ($lonid eq '');
15477: my ($idnref,$cached)=
15478: &Apache::lonnet::is_cached_new('internetnames',$lonid);
15479: if ($cached) {
15480: return $idnref;
15481: }
15482: my $ip = &get_host_ip($lonid);
15483: my @hosts = &get_hosts_from_ip($ip);
15484: my %iphost = &get_iphost();
15485: my (@idns,%seen);
15486: foreach my $id (@hosts) {
15487: my $dom = &host_domain($id);
15488: my $prim_id = &domain($dom,'primary');
15489: my $prim_ip = &get_host_ip($prim_id);
15490: next if ($seen{$prim_ip});
15491: if (ref($iphost{$prim_ip}) eq 'ARRAY') {
15492: foreach my $id (@{$iphost{$prim_ip}}) {
15493: my $intdom = &internet_dom($id);
15494: unless (grep(/^\Q$intdom\E$/,@idns)) {
15495: push(@idns,$intdom);
15496: }
15497: }
15498: }
15499: $seen{$prim_ip} = 1;
15500: }
1.1172.2.23 raeburn 15501: return &do_cache_new('internetnames',$lonid,\@idns,12*60*60);
1.1074 raeburn 15502: }
15503:
1.986 foxr 15504: }
15505:
1.1079 raeburn 15506: sub all_loncaparevs {
1.1172.2.39 raeburn 15507: return qw(1.1 1.2 1.3 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 2.10 2.11);
1.1079 raeburn 15508: }
15509:
1.1172.2.27 raeburn 15510: # ------------------------------------------------------- Read loncaparev table
15511: {
15512: sub load_loncaparevs {
15513: if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
1.1172.2.96 raeburn 15514: if (open(my $config,"<","$perlvar{'lonTabDir'}/loncaparevs.tab")) {
1.1172.2.27 raeburn 15515: while (my $configline=<$config>) {
15516: chomp($configline);
15517: my ($hostid,$loncaparev)=split(/:/,$configline);
15518: $loncaparevs{$hostid}=$loncaparev;
15519: }
15520: close($config);
15521: }
15522: }
15523: }
15524: }
15525:
15526: # ----------------------------------------------------- Read serverhostID table
15527: {
15528: sub load_serverhomeIDs {
15529: if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
1.1172.2.96 raeburn 15530: if (open(my $config,"<","$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
1.1172.2.27 raeburn 15531: while (my $configline=<$config>) {
15532: chomp($configline);
15533: my ($name,$id)=split(/:/,$configline);
15534: $serverhomeIDs{$name}=$id;
15535: }
15536: close($config);
15537: }
15538: }
15539: }
15540: }
15541:
15542:
1.862 albertel 15543: BEGIN {
15544:
15545: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
15546: unless ($readit) {
15547: {
15548: my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
15549: %perlvar = (%perlvar,%{$configvars});
15550: }
15551:
15552:
1.1 albertel 15553: # ------------------------------------------------------ Read spare server file
15554: {
1.1172.2.96 raeburn 15555: open(my $config,"<","$perlvar{'lonTabDir'}/spare.tab");
1.1 albertel 15556:
15557: while (my $configline=<$config>) {
15558: chomp($configline);
1.284 matthew 15559: if ($configline) {
1.784 albertel 15560: my ($host,$type) = split(':',$configline,2);
1.785 albertel 15561: if (!defined($type) || $type eq '') { $type = 'default' };
1.784 albertel 15562: push(@{ $spareid{$type} }, $host);
1.1 albertel 15563: }
15564: }
1.448 albertel 15565: close($config);
1.1 albertel 15566: }
1.11 www 15567: # ------------------------------------------------------------ Read permissions
15568: {
1.1172.2.96 raeburn 15569: open(my $config,"<","$perlvar{'lonTabDir'}/roles.tab");
1.11 www 15570:
15571: while (my $configline=<$config>) {
1.448 albertel 15572: chomp($configline);
15573: if ($configline) {
15574: my ($role,$perm)=split(/ /,$configline);
15575: if ($perm ne '') { $pr{$role}=$perm; }
15576: }
1.11 www 15577: }
1.448 albertel 15578: close($config);
1.11 www 15579: }
15580:
15581: # -------------------------------------------- Read plain texts for permissions
15582: {
1.1172.2.96 raeburn 15583: open(my $config,"<","$perlvar{'lonTabDir'}/rolesplain.tab");
1.11 www 15584:
15585: while (my $configline=<$config>) {
1.448 albertel 15586: chomp($configline);
15587: if ($configline) {
1.742 raeburn 15588: my ($short,@plain)=split(/:/,$configline);
15589: %{$prp{$short}} = ();
15590: if (@plain > 0) {
15591: $prp{$short}{'std'} = $plain[0];
15592: for (my $i=1; $i<@plain; $i++) {
15593: $prp{$short}{'alt'.$i} = $plain[$i];
15594: }
15595: }
1.448 albertel 15596: }
1.135 www 15597: }
1.448 albertel 15598: close($config);
1.135 www 15599: }
15600:
15601: # ---------------------------------------------------------- Read package table
15602: {
1.1172.2.96 raeburn 15603: open(my $config,"<","$perlvar{'lonTabDir'}/packages.tab");
1.135 www 15604:
15605: while (my $configline=<$config>) {
1.483 albertel 15606: if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448 albertel 15607: chomp($configline);
15608: my ($short,$plain)=split(/:/,$configline);
15609: my ($pack,$name)=split(/\&/,$short);
15610: if ($plain ne '') {
15611: $packagetab{$pack.'&'.$name.'&name'}=$name;
15612: $packagetab{$short}=$plain;
15613: }
1.11 www 15614: }
1.448 albertel 15615: close($config);
1.329 matthew 15616: }
15617:
1.1172.2.27 raeburn 15618: # --------------------------------------------------------- Read loncaparev table
1.1073 raeburn 15619:
1.1172.2.27 raeburn 15620: &load_loncaparevs();
15621:
15622: # ------------------------------------------------------- Read serverhostID table
15623:
15624: &load_serverhomeIDs();
1.1074 raeburn 15625:
1.1172.2.27 raeburn 15626: # ---------------------------------------------------------- Read releaseslist XML
1.1079 raeburn 15627: {
15628: my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
15629: if (-e $file) {
15630: my $parser = HTML::LCParser->new($file);
15631: while (my $token = $parser->get_token()) {
15632: if ($token->[0] eq 'S') {
15633: my $item = $token->[1];
15634: my $name = $token->[2]{'name'};
15635: my $value = $token->[2]{'value'};
15636: if ($item ne '' && $name ne '' && $value ne '') {
15637: my $release = $parser->get_text();
15638: $release =~ s/(^\s*|\s*$ )//gx;
15639: $needsrelease{$item.':'.$name.':'.$value} = $release;
15640: }
15641: }
15642: }
15643: }
1.1073 raeburn 15644: }
15645:
1.1138 raeburn 15646: # ---------------------------------------------------------- Read managers table
15647: {
15648: if (-e "$perlvar{'lonTabDir'}/managers.tab") {
1.1172.2.96 raeburn 15649: if (open(my $config,"<","$perlvar{'lonTabDir'}/managers.tab")) {
1.1138 raeburn 15650: while (my $configline=<$config>) {
15651: chomp($configline);
15652: next if ($configline =~ /^\#/);
15653: if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
15654: $managerstab{$configline} = 1;
15655: }
15656: }
15657: close($config);
15658: }
15659: }
15660: }
15661:
1.329 matthew 15662: # ------------- set up temporary directory
15663: {
1.1117 foxr 15664: $tmpdir = LONCAPA::tempdir();
1.329 matthew 15665:
1.11 www 15666: }
15667:
1.1172.2.104 raeburn 15668: # ------------- set default texengine (domain default overrides this)
15669: {
15670: $deftex = LONCAPA::texengine();
15671: }
15672:
1.1172.2.114 raeburn 15673: # ------------- set default minimum length for passwords for internal auth users
15674: {
15675: $passwdmin = LONCAPA::passwd_min();
15676: }
15677:
1.794 albertel 15678: $memcache=new Cache::Memcached({'servers' => ['127.0.0.1:11211'],
15679: 'compress_threshold'=> 20_000,
15680: });
1.185 www 15681:
1.281 www 15682: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186 www 15683: $dumpcount=0;
1.958 www 15684: $locknum=0;
1.22 www 15685:
1.163 harris41 15686: &logtouch();
1.672 albertel 15687: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195 www 15688: $readit=1;
1.564 albertel 15689: {
15690: use integer;
15691: my $test=(2**32)+1;
1.568 albertel 15692: if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564 albertel 15693: &logthis(" Detected 64bit platform ($_64bit)");
15694: }
1.195 www 15695: }
1.1 albertel 15696: }
1.179 www 15697:
1.1 albertel 15698: 1;
1.191 harris41 15699: __END__
15700:
1.243 albertel 15701: =pod
15702:
1.191 harris41 15703: =head1 NAME
15704:
1.243 albertel 15705: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191 harris41 15706:
15707: =head1 SYNOPSIS
15708:
1.243 albertel 15709: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191 harris41 15710:
15711: &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
15712:
1.243 albertel 15713: Common parameters:
15714:
15715: =over 4
15716:
15717: =item *
15718:
15719: $uname : an internal username (if $cname expecting a course Id specifically)
15720:
15721: =item *
15722:
15723: $udom : a domain (if $cdom expecting a course's domain specifically)
15724:
15725: =item *
15726:
15727: $symb : a resource instance identifier
15728:
15729: =item *
15730:
15731: $namespace : the name of a .db file that contains the data needed or
15732: being set.
15733:
15734: =back
15735:
1.394 bowersj2 15736: =head1 OVERVIEW
1.191 harris41 15737:
1.394 bowersj2 15738: lonnet provides subroutines which interact with the
15739: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
15740: about classes, users, and resources.
1.243 albertel 15741:
15742: For many of these objects you can also use this to store data about
15743: them or modify them in various ways.
1.191 harris41 15744:
1.394 bowersj2 15745: =head2 Symbs
1.191 harris41 15746:
1.394 bowersj2 15747: To identify a specific instance of a resource, LON-CAPA uses symbols
15748: or "symbs"X<symb>. These identifiers are built from the URL of the
15749: map, the resource number of the resource in the map, and the URL of
15750: the resource itself. The latter is somewhat redundant, but might help
15751: if maps change.
15752:
15753: An example is
15754:
15755: msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
15756:
15757: The respective map entry is
15758:
15759: <resource id="19" src="/res/msu/korte/tests/part12.problem"
15760: title="Problem 2">
15761: </resource>
15762:
15763: Symbs are used by the random number generator, as well as to store and
15764: restore data specific to a certain instance of for example a problem.
15765:
15766: =head2 Storing And Retrieving Data
15767:
15768: X<store()>X<cstore()>X<restore()>Three of the most important functions
15769: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
15770: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
15771: is is the non-critical message twin of cstore. These functions are for
15772: handlers to store a perl hash to a user's permanent data space in an
15773: easy manner, and to retrieve it again on another call. It is expected
15774: that a handler would use this once at the beginning to retrieve data,
15775: and then again once at the end to send only the new data back.
15776:
15777: The data is stored in the user's data directory on the user's
15778: homeserver under the ID of the course.
15779:
15780: The hash that is returned by restore will have all of the previous
15781: value for all of the elements of the hash.
15782:
15783: Example:
15784:
15785: #creating a hash
15786: my %hash;
15787: $hash{'foo'}='bar';
15788:
15789: #storing it
15790: &Apache::lonnet::cstore(\%hash);
15791:
15792: #changing a value
15793: $hash{'foo'}='notbar';
15794:
15795: #adding a new value
15796: $hash{'bar'}='foo';
15797: &Apache::lonnet::cstore(\%hash);
15798:
15799: #retrieving the hash
15800: my %history=&Apache::lonnet::restore();
15801:
15802: #print the hash
15803: foreach my $key (sort(keys(%history))) {
15804: print("\%history{$key} = $history{$key}");
15805: }
15806:
15807: Will print out:
1.191 harris41 15808:
1.394 bowersj2 15809: %history{1:foo} = bar
15810: %history{1:keys} = foo:timestamp
15811: %history{1:timestamp} = 990455579
15812: %history{2:bar} = foo
15813: %history{2:foo} = notbar
15814: %history{2:keys} = foo:bar:timestamp
15815: %history{2:timestamp} = 990455580
15816: %history{bar} = foo
15817: %history{foo} = notbar
15818: %history{timestamp} = 990455580
15819: %history{version} = 2
15820:
15821: Note that the special hash entries C<keys>, C<version> and
15822: C<timestamp> were added to the hash. C<version> will be equal to the
15823: total number of versions of the data that have been stored. The
15824: C<timestamp> attribute will be the UNIX time the hash was
15825: stored. C<keys> is available in every historical section to list which
15826: keys were added or changed at a specific historical revision of a
15827: hash.
15828:
15829: B<Warning>: do not store the hash that restore returns directly. This
15830: will cause a mess since it will restore the historical keys as if the
15831: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191 harris41 15832:
1.394 bowersj2 15833: Calling convention:
1.191 harris41 15834:
1.1172.2.27 raeburn 15835: my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname);
1.1172.2.64 raeburn 15836: &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$laststore);
1.191 harris41 15837:
1.394 bowersj2 15838: For more detailed information, see lonnet specific documentation.
1.191 harris41 15839:
1.394 bowersj2 15840: =head1 RETURN MESSAGES
1.191 harris41 15841:
1.394 bowersj2 15842: =over 4
1.191 harris41 15843:
1.394 bowersj2 15844: =item * B<con_lost>: unable to contact remote host
1.191 harris41 15845:
1.394 bowersj2 15846: =item * B<con_delayed>: unable to contact remote host, message will be delivered
15847: when the connection is brought back up
1.191 harris41 15848:
1.394 bowersj2 15849: =item * B<con_failed>: unable to contact remote host and unable to save message
15850: for later delivery
1.191 harris41 15851:
1.967 bisitz 15852: =item * B<error:>: an error a occurred, a description of the error follows the :
1.191 harris41 15853:
1.394 bowersj2 15854: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243 albertel 15855: that was requested
1.191 harris41 15856:
1.243 albertel 15857: =back
1.191 harris41 15858:
1.243 albertel 15859: =head1 PUBLIC SUBROUTINES
1.191 harris41 15860:
1.243 albertel 15861: =head2 Session Environment Functions
1.191 harris41 15862:
1.243 albertel 15863: =over 4
1.191 harris41 15864:
1.394 bowersj2 15865: =item *
15866: X<appenv()>
1.949 raeburn 15867: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
1.394 bowersj2 15868: the user envirnoment file, and will be restored for each access this
1.620 albertel 15869: user makes during this session, also modifies the %env for the current
1.949 raeburn 15870: process. Optional rolesarrayref - if defined contains a reference to an array
15871: of roles which are exempt from the restriction on modifying user.role entries
15872: in the user's environment.db and in %env.
1.191 harris41 15873:
15874: =item *
1.394 bowersj2 15875: X<delenv()>
1.987 raeburn 15876: B<delenv($delthis,$regexp)>: removes all items from the session
15877: environment file that begin with $delthis. If the
15878: optional second arg - $regexp - is true, $delthis is treated as a
15879: regular expression, otherwise \Q$delthis\E is used.
15880: The values are also deleted from the current processes %env.
1.191 harris41 15881:
1.795 albertel 15882: =item * get_env_multiple($name)
15883:
15884: gets $name from the %env hash, it seemlessly handles the cases where multiple
15885: values may be defined and end up as an array ref.
15886:
15887: returns an array of values
15888:
1.243 albertel 15889: =back
15890:
15891: =head2 User Information
1.191 harris41 15892:
1.243 albertel 15893: =over 4
1.191 harris41 15894:
15895: =item *
1.394 bowersj2 15896: X<queryauthenticate()>
15897: B<queryauthenticate($uname,$udom)>: try to determine user's current
1.191 harris41 15898: authentication scheme
15899:
15900: =item *
1.394 bowersj2 15901: X<authenticate()>
1.1073 raeburn 15902: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
1.394 bowersj2 15903: authenticate user from domain's lib servers (first use the current
15904: one). C<$upass> should be the users password.
1.1073 raeburn 15905: $checkdefauth is optional (value is 1 if a check should be made to
15906: authenticate user using default authentication method, and allow
15907: account creation if username does not have account in the domain).
15908: $clientcancheckhost is optional (value is 1 if checking whether the
15909: server can host will occur on the client side in lonauth.pm).
1.191 harris41 15910:
15911: =item *
1.394 bowersj2 15912: X<homeserver()>
15913: B<homeserver($uname,$udom)>: find the server which has
15914: the user's directory and files (there must be only one), this caches
15915: the answer, and also caches if there is a borken connection.
1.191 harris41 15916:
15917: =item *
1.394 bowersj2 15918: X<idget()>
15919: B<idget($udom,@ids)>: find the usernames behind a list of IDs
15920: (IDs are a unique resource in a domain, there must be only 1 ID per
15921: username, and only 1 username per ID in a specific domain) (returns
15922: hash: id=>name,id=>name)
1.191 harris41 15923:
15924: =item *
1.394 bowersj2 15925: X<idrget()>
15926: B<idrget($udom,@unames)>: find the IDs behind a list of
15927: usernames (returns hash: name=>id,name=>id)
1.191 harris41 15928:
15929: =item *
1.394 bowersj2 15930: X<idput()>
15931: B<idput($udom,%ids)>: store away a list of names and associated IDs
1.191 harris41 15932:
15933: =item *
1.394 bowersj2 15934: X<rolesinit()>
1.1169 droeschl 15935: B<rolesinit($udom,$username)>: get user privileges.
15936: returns user role, first access and timer interval hashes
1.243 albertel 15937:
15938: =item *
1.1171 droeschl 15939: X<privileged()>
15940: B<privileged($username,$domain)>: returns a true if user has a
15941: privileged and active role (i.e. su or dc), false otherwise.
15942:
15943: =item *
1.551 albertel 15944: X<getsection()>
15945: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243 albertel 15946: course $cname, return section name/number or '' for "not in course"
15947: and '-1' for "no section"
15948:
15949: =item *
1.394 bowersj2 15950: X<userenvironment()>
15951: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243 albertel 15952: passed in @what from the requested user's environment, returns a hash
15953:
1.858 raeburn 15954: =item *
15955: X<userlog_query()>
1.859 albertel 15956: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
15957: activity.log file. %filters defines filters applied when parsing the
15958: log file. These can be start or end timestamps, or the type of action
15959: - log to look for Login or Logout events, check for Checkin or
15960: Checkout, role for role selection. The response is in the form
15961: timestamp1:hostid1:event1×tamp2:hostid2:event2 where events are
15962: escaped strings of the action recorded in the activity.log file.
1.858 raeburn 15963:
1.243 albertel 15964: =back
15965:
15966: =head2 User Roles
15967:
15968: =over 4
15969:
15970: =item *
15971:
1.1172.2.65 raeburn 15972: allowed($priv,$uri,$symb,$role,$clientip,$noblockcheck) : check for a user privilege;
15973: returns codes for allowed actions.
15974:
15975: The first argument is required, all others are optional.
15976:
15977: $priv is the privilege being checked.
15978: $uri contains additional information about what is being checked for access (e.g.,
15979: URL, course ID etc.).
15980: $symb is the unique resource instance identifier in a course; if needed,
15981: but not provided, it will be retrieved via a call to &symbread().
15982: $role is the role for which a priv is being checked (only used if priv is evb).
15983: $clientip is the user's IP address (only used when checking for access to portfolio
15984: files).
15985: $noblockcheck, if true, skips calls to &has_comm_blocking() for the bre priv. This
15986: prevents recursive calls to &allowed.
15987:
1.243 albertel 15988: F: full access
15989: U,I,K: authentication modes (cxx only)
15990: '': forbidden
15991: 1: user needs to choose course
15992: 2: browse allowed
1.766 albertel 15993: A: passphrase authentication needed
1.1172.2.65 raeburn 15994: B: access temporarily blocked because of a blocking event in a course.
1.1172.2.146. .1(raebu 15995:22): D: access blocked because access is required via session initiated via deep-link
1.243 albertel 15996:
15997: =item *
15998:
1.1172.2.13 raeburn 15999: constructaccess($url,$setpriv) : check for access to construction space URL
16000:
16001: See if the owner domain and name in the URL match those in the
16002: expected environment. If so, return three element list
16003: ($ownername,$ownerdomain,$ownerhome).
16004:
16005: Otherwise return the null string.
16006:
16007: If second argument 'setpriv' is true, it assigns the privileges,
16008: and returns the same three element list, unless the owner has
16009: blocked "ad hoc" Domain Coordinator access to the Author Space,
16010: in which case the null string is returned.
16011:
16012: =item *
16013:
1.1172.2.84 raeburn 16014: definerole($rolename,$sysrole,$domrole,$courole,$uname,$udom) : define role;
16015: define a custom role rolename set privileges in format of lonTabs/roles.tab
16016: for system, domain, and course level. $uname and $udom are optional (current
16017: user's username and domain will be used when either of $uname or $udom are absent.
1.243 albertel 16018:
16019: =item *
16020:
1.988 raeburn 16021: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash
16022: (rolesplain.tab); plain text explanation of a user role term.
1.1008 raeburn 16023: $type is Course (default) or Community.
1.988 raeburn 16024: If $forcedefault evaluates to true, text returned will be default
16025: text for $type. Otherwise, if this is a course, the text returned
16026: will be a custom name for the role (if defined in the course's
16027: environment). If no custom name is defined the default is returned.
16028:
1.832 raeburn 16029: =item *
16030:
1.1172.2.23 raeburn 16031: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv) :
1.858 raeburn 16032: All arguments are optional. Returns a hash of a roles, either for
16033: co-author/assistant author roles for a user's Construction Space
1.906 albertel 16034: (default), or if $context is 'userroles', roles for the user himself,
1.933 raeburn 16035: In the hash, keys are set to colon-separated $uname,$udom,$role, and
16036: (optionally) if $withsec is true, a fourth colon-separated item - $section.
16037: For each key, value is set to colon-separated start and end times for
16038: the role. If no username and domain are specified, will default to
1.934 raeburn 16039: current user/domain. Types, roles, and roledoms are references to arrays
1.858 raeburn 16040: of role statuses (active, future or previous), roles
16041: (e.g., cc,in, st etc.) and domains of the roles which can be used
16042: to restrict the list of roles reported. If no array ref is
16043: provided for types, will default to return only active roles.
1.834 albertel 16044:
1.1172.2.13 raeburn 16045: =item *
16046:
16047: in_course($udom,$uname,$cdom,$cnum,$type,$hideprivileged) : determine if
16048: user: $uname:$udom has a role in the course: $cdom_$cnum.
16049:
16050: Additional optional arguments are: $type (if role checking is to be restricted
16051: to certain user status types -- previous (expired roles), active (currently
16052: available roles) or future (roles available in the future), and
16053: $hideprivileged -- if true will not report course roles for users who
1.1172.2.23 raeburn 16054: have active Domain Coordinator role in course's domain or in additional
16055: domains (specified in 'Domains to check for privileged users' in course
16056: environment -- set via: Course Settings -> Classlists and staff listing).
16057:
16058: =item *
16059:
16060: privileged($username,$domain,$possdomains,$possroles) : returns 1 if user
16061: $username:$domain is a privileged user (e.g., Domain Coordinator or Super User)
16062: $possdomains and $possroles are optional array refs -- to domains to check and
16063: roles to check. If $possdomains is not specified, a dump will be done of the
16064: users' roles.db to check for a dc or su role in any domain. This can be
16065: time consuming if &privileged is called repeatedly (e.g., when displaying a
16066: classlist), so in such cases, supplying a $possdomains array is preferred, as
16067: this then allows &privileged_by_domain() to be used, which caches the identity
16068: of privileged users, eliminating the need for repeated calls to &dump().
16069:
16070: =item *
16071:
16072: privileged_by_domain($possdomains,$roles) : returns a hash of a hash of a hash,
16073: where the outer hash keys are domains specified in the $possdomains array ref,
16074: next inner hash keys are privileged roles specified in the $roles array ref,
16075: and the innermost hash contains key = value pairs for username:domain = end:start
16076: for active or future "privileged" users with that role in that domain. To avoid
16077: repeated dumps of domain roles -- via &get_domain_roles() -- contents of the
16078: innerhash are cached using priv_$role and $dom as the identifiers.
1.1172.2.13 raeburn 16079:
1.243 albertel 16080: =back
16081:
16082: =head2 User Modification
16083:
16084: =over 4
16085:
16086: =item *
16087:
1.957 raeburn 16088: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
1.243 albertel 16089: user for the level given by URL. Optional start and end dates (leave empty
16090: string or zero for "no date")
1.191 harris41 16091:
16092: =item *
16093:
1.243 albertel 16094: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
16095: change a users, password, possible return values are: ok,
16096: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
16097: refused
1.191 harris41 16098:
16099: =item *
16100:
1.243 albertel 16101: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191 harris41 16102:
16103: =item *
16104:
1.1058 raeburn 16105: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
16106: $forceid,$desiredhome,$email,$inststatus,$candelete) :
16107:
16108: will update user information (firstname,middlename,lastname,generation,
16109: permanentemail), and if forceid is true, student/employee ID also.
16110: A user's institutional affiliation(s) can also be updated.
16111: User information fields will not be overwritten with empty entries
16112: unless the field is included in the $candelete array reference.
16113: This array is included when a single user is modified via "Manage Users",
16114: or when Autoupdate.pl is run by cron in a domain.
1.191 harris41 16115:
16116: =item *
16117:
1.286 matthew 16118: modifystudent
16119:
1.957 raeburn 16120: modify a student's enrollment and identification information.
1.1172.2.31 raeburn 16121: The course id is resolved based on the current user's environment.
16122: This means the invoking user must be a course coordinator or otherwise
1.286 matthew 16123: associated with a course.
16124:
1.297 matthew 16125: This call is essentially a wrapper for lonnet::modifyuser and
16126: lonnet::modify_student_enrollment
1.286 matthew 16127:
16128: Inputs:
16129:
16130: =over 4
16131:
1.957 raeburn 16132: =item B<$udom> Student's loncapa domain
1.286 matthew 16133:
1.957 raeburn 16134: =item B<$uname> Student's loncapa login name
1.286 matthew 16135:
1.964 bisitz 16136: =item B<$uid> Student/Employee ID
1.286 matthew 16137:
1.957 raeburn 16138: =item B<$umode> Student's authentication mode
1.286 matthew 16139:
1.957 raeburn 16140: =item B<$upass> Student's password
1.286 matthew 16141:
1.957 raeburn 16142: =item B<$first> Student's first name
1.286 matthew 16143:
1.957 raeburn 16144: =item B<$middle> Student's middle name
1.286 matthew 16145:
1.957 raeburn 16146: =item B<$last> Student's last name
1.286 matthew 16147:
1.957 raeburn 16148: =item B<$gene> Student's generation
1.286 matthew 16149:
1.957 raeburn 16150: =item B<$usec> Student's section in course
1.286 matthew 16151:
16152: =item B<$end> Unix time of the roles expiration
16153:
16154: =item B<$start> Unix time of the roles start date
16155:
16156: =item B<$forceid> If defined, allow $uid to be changed
16157:
16158: =item B<$desiredhome> server to use as home server for student
16159:
1.957 raeburn 16160: =item B<$email> Student's permanent e-mail address
16161:
16162: =item B<$type> Type of enrollment (auto or manual)
16163:
1.963 raeburn 16164: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto
16165:
16166: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
1.957 raeburn 16167:
1.963 raeburn 16168: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
1.957 raeburn 16169:
1.963 raeburn 16170: =item B<$context> role change context (shown in User Management Logs display in a course)
1.957 raeburn 16171:
1.1172.2.19 raeburn 16172: =item B<$inststatus> institutional status of user - : separated string of escaped status types
16173:
16174: =item B<$credits> Number of credits student will earn from this class - only needs to be supplied if value needs to be different from default credits for class.
1.957 raeburn 16175:
1.286 matthew 16176: =back
1.297 matthew 16177:
16178: =item *
16179:
16180: modify_student_enrollment
16181:
1.1172.2.31 raeburn 16182: Change a student's enrollment status in a class. The environment variable
1.297 matthew 16183: 'role.request.course' must be defined for this function to proceed.
16184:
16185: Inputs:
16186:
16187: =over 4
16188:
1.1172.2.31 raeburn 16189: =item $udom, student's domain
1.297 matthew 16190:
1.1172.2.31 raeburn 16191: =item $uname, student's name
1.297 matthew 16192:
1.1172.2.31 raeburn 16193: =item $uid, student's user id
1.297 matthew 16194:
1.1172.2.31 raeburn 16195: =item $first, student's first name
1.297 matthew 16196:
16197: =item $middle
16198:
16199: =item $last
16200:
16201: =item $gene
16202:
16203: =item $usec
16204:
16205: =item $end
16206:
16207: =item $start
16208:
1.957 raeburn 16209: =item $type
16210:
16211: =item $locktype
16212:
16213: =item $cid
16214:
16215: =item $selfenroll
16216:
16217: =item $context
16218:
1.1172.2.19 raeburn 16219: =item $credits, number of credits student will earn from this class
16220:
1.1172.2.76 raeburn 16221: =item $instsec, institutional course section code for student
16222:
1.297 matthew 16223: =back
16224:
1.191 harris41 16225:
16226: =item *
16227:
1.243 albertel 16228: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
16229: custom role; give a custom role to a user for the level given by URL. Specify
16230: name and domain of role author, and role name
1.191 harris41 16231:
16232: =item *
16233:
1.243 albertel 16234: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191 harris41 16235:
16236: =item *
16237:
1.243 albertel 16238: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
16239:
16240: =back
16241:
16242: =head2 Course Infomation
16243:
16244: =over 4
1.191 harris41 16245:
16246: =item *
16247:
1.1118 foxr 16248: coursedescription($courseid,$options) : returns a hash of information about the
1.631 albertel 16249: specified course id, including all environment settings for the
16250: course, the description of the course will be in the hash under the
16251: key 'description'
1.191 harris41 16252:
1.1118 foxr 16253: $options is an optional parameter that if supplied is a hash reference that controls
16254: what how this function works. It has the following key/values:
16255:
16256: =over 4
16257:
16258: =item freshen_cache
16259:
16260: If defined, and the environment cache for the course is valid, it is
16261: returned in the returned hash.
16262:
16263: =item one_time
16264:
16265: If defined, the last cache time is set to _now_
16266:
16267: =item user
16268:
16269: If defined, the supplied username is used instead of the current user.
16270:
16271:
16272: =back
16273:
1.191 harris41 16274: =item *
16275:
1.624 albertel 16276: resdata($name,$domain,$type,@which) : request for current parameter
16277: setting for a specific $type, where $type is either 'course' or 'user',
16278: @what should be a list of parameters to ask about. This routine caches
1.1172.2.31 raeburn 16279: answers for 10 minutes.
1.243 albertel 16280:
1.877 foxr 16281: =item *
16282:
16283: get_courseresdata($courseid, $domain) : dump the entire course resource
16284: data base, returning a hash that is keyed by the resource name and has
16285: values that are the resource value. I believe that the timestamps and
16286: versions are also returned.
16287:
1.243 albertel 16288: =back
16289:
16290: =head2 Course Modification
16291:
16292: =over 4
1.191 harris41 16293:
16294: =item *
16295:
1.243 albertel 16296: writecoursepref($courseid,%prefs) : write preferences (environment
16297: database) for a course
1.191 harris41 16298:
16299: =item *
16300:
1.1011 raeburn 16301: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
16302:
16303: =item *
16304:
1.1038 raeburn 16305: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
1.243 albertel 16306:
1.1167 droeschl 16307: =item *
16308:
16309: is_course($courseid), is_course($cdom, $cnum)
16310:
16311: Accepts either a combined $courseid (in the form of domain_courseid) or the
16312: two component version $cdom, $cnum. It checks if the specified course exists.
16313:
16314: Returns:
16315: undef if the course doesn't exist, otherwise
16316: in scalar context the combined courseid.
16317: in list context the two components of the course identifier, domain and
16318: courseid.
16319:
1.243 albertel 16320: =back
16321:
1.1172.2.109 raeburn 16322: =head2 Bubblesheet Configuration
16323:
16324: =over 4
16325:
16326: =item *
16327:
16328: get_scantron_config($which)
16329:
16330: $which - the name of the configuration to parse from the file.
16331:
16332: Parses and returns the bubblesheet configuration line selected as a
16333: hash of configuration file fields.
16334:
16335:
16336: Returns:
16337: If the named configuration is not in the file, an empty
16338: hash is returned.
16339:
16340: a hash with the fields
16341: name - internal name for the this configuration setup
16342: description - text to display to operator that describes this config
16343: CODElocation - if 0 or the string 'none'
16344: - no CODE exists for this config
16345: if -1 || the string 'letter'
16346: - a CODE exists for this config and is
16347: a string of letters
16348: Unsupported value (but planned for future support)
16349: if a positive integer
16350: - The CODE exists as the first n items from
16351: the question section of the form
16352: if the string 'number'
16353: - The CODE exists for this config and is
16354: a string of numbers
16355: CODEstart - (only matter if a CODE exists) column in the line where
16356: the CODE starts
16357: CODElength - length of the CODE
16358: IDstart - column where the student/employee ID starts
16359: IDlength - length of the student/employee ID info
16360: Qstart - column where the information from the bubbled
16361: 'questions' start
16362: Qlength - number of columns comprising a single bubble line from
16363: the sheet. (usually either 1 or 10)
16364: Qon - either a single character representing the character used
16365: to signal a bubble was chosen in the positional setup, or
16366: the string 'letter' if the letter of the chosen bubble is
16367: in the final, or 'number' if a number representing the
16368: chosen bubble is in the file (1->A 0->J)
16369: Qoff - the character used to represent that a bubble was
16370: left blank
16371: PaperID - if the scanning process generates a unique number for each
16372: sheet scanned the column that this ID number starts in
16373: PaperIDlength - number of columns that comprise the unique ID number
16374: for the sheet of paper
16375: FirstName - column that the first name starts in
16376: FirstNameLength - number of columns that the first name spans
16377: LastName - column that the last name starts in
16378: LastNameLength - number of columns that the last name spans
16379: BubblesPerRow - number of bubbles available in each row used to
16380: bubble an answer. (If not specified, 10 assumed).
16381:
16382:
16383: =item *
16384:
16385: get_scantronformat_file($cdom)
16386:
16387: $cdom - the course's domain (optional); if not supplied, uses
16388: domain for current $env{'request.course.id'}.
16389:
16390: Returns an array containing lines from the scantron format file for
16391: the domain of the course.
16392:
16393: If a url for a custom.tab file is listed in domain's configuration.db,
16394: lines are from this file.
16395:
16396: Otherwise, if a default.tab has been published in RES space by the
16397: domainconfig user, lines are from this file.
16398:
16399: Otherwise, fall back to getting lines from the legacy file on the
16400: local server: /home/httpd/lonTabs/default_scantronformat.tab
16401:
16402: =back
16403:
1.243 albertel 16404: =head2 Resource Subroutines
16405:
16406: =over 4
1.191 harris41 16407:
16408: =item *
16409:
1.243 albertel 16410: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191 harris41 16411:
16412: =item *
16413:
1.243 albertel 16414: repcopy($filename) : subscribes to the requested file, and attempts to
16415: replicate from the owning library server, Might return
1.607 raeburn 16416: 'unavailable', 'not_found', 'forbidden', 'ok', or
16417: 'bad_request', also attempts to grab the metadata for the
1.243 albertel 16418: resource. Expects the local filesystem pathname
16419: (/home/httpd/html/res/....)
16420:
16421: =back
16422:
16423: =head2 Resource Information
16424:
16425: =over 4
1.191 harris41 16426:
16427: =item *
16428:
1.1172.2.28 raeburn 16429: EXT($varname,$symb,$udom,$uname,$usection,$recurse,$cid) : evaluates
16430: and returns the value of a variety of different possible values,
16431: $varname should be a request string, and the other parameters can be
16432: used to specify who and what one is asking about. Ordinarily, $cid
16433: does not need to be specified, as it is retrived from
16434: $env{'request.course.id'}, but &Apache::lonnet::EXT() is called
16435: within lonuserstate::loadmap() when initializing a course, before
16436: $env{'request.course.id'} has been set, so it needs to be provided
16437: in that one case.
1.243 albertel 16438:
16439: Possible values for $varname are environment.lastname (or other item
16440: from the envirnment hash), user.name (or someother aspect about the
16441: user), resource.0.maxtries (or some other part and parameter of a
16442: resource)
1.204 albertel 16443:
16444: =item *
16445:
1.243 albertel 16446: directcondval($number) : get current value of a condition; reads from a state
16447: string
1.204 albertel 16448:
16449: =item *
16450:
1.243 albertel 16451: condval($condidx) : value of condition index based on state
1.204 albertel 16452:
16453: =item *
16454:
1.1172.2.146. .13(raeb 16455:-23): metadata($uri,$what,$toolsymb,$liburi,$prefix,$depthcount) : request a
1.243 albertel 16456: resource's metadata, $what should be either a specific key, or either
16457: 'keys' (to get a list of possible keys) or 'packages' to get a list of
1.1172.2.146. .13(raeb 16458:-23): packages that this resource currently uses, the last 3 arguments are
16459:-23): only used internally for recursive metadata.
16460:-23):
16461:-23): the toolsymb is only used where the uri is for an external tool (for which
16462:-23): the uri as well as the symb are guaranteed to be unique).
1.243 albertel 16463:
16464: this function automatically caches all requests
1.191 harris41 16465:
16466: =item *
16467:
1.243 albertel 16468: metadata_query($query,$custom,$customshow) : make a metadata query against the
16469: network of library servers; returns file handle of where SQL and regex results
16470: will be stored for query
1.191 harris41 16471:
16472: =item *
16473:
1.1172.2.66 raeburn 16474: symbread($filename,$donotrecurse,$ignorecachednull,$checkforblock,$possibles) :
16475: return symbolic list entry (all arguments optional).
16476:
16477: Args: filename is the filename (including path) for the file for which a symb
16478: is required; donotrecurse, if true will prevent calls to allowed() being made
16479: to check access status if more than one resource was found in the bighash
16480: (see rev. 1.249) to avoid an infinite loop if an ambiguous resource is part of
16481: a randompick); ignorecachednull, if true will prevent a symb of '' being
16482: returned if $env{$cache_str} is defined as ''; checkforblock if true will
16483: cause possible symbs to be checked to determine if they are subject to content
16484: blocking, if so they will not be included as possible symbs; possibles is a
16485: ref to a hash, which, as a side effect, will be populated with all possible
16486: symbs (content blocking not tested).
16487:
1.243 albertel 16488: returns the data handle
1.191 harris41 16489:
16490: =item *
16491:
1.1172.2.17 raeburn 16492: symbverify($symb,$thisfn,$encstate) : verifies that $symb actually exists
1.1172.2.11 raeburn 16493: and is a possible symb for the URL in $thisfn, and if is an encrypted
1.582 albertel 16494: resource that the user accessed using /enc/ returns a 1 on success, 0
1.1172.2.11 raeburn 16495: on failure, user must be in a course, as it assumes the existence of
16496: the course initial hash, and uses $env('request.course.id'}. The third
16497: arg is an optional reference to a scalar. If this arg is passed in the
16498: call to symbverify, it will be set to 1 if the symb has been set to be
16499: encrypted; otherwise it will be null.
1.243 albertel 16500:
1.191 harris41 16501: =item *
16502:
1.243 albertel 16503: symbclean($symb) : removes versions numbers from a symb, returns the
16504: cleaned symb
1.191 harris41 16505:
16506: =item *
16507:
1.243 albertel 16508: is_on_map($uri) : checks if the $uri is somewhere on the current
16509: course map, user must be in a course for it to work.
1.191 harris41 16510:
16511: =item *
16512:
1.243 albertel 16513: numval($salt) : return random seed value (addend for rndseed)
1.191 harris41 16514:
16515: =item *
16516:
1.243 albertel 16517: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
16518: a random seed, all arguments are optional, if they aren't sent it uses the
16519: environment to derive them. Note: if symb isn't sent and it can't get one
16520: from &symbread it will use the current time as its return value
1.191 harris41 16521:
16522: =item *
16523:
1.243 albertel 16524: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
16525: unfakeable, receipt
1.191 harris41 16526:
16527: =item *
16528:
1.620 albertel 16529: receipt() : API to ireceipt working off of env values; given out to users
1.191 harris41 16530:
16531: =item *
16532:
1.243 albertel 16533: countacc($url) : count the number of accesses to a given URL
1.191 harris41 16534:
16535: =item *
16536:
1.243 albertel 16537: 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 16538:
16539: =item *
16540:
1.243 albertel 16541: 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 16542:
16543: =item *
16544:
1.243 albertel 16545: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191 harris41 16546:
16547: =item *
16548:
1.243 albertel 16549: devalidate($symb) : devalidate temporary spreadsheet calculations,
16550: forcing spreadsheet to reevaluate the resource scores next time.
16551:
1.1172.2.13 raeburn 16552: =item *
16553:
16554: can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group) : determine if current user can edit a particular resource,
16555: when viewing in course context.
16556:
16557: input: six args -- filename (decluttered), course number, course domain,
16558: url, symb (if registered) and group (if this is a
16559: group item -- e.g., bulletin board, group page etc.).
16560:
16561: output: array of five scalars --
16562: $cfile -- url for file editing if editable on current server
16563: $home -- homeserver of resource (i.e., for author if published,
16564: or course if uploaded.).
16565: $switchserver -- 1 if server switch will be needed.
16566: $forceedit -- 1 if icon/link should be to go to edit mode
16567: $forceview -- 1 if icon/link should be to go to view mode
16568:
16569: =item *
16570:
16571: is_course_upload($file,$cnum,$cdom)
16572:
16573: Used in course context to determine if current file was uploaded to
16574: the course (i.e., would be found in /userfiles/docs on the course's
16575: homeserver.
16576:
16577: input: 3 args -- filename (decluttered), course number and course domain.
16578: output: boolean -- 1 if file was uploaded.
16579:
1.243 albertel 16580: =back
16581:
16582: =head2 Storing/Retreiving Data
16583:
16584: =over 4
1.191 harris41 16585:
16586: =item *
16587:
1.1172.2.64 raeburn 16588: store($storehash,$symb,$namespace,$udom,$uname,$laststore) : stores hash
16589: permanently for this url; hashref needs to be given and should be a \%hashname;
16590: the remaining args aren't required and if they aren't passed or are '' they will
16591: be derived from the env (with the exception of $laststore, which is an
16592: optional arg used when a user's submission is stored in grading).
16593: $laststore is $version=$timestamp, where $version is the most recent version
16594: number retrieved for the corresponding $symb in the $namespace db file, and
16595: $timestamp is the timestamp for that transaction (UNIX time).
16596: $laststore is currently only passed when cstore() is called by
16597: structuretags::finalize_storage().
1.191 harris41 16598:
16599: =item *
16600:
1.1172.2.64 raeburn 16601: cstore($storehash,$symb,$namespace,$udom,$uname,$laststore) : same as store
16602: but uses critical subroutine
1.191 harris41 16603:
16604: =item *
16605:
1.243 albertel 16606: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
16607: all args are optional
1.191 harris41 16608:
16609: =item *
16610:
1.717 albertel 16611: dumpstore($namespace,$udom,$uname,$regexp,$range) :
16612: dumps the complete (or key matching regexp) namespace into a hash
16613: ($udom, $uname, $regexp, $range are optional) for a namespace that is
16614: normally &store()ed into
16615:
16616: $range should be either an integer '100' (give me the first 100
16617: matching records)
16618: or be two integers sperated by a - with no spaces
16619: '30-50' (give me the 30th through the 50th matching
16620: records)
16621:
16622:
16623: =item *
16624:
1.1172.2.59 raeburn 16625: putstore($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog) :
1.717 albertel 16626: replaces a &store() version of data with a replacement set of data
16627: for a particular resource in a namespace passed in the $storehash hash
1.1172.2.59 raeburn 16628: reference. If $tolog is true, the transaction is logged in the courselog
16629: with an action=PUTSTORE.
1.717 albertel 16630:
16631: =item *
16632:
1.243 albertel 16633: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
16634: works very similar to store/cstore, but all data is stored in a
16635: temporary location and can be reset using tmpreset, $storehash should
16636: be a hash reference, returns nothing on success
1.191 harris41 16637:
16638: =item *
16639:
1.243 albertel 16640: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
16641: similar to restore, but all data is stored in a temporary location and
16642: can be reset using tmpreset. Returns a hash of values on success,
16643: error string otherwise.
1.191 harris41 16644:
16645: =item *
16646:
1.243 albertel 16647: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
16648: deltes all keys for $symb form the temporary storage hash.
1.191 harris41 16649:
16650: =item *
16651:
1.243 albertel 16652: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
16653: reference filled in from namesp ($udom and $uname are optional)
1.191 harris41 16654:
16655: =item *
16656:
1.243 albertel 16657: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
16658: namesp ($udom and $uname are optional)
1.191 harris41 16659:
16660: =item *
16661:
1.702 albertel 16662: dump($namespace,$udom,$uname,$regexp,$range) :
1.243 albertel 16663: dumps the complete (or key matching regexp) namespace into a hash
1.702 albertel 16664: ($udom, $uname, $regexp, $range are optional)
1.449 matthew 16665:
1.702 albertel 16666: $range should be either an integer '100' (give me the first 100
16667: matching records)
16668: or be two integers sperated by a - with no spaces
16669: '30-50' (give me the 30th through the 50th matching
16670: records)
1.449 matthew 16671: =item *
16672:
16673: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
16674: $store can be a scalar, an array reference, or if the amount to be
16675: incremented is > 1, a hash reference.
16676:
16677: ($udom and $uname are optional)
1.191 harris41 16678:
16679: =item *
16680:
1.243 albertel 16681: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
16682: ($udom and $uname are optional)
1.191 harris41 16683:
16684: =item *
16685:
1.243 albertel 16686: cput($namespace,$storehash,$udom,$uname) : critical put
16687: ($udom and $uname are optional)
1.191 harris41 16688:
16689: =item *
16690:
1.748 albertel 16691: newput($namespace,$storehash,$udom,$uname) :
16692:
16693: Attempts to store the items in the $storehash, but only if they don't
16694: currently exist, if this succeeds you can be certain that you have
16695: successfully created a new key value pair in the $namespace db.
16696:
16697:
16698: Args:
16699: $namespace: name of database to store values to
16700: $storehash: hashref to store to the db
16701: $udom: (optional) domain of user containing the db
16702: $uname: (optional) name of user caontaining the db
16703:
16704: Returns:
16705: 'ok' -> succeeded in storing all keys of $storehash
16706: 'key_exists: <key>' -> failed to anything out of $storehash, as at
16707: least <key> already existed in the db (other
16708: requested keys may also already exist)
1.967 bisitz 16709: 'error: <msg>' -> unable to tie the DB or other error occurred
1.748 albertel 16710: 'con_lost' -> unable to contact request server
16711: 'refused' -> action was not allowed by remote machine
16712:
16713:
16714: =item *
16715:
1.243 albertel 16716: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
16717: reference filled in from namesp (encrypts the return communication)
16718: ($udom and $uname are optional)
1.191 harris41 16719:
16720: =item *
16721:
1.243 albertel 16722: log($udom,$name,$home,$message) : write to permanent log for user; use
16723: critical subroutine
16724:
1.806 raeburn 16725: =item *
16726:
1.860 raeburn 16727: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
16728: array reference filled in from namespace found in domain level on either
16729: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806 raeburn 16730:
16731: =item *
16732:
1.860 raeburn 16733: put_dom($namespace,$storehash,$udom,$uhome) : stores hash in namespace at
16734: domain level either on specified domain server ($uhome) or primary domain
16735: server ($udom and $uhome are optional)
1.806 raeburn 16736:
1.943 raeburn 16737: =item *
16738:
1.1172.2.35 raeburn 16739: get_domain_defaults($target_domain,$ignore_cache) : returns hash with defaults
16740: for: authentication, language, quotas, timezone, date locale, and portal URL in
16741: the target domain.
16742:
16743: May also include additional key => value pairs for the following groups:
16744:
16745: =over
16746:
16747: =item
16748: disk quotas (MB allocated by default to portfolios and authoring spaces).
16749:
16750: =over
16751:
16752: =item defaultquota, authorquota
16753:
16754: =back
16755:
16756: =item
16757: tools (availability of aboutme page, blog, webDAV access for authoring spaces,
16758: portfolio for users).
16759:
16760: =over
16761:
16762: =item
16763: aboutme, blog, webdav, portfolio
16764:
16765: =back
16766:
16767: =item
16768: requestcourses: ability to request courses, and how requests are processed.
16769:
16770: =over
16771:
16772: =item
1.1172.2.37 raeburn 16773: official, unofficial, community, textbook
1.1172.2.35 raeburn 16774:
16775: =back
16776:
16777: =item
16778: inststatus: types of institutional affiliation, and order in which they are displayed.
16779:
16780: =over
16781:
16782: =item
1.1172.2.44 raeburn 16783: inststatustypes, inststatusorder, inststatusguest
1.1172.2.35 raeburn 16784:
16785: =back
16786:
16787: =item
16788: coursedefaults: can PDF forms can be created, default credits for courses, default quotas (MB)
16789: for course's uploaded content.
16790:
16791: =over
16792:
16793: =item
1.1172.2.68 raeburn 16794: canuse_pdfforms, officialcredits, unofficialcredits, textbookcredits, officialquota, unofficialquota,
16795: communityquota, textbookquota
1.1172.2.35 raeburn 16796:
16797: =back
16798:
16799: =item
16800: usersessions: set options for hosting of your users in other domains, and hosting of users from other domains
16801: on your servers.
16802:
16803: =over
16804:
16805: =item
16806: remotesessions, hostedsessions
16807:
16808: =back
16809:
16810: =back
16811:
16812: In cases where a domain coordinator has never used the "Set Domain Configuration"
16813: utility to create a configuration.db file on a domain's primary library server
16814: only the following domain defaults: auth_def, auth_arg_def, lang_def
16815: -- corresponding values are authentication type (internal, krb4, krb5,
16816: or localauth), initial password or a kerberos realm, language (e.g., en-us) --
16817: will be available. Values are retrieved from cache (if current), unless the
16818: optional $ignore_cache arg is true, or from domain's configuration.db (if available),
16819: or lastly from values in lonTabs/dns_domain,tab, or lonTabs/domain.tab.
16820:
16821: Typical usage:
1.943 raeburn 16822:
1.1172.2.35 raeburn 16823: %domdefaults = &get_domain_defaults($target_domain);
1.943 raeburn 16824:
1.243 albertel 16825: =back
16826:
16827: =head2 Network Status Functions
16828:
16829: =over 4
1.191 harris41 16830:
16831: =item *
16832:
1.1137 raeburn 16833: dirlist() : return directory list based on URI (first arg).
16834:
16835: Inputs: 1 required, 5 optional.
16836:
16837: =over
16838:
16839: =item
16840: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
16841:
16842: =item
16843: $userdomain - domain of user/course to be listed. Extracted from $uri if absent.
16844:
16845: =item
16846: $username - username of user/course to be listed. Extracted from $uri if absent.
16847:
16848: =item
16849: $getpropath - boolean: 1 if prepend path using &propath().
16850:
16851: =item
16852: $getuserdir - boolean: 1 if prepend path for "userfiles".
16853:
16854: =item
16855: $alternateRoot - path to prepend in place of path from $uri.
16856:
16857: =back
16858:
16859: Returns: Array of up to two items.
16860:
16861: =over
16862:
16863: a reference to an array of files/subdirectories
16864:
16865: =over
16866:
16867: Each element in the array of files/subdirectories is a & separated list of
16868: item name and the result of running stat on the item. If dirlist was requested
16869: for a file instead of a directory, the item name will be ''. For a directory
16870: listing, if the item is a metadata file, the element will end &N&M
16871: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
16872: default copyright set (1).
16873:
16874: =back
16875:
16876: a scalar containing error condition (if encountered).
16877:
16878: =over
16879:
16880: =item
16881: no_host (no homeserver identified for $username:$domain).
16882:
16883: =item
16884: no_such_host (server contacted for listing not identified as valid host).
16885:
16886: =item
16887: con_lost (connection to remote server failed).
16888:
16889: =item
16890: refused (invalid $username:$domain received on lond side).
16891:
16892: =item
16893: no_such_dir (directory at specified path on lond side does not exist).
16894:
16895: =item
16896: empty (directory at specified path on lond side is empty).
16897:
16898: =over
16899:
16900: This is currently not encountered because the &ls3, &ls2,
16901: &ls (_handler) routines on the lond side do not filter out
16902: . and .. from a directory listing.
16903:
16904: =back
16905:
16906: =back
16907:
16908: =back
1.191 harris41 16909:
16910: =item *
16911:
1.243 albertel 16912: spareserver() : find server with least workload from spare.tab
16913:
1.986 foxr 16914:
16915: =item *
16916:
16917: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
16918: if there is no corresponding loncapa host.
16919:
1.243 albertel 16920: =back
16921:
1.986 foxr 16922:
1.243 albertel 16923: =head2 Apache Request
16924:
16925: =over 4
1.191 harris41 16926:
16927: =item *
16928:
1.243 albertel 16929: ssi($url,%hash) : server side include, does a complete request cycle on url to
16930: localhost, posts hash
16931:
16932: =back
16933:
16934: =head2 Data to String to Data
16935:
16936: =over 4
1.191 harris41 16937:
16938: =item *
16939:
1.243 albertel 16940: hash2str(%hash) : convert a hash into a string complete with escaping and '='
16941: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191 harris41 16942:
16943: =item *
16944:
1.243 albertel 16945: hashref2str($hashref) : convert a hashref into a string complete with
16946: escaping and '=' and '&' separators, supports elements that are
16947: arrayrefs and hashrefs
1.191 harris41 16948:
16949: =item *
16950:
1.243 albertel 16951: arrayref2str($arrayref) : convert an arrayref into a string complete
16952: with escaping and '&' separators, supports elements that are arrayrefs
16953: and hashrefs
1.191 harris41 16954:
16955: =item *
16956:
1.243 albertel 16957: str2hash($string) : convert string to hash using unescaping and
16958: splitting on '=' and '&', supports elements that are arrayrefs and
16959: hashrefs
1.191 harris41 16960:
16961: =item *
16962:
1.243 albertel 16963: str2array($string) : convert string to hash using unescaping and
16964: splitting on '&', supports elements that are arrayrefs and hashrefs
16965:
16966: =back
16967:
16968: =head2 Logging Routines
16969:
16970:
16971: These routines allow one to make log messages in the lonnet.log and
16972: lonnet.perm logfiles.
1.191 harris41 16973:
1.1119 foxr 16974: =over 4
16975:
1.191 harris41 16976: =item *
16977:
1.243 albertel 16978: logtouch() : make sure the logfile, lonnet.log, exists
1.191 harris41 16979:
16980: =item *
16981:
1.243 albertel 16982: logthis() : append message to the normal lonnet.log file, it gets
16983: preiodically rolled over and deleted.
1.191 harris41 16984:
16985: =item *
16986:
1.243 albertel 16987: logperm() : append a permanent message to lonnet.perm.log, this log
16988: file never gets deleted by any automated portion of the system, only
16989: messages of critical importance should go in here.
16990:
1.1119 foxr 16991:
1.243 albertel 16992: =back
16993:
16994: =head2 General File Helper Routines
16995:
16996: =over 4
1.191 harris41 16997:
16998: =item *
16999:
1.481 raeburn 17000: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
17001: (a) files in /uploaded
17002: (i) If a local copy of the file exists -
17003: compares modification date of local copy with last-modified date for
17004: definitive version stored on home server for course. If local copy is
17005: stale, requests a new version from the home server and stores it.
17006: If the original has been removed from the home server, then local copy
17007: is unlinked.
17008: (ii) If local copy does not exist -
17009: requests the file from the home server and stores it.
17010:
17011: If $caller is 'uploadrep':
17012: This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
17013: for request for files originally uploaded via DOCS.
17014: - returns 'ok' if fresh local copy now available, -1 otherwise.
17015:
17016: Otherwise:
17017: This indicates a call from the content generation phase of the request.
17018: - returns the entire contents of the file or -1.
17019:
17020: (b) files in /res
17021: - returns the entire contents of a file or -1;
17022: it properly subscribes to and replicates the file if neccessary.
1.191 harris41 17023:
1.712 albertel 17024:
17025: =item *
17026:
17027: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
17028: reference
17029:
17030: returns either a stat() list of data about the file or an empty list
17031: if the file doesn't exist or couldn't find out about it (connection
17032: problems or user unknown)
17033:
1.191 harris41 17034: =item *
17035:
1.243 albertel 17036: filelocation($dir,$file) : returns file system location of a file
17037: based on URI; meant to be "fairly clean" absolute reference, $dir is a
17038: directory that relative $file lookups are to looked in ($dir of /a/dir
17039: and a file of ../bob will become /a/bob)
1.191 harris41 17040:
17041: =item *
17042:
17043: hreflocation($dir,$file) : returns file system location or a URL; same as
17044: filelocation except for hrefs
17045:
17046: =item *
17047:
1.1172.2.49 raeburn 17048: declutter() : declutters URLs -- remove beginning slashes, 'res' etc.
17049: also removes beginning /home/httpd/html unless /priv/ follows it.
1.191 harris41 17050:
1.243 albertel 17051: =back
17052:
1.608 albertel 17053: =head2 Usererfile file routines (/uploaded*)
17054:
17055: =over 4
17056:
17057: =item *
17058:
17059: userfileupload(): main rotine for putting a file in a user or course's
17060: filespace, arguments are,
17061:
1.620 albertel 17062: formname - required - this is the name of the element in $env where the
1.608 albertel 17063: filename, and the contents of the file to create/modifed exist
1.620 albertel 17064: the filename is in $env{'form.'.$formname.'.filename'} and the
17065: contents of the file is located in $env{'form.'.$formname}
1.1090 raeburn 17066: context - if coursedoc, store the file in the course of the active role
17067: of the current user;
17068: if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
17069: if 'canceloverwrite': delete file in tmp/overwrites directory
1.608 albertel 17070: subdir - required - subdirectory to put the file in under ../userfiles/
17071: if undefined, it will be placed in "unknown"
17072:
17073: (This routine calls clean_filename() to remove any dangerous
17074: characters from the filename, and then calls finuserfileupload() to
17075: complete the transaction)
17076:
17077: returns either the url of the uploaded file (/uploaded/....) if successful
17078: and /adm/notfound.html if unsuccessful
17079:
17080: =item *
17081:
17082: clean_filename(): routine for cleaing a filename up for storage in
17083: userfile space, argument is:
17084:
17085: filename - proposed filename
17086:
17087: returns: the new clean filename
17088:
17089: =item *
17090:
1.1090 raeburn 17091: finishuserfileupload(): routine that creates and sends the file to
1.608 albertel 17092: userspace, probably shouldn't be called directly
17093:
17094: docuname: username or courseid of destination for the file
17095: docudom: domain of user/course of destination for the file
17096: formname: same as for userfileupload()
1.1090 raeburn 17097: fname: filename (including subdirectories) for the file
17098: parser: if 'parse', will parse (html) file to extract references to objects, links etc.
1.1172.2.109 raeburn 17099: if hashref, and context is scantron, will convert csv format to standard format
1.1090 raeburn 17100: allfiles: reference to hash used to store objects found by parser
17101: codebase: reference to hash used for codebases of java objects found by parser
17102: thumbwidth: width (pixels) of thumbnail to be created for uploaded image
17103: thumbheight: height (pixels) of thumbnail to be created for uploaded image
17104: resizewidth: width to be used to resize image using resizeImage from ImageMagick
17105: resizeheight: height to be used to resize image using resizeImage from ImageMagick
17106: context: if 'overwrite', will move the uploaded file from its temporary location to
17107: userfiles to facilitate overwriting a previously uploaded file with same name.
1.1095 raeburn 17108: mimetype: reference to scalar to accommodate mime type determined
17109: from File::MMagic if $parser = parse.
1.608 albertel 17110:
17111: returns either the url of the uploaded file (/uploaded/....) if successful
1.1090 raeburn 17112: and /adm/notfound.html if unsuccessful (or an error message if context
17113: was 'overwrite').
17114:
1.608 albertel 17115:
17116: =item *
17117:
17118: renameuserfile(): renames an existing userfile to a new name
17119:
17120: Args:
17121: docuname: username or courseid of destination for the file
17122: docudom: domain of user/course of destination for the file
17123: old: current file name (including any subdirs under userfiles)
17124: new: desired file name (including any subdirs under userfiles)
17125:
17126: =item *
17127:
17128: mkdiruserfile(): creates a directory is a userfiles dir
17129:
17130: Args:
17131: docuname: username or courseid of destination for the file
17132: docudom: domain of user/course of destination for the file
17133: dir: dir to create (including any subdirs under userfiles)
17134:
17135: =item *
17136:
17137: removeuserfile(): removes a file that exists in userfiles
17138:
17139: Args:
17140: docuname: username or courseid of destination for the file
17141: docudom: domain of user/course of destination for the file
17142: fname: filname to delete (including any subdirs under userfiles)
17143:
17144: =item *
17145:
17146: removeuploadedurl(): convience function for removeuserfile()
17147:
17148: Args:
17149: url: a full /uploaded/... url to delete
17150:
1.747 albertel 17151: =item *
17152:
17153: get_portfile_permissions():
17154: Args:
17155: domain: domain of user or course contain the portfolio files
17156: user: name of user or num of course contain the portfolio files
17157: Returns:
17158: hashref of a dump of the proper file_permissions.db
17159:
17160:
17161: =item *
17162:
17163: get_access_controls():
17164:
17165: Args:
17166: current_permissions: the hash ref returned from get_portfile_permissions()
17167: group: (optional) the group you want the files associated with
17168: file: (optional) the file you want access info on
17169:
17170: Returns:
1.749 raeburn 17171: a hash (keys are file names) of hashes containing
17172: keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
17173: values are XML containing access control settings (see below)
1.747 albertel 17174:
17175: Internal notes:
17176:
1.749 raeburn 17177: access controls are stored in file_permissions.db as key=value pairs.
17178: key -> path to file/file_name\0uniqueID:scope_end_start
17179: where scope -> public,guest,course,group,domains or users.
17180: end -> UNIX time for end of access (0 -> no end date)
17181: start -> UNIX time for start of access
17182:
17183: value -> XML description of access control
17184: <scope type=""> (type =1 of: public,guest,course,group,domains,users">
17185: <start></start>
17186: <end></end>
17187:
17188: <password></password> for scope type = guest
17189:
17190: <domain></domain> for scope type = course or group
17191: <number></number>
17192: <roles id="">
17193: <role></role>
17194: <access></access>
17195: <section></section>
17196: <group></group>
17197: </roles>
17198:
17199: <dom></dom> for scope type = domains
17200:
17201: <users> for scope type = users
17202: <user>
17203: <uname></uname>
17204: <udom></udom>
17205: </user>
17206: </users>
17207: </scope>
17208:
17209: Access data is also aggregated for each file in an additional key=value pair:
17210: key -> path to file/file_name\0accesscontrol
17211: value -> reference to hash
17212: hash contains key = value pairs
17213: where key = uniqueID:scope_end_start
17214: value = UNIX time record was last updated
17215:
17216: Used to improve speed of look-ups of access controls for each file.
17217:
17218: Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
17219:
1.1172.2.13 raeburn 17220: =item *
17221:
1.749 raeburn 17222: modify_access_controls():
17223:
17224: Modifies access controls for a portfolio file
17225: Args
17226: 1. file name
17227: 2. reference to hash of required changes,
17228: 3. domain
17229: 4. username
17230: where domain,username are the domain of the portfolio owner
17231: (either a user or a course)
17232:
17233: Returns:
17234: 1. result of additions or updates ('ok' or 'error', with error message).
17235: 2. result of deletions ('ok' or 'error', with error message).
17236: 3. reference to hash of any new or updated access controls.
17237: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
17238: key = integer (inbound ID)
1.1172.2.13 raeburn 17239: value = uniqueID
17240:
17241: =item *
17242:
17243: get_timebased_id():
17244:
17245: Attempts to get a unique timestamp-based suffix for use with items added to a
17246: course via the Course Editor (e.g., folders, composite pages,
17247: group bulletin boards).
17248:
17249: Args: (first three required; six others optional)
17250:
17251: 1. prefix (alphanumeric): of keys in hash, e.g., suppsequence, docspage,
17252: docssequence, or name of group
17253:
17254: 2. keyid (alphanumeric): name of temporary locking key in hash,
17255: e.g., num, boardids
17256:
17257: 3. namespace: name of gdbm file used to store suffixes already assigned;
17258: file will be named nohist_namespace.db
17259:
17260: 4. cdom: domain of course; default is current course domain from %env
17261:
17262: 5. cnum: course number; default is current course number from %env
17263:
17264: 6. idtype: set to concat if an additional digit is to be appended to the
17265: unix timestamp to form the suffix, if the plain timestamp is already
17266: in use. Default is to not do this, but simply increment the unix
17267: timestamp by 1 until a unique key is obtained.
17268:
17269: 7. who: holder of locking key; defaults to user:domain for user.
17270:
17271: 8. locktries: number of attempts to obtain a lock (sleep of 1s before
17272: retrying); default is 3.
17273:
17274: 9. maxtries: number of attempts to obtain a unique suffix; default is 20.
17275:
17276: Returns:
17277:
17278: 1. suffix obtained (numeric)
17279:
17280: 2. result of deleting locking key (ok if deleted, or lock never obtained)
17281:
17282: 3. error: contains (localized) error message if an error occurred.
17283:
1.747 albertel 17284:
1.608 albertel 17285: =back
17286:
1.243 albertel 17287: =head2 HTTP Helper Routines
17288:
17289: =over 4
17290:
1.191 harris41 17291: =item *
17292:
17293: escape() : unpack non-word characters into CGI-compatible hex codes
17294:
17295: =item *
17296:
17297: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
17298:
1.243 albertel 17299: =back
17300:
17301: =head1 PRIVATE SUBROUTINES
17302:
17303: =head2 Underlying communication routines (Shouldn't call)
17304:
17305: =over 4
17306:
17307: =item *
17308:
17309: subreply() : tries to pass a message to lonc, returns con_lost if incapable
17310:
17311: =item *
17312:
17313: reply() : uses subreply to send a message to remote machine, logs all failures
17314:
17315: =item *
17316:
17317: critical() : passes a critical message to another server; if cannot
17318: get through then place message in connection buffer directory and
17319: returns con_delayed, if incapable of saving message, returns
17320: con_failed
17321:
17322: =item *
17323:
17324: reconlonc() : tries to reconnect lonc client processes.
17325:
17326: =back
17327:
17328: =head2 Resource Access Logging
17329:
17330: =over 4
17331:
17332: =item *
17333:
17334: flushcourselogs() : flush (save) buffer logs and access logs
17335:
17336: =item *
17337:
17338: courselog($what) : save message for course in hash
17339:
17340: =item *
17341:
17342: courseacclog($what) : save message for course using &courselog(). Perform
17343: special processing for specific resource types (problems, exams, quizzes, etc).
17344:
1.191 harris41 17345: =item *
17346:
17347: goodbye() : flush course logs and log shutting down; it is called in srm.conf
17348: as a PerlChildExitHandler
1.243 albertel 17349:
17350: =back
17351:
17352: =head2 Other
17353:
17354: =over 4
17355:
17356: =item *
17357:
17358: symblist($mapname,%newhash) : update symbolic storage links
1.191 harris41 17359:
17360: =back
17361:
17362: =cut
1.877 foxr 17363:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>