Annotation of loncom/lonnet/perl/lonnet.pm, revision 1.1172.2.146.2.23
1.1 albertel 1: # The LearningOnline Network
2: # TCP networking package
1.12 www 3: #
1.1172.2.146. .23(raeb 4:-24): # $Id: lonnet.pm,v 1.1172.2.146.2.22 2024/08/25 17:44:07 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,
8107: portfolio => 1,
1.1172.2.146. .22(raeb 8108:-24): portaccess => 1,
.6(raebu 8109:22): timezone => 1,
1.985 raeburn 8110: );
8111: }
1.976 raeburn 8112: return if (!defined($tools{$tool}));
8113:
1.1172.2.35 raeburn 8114: if (($udom eq '') || ($uname eq '')) {
1.976 raeburn 8115: $udom = $env{'user.domain'};
8116: $uname = $env{'user.name'};
8117: }
8118:
1.978 raeburn 8119: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
8120: if ($action ne 'reload') {
1.985 raeburn 8121: if ($context eq 'requestcourses') {
8122: return $env{'environment.canrequest.'.$tool};
1.1172.2.9 raeburn 8123: } elsif ($context eq 'requestauthor') {
8124: return $env{'environment.canrequest.author'};
1.1172.2.146. .18(raeb 8125:-24): } elsif ($context eq 'authordefaults') {
8126:-24): if ($tool eq 'webdav') {
8127:-24): return $env{'environment.availabletools.'.$tool};
8128:-24): }
1.985 raeburn 8129: } else {
8130: return $env{'environment.availabletools.'.$tool};
8131: }
8132: }
1.976 raeburn 8133: }
8134:
1.1172.2.9 raeburn 8135: my ($toolstatus,$inststatus,$envkey);
8136: if ($context eq 'requestauthor') {
8137: $envkey = $context;
1.1172.2.146. .18(raeb 8138:-24): } elsif ($context eq 'authordefaults') {
8139:-24): if ($tool eq 'webdav') {
8140:-24): $envkey = 'tools.'.$tool;
8141:-24): }
1.1172.2.9 raeburn 8142: } else {
8143: $envkey = $context.'.'.$tool;
8144: }
1.976 raeburn 8145:
1.985 raeburn 8146: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
8147: ($action ne 'reload')) {
1.1172.2.9 raeburn 8148: $toolstatus = $env{'environment.'.$envkey};
1.976 raeburn 8149: $inststatus = $env{'environment.inststatus'};
8150: } else {
1.1084 raeburn 8151: if (ref($userenvref) eq 'HASH') {
1.1172.2.9 raeburn 8152: $toolstatus = $userenvref->{$envkey};
1.1084 raeburn 8153: $inststatus = $userenvref->{'inststatus'};
8154: } else {
1.1172.2.9 raeburn 8155: my %userenv = &userenvironment($udom,$uname,$envkey,'inststatus');
8156: $toolstatus = $userenv{$envkey};
1.1084 raeburn 8157: $inststatus = $userenv{'inststatus'};
8158: }
1.976 raeburn 8159: }
8160:
8161: if ($toolstatus ne '') {
8162: if ($toolstatus) {
8163: $access = 1;
8164: } else {
8165: $access = 0;
8166: }
8167: return $access;
8168: }
8169:
1.1084 raeburn 8170: my ($is_adv,%domdef);
8171: if (ref($is_advref) eq 'HASH') {
8172: $is_adv = $is_advref->{'is_adv'};
8173: } else {
8174: $is_adv = &is_advanced_user($udom,$uname);
8175: }
8176: if (ref($domdefref) eq 'HASH') {
8177: %domdef = %{$domdefref};
8178: } else {
8179: %domdef = &get_domain_defaults($udom);
8180: }
1.976 raeburn 8181: if (ref($domdef{$tool}) eq 'HASH') {
8182: if ($is_adv) {
8183: if ($domdef{$tool}{'_LC_adv'} ne '') {
8184: if ($domdef{$tool}{'_LC_adv'}) {
8185: $access = 1;
8186: } else {
8187: $access = 0;
8188: }
8189: return $access;
8190: }
8191: }
8192: if ($inststatus ne '') {
8193: my ($hasaccess,$hasnoaccess);
8194: foreach my $affiliation (split(/:/,$inststatus)) {
8195: if ($domdef{$tool}{$affiliation} ne '') {
8196: if ($domdef{$tool}{$affiliation}) {
8197: $hasaccess = 1;
8198: } else {
8199: $hasnoaccess = 1;
8200: }
8201: }
8202: }
8203: if ($hasaccess || $hasnoaccess) {
8204: if ($hasaccess) {
8205: $access = 1;
8206: } elsif ($hasnoaccess) {
8207: $access = 0;
8208: }
8209: return $access;
8210: }
8211: } else {
8212: if ($domdef{$tool}{'default'} ne '') {
8213: if ($domdef{$tool}{'default'}) {
8214: $access = 1;
8215: } elsif ($domdef{$tool}{'default'} == 0) {
8216: $access = 0;
8217: }
8218: return $access;
8219: }
8220: }
8221: } else {
1.1172.2.6 raeburn 8222: if (($context eq 'tools') && ($tool ne 'webdav')) {
1.985 raeburn 8223: $access = 1;
8224: } else {
8225: $access = 0;
8226: }
1.976 raeburn 8227: return $access;
8228: }
8229: }
8230:
1.1050 raeburn 8231: sub is_course_owner {
8232: my ($cdom,$cnum,$udom,$uname) = @_;
8233: if (($udom eq '') || ($uname eq '')) {
8234: $udom = $env{'user.domain'};
8235: $uname = $env{'user.name'};
8236: }
8237: unless (($udom eq '') || ($uname eq '')) {
8238: if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
8239: if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
8240: return 1;
8241: } else {
8242: my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
8243: if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
8244: return 1;
8245: }
8246: }
8247: }
8248: }
8249: return;
8250: }
8251:
1.976 raeburn 8252: sub is_advanced_user {
8253: my ($udom,$uname) = @_;
1.1085 raeburn 8254: if ($udom ne '' && $uname ne '') {
8255: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
1.1128 raeburn 8256: if (wantarray) {
8257: return ($env{'user.adv'},$env{'user.author'});
8258: } else {
8259: return $env{'user.adv'};
8260: }
1.1085 raeburn 8261: }
8262: }
1.976 raeburn 8263: my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
8264: my %allroles;
1.1128 raeburn 8265: my ($is_adv,$is_author);
1.976 raeburn 8266: foreach my $role (keys(%roleshash)) {
8267: my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
8268: my $area = '/'.$tdomain.'/'.$trest;
8269: if ($sec ne '') {
8270: $area .= '/'.$sec;
8271: }
8272: if (($area ne '') && ($trole ne '')) {
8273: my $spec=$trole.'.'.$area;
8274: if ($trole =~ /^cr\//) {
8275: &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
8276: } elsif ($trole ne 'gr') {
8277: &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
8278: }
1.1128 raeburn 8279: if ($trole eq 'au') {
8280: $is_author = 1;
8281: }
1.976 raeburn 8282: }
8283: }
8284: foreach my $role (keys(%allroles)) {
8285: last if ($is_adv);
8286: foreach my $item (split(/:/,$allroles{$role})) {
8287: if ($item ne '') {
8288: my ($privilege,$restrictions)=split(/&/,$item);
8289: if ($privilege eq 'adv') {
8290: $is_adv = 1;
8291: last;
8292: }
8293: }
8294: }
8295: }
1.1128 raeburn 8296: if (wantarray) {
8297: return ($is_adv,$is_author);
8298: }
1.976 raeburn 8299: return $is_adv;
8300: }
1.798 raeburn 8301:
1.1035 raeburn 8302: sub check_can_request {
1.1172.2.146. .13(raeb 8303:-23): my ($dom,$can_request,$request_domains,$uname,$udom) = @_;
1.1035 raeburn 8304: my $canreq = 0;
1.1172.2.146. .13(raeb 8305:-23): if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
8306:-23): $uname = $env{'user.name'};
8307:-23): $udom = $env{'user.domain'};
8308:-23): }
1.1035 raeburn 8309: my ($types,$typename) = &Apache::loncommon::course_types();
8310: my @options = ('approval','validate','autolimit');
8311: my $optregex = join('|',@options);
8312: if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
8313: foreach my $type (@{$types}) {
1.1172.2.146. .13(raeb 8314:-23): if (&usertools_access($uname,$udom,$type,undef,
8315:-23): 'requestcourses')) {
1.1035 raeburn 8316: $canreq ++;
1.1036 raeburn 8317: if (ref($request_domains) eq 'HASH') {
1.1172.2.146. .13(raeb 8318:-23): push(@{$request_domains->{$type}},$udom);
1.1036 raeburn 8319: }
1.1172.2.146. .13(raeb 8320:-23): if ($dom eq $udom) {
1.1035 raeburn 8321: $can_request->{$type} = 1;
8322: }
8323: }
1.1172.2.146. .13(raeb 8324:-23): if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '') &&
8325:-23): ($env{'environment.reqcrsotherdom.'.$type} ne '')) {
1.1035 raeburn 8326: my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
8327: if (@curr > 0) {
1.1036 raeburn 8328: foreach my $item (@curr) {
8329: if (ref($request_domains) eq 'HASH') {
8330: my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
8331: if ($otherdom ne '') {
8332: if (ref($request_domains->{$type}) eq 'ARRAY') {
8333: unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
8334: push(@{$request_domains->{$type}},$otherdom);
8335: }
8336: } else {
8337: push(@{$request_domains->{$type}},$otherdom);
8338: }
8339: }
8340: }
8341: }
1.1172.2.146. .13(raeb 8342:-23): unless ($dom eq $env{'user.domain'}) {
1.1036 raeburn 8343: $canreq ++;
1.1035 raeburn 8344: if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
8345: $can_request->{$type} = 1;
8346: }
8347: }
8348: }
8349: }
8350: }
8351: }
8352: return $canreq;
8353: }
8354:
1.341 www 8355: # ---------------------------------------------- Custom access rule evaluation
8356:
8357: sub customaccess {
8358: my ($priv,$uri)=@_;
1.807 albertel 8359: my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819 www 8360: my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807 albertel 8361: $udom = &LONCAPA::clean_domain($udom);
8362: $ucrs = &LONCAPA::clean_username($ucrs);
1.341 www 8363: my $access=0;
1.800 albertel 8364: foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.893 albertel 8365: my ($effect,$realm,$role,$type)=split(/\:/,$right);
8366: if ($type eq 'user') {
8367: foreach my $scope (split(/\s*\,\s*/,$realm)) {
1.896 albertel 8368: my ($tdom,$tuname)=split(m{/},$scope);
1.893 albertel 8369: if ($tdom) {
8370: if ($tdom ne $env{'user.domain'}) { next; }
8371: }
1.896 albertel 8372: if ($tuname) {
8373: if ($tuname ne $env{'user.name'}) { next; }
1.893 albertel 8374: }
8375: $access=($effect eq 'allow');
8376: last;
8377: }
8378: } else {
8379: if ($role) {
8380: if ($role ne $urole) { next; }
8381: }
8382: foreach my $scope (split(/\s*\,\s*/,$realm)) {
8383: my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
8384: if ($tdom) {
8385: if ($tdom ne $udom) { next; }
8386: }
8387: if ($tcrs) {
8388: if ($tcrs ne $ucrs) { next; }
8389: }
8390: if ($tsec) {
8391: if ($tsec ne $usec) { next; }
8392: }
8393: $access=($effect eq 'allow');
8394: last;
8395: }
8396: if ($realm eq '' && $role eq '') {
8397: $access=($effect eq 'allow');
8398: }
1.402 bowersj2 8399: }
1.341 www 8400: }
8401: return $access;
8402: }
8403:
1.103 harris41 8404: # ------------------------------------------------- Check for a user privilege
1.12 www 8405:
8406: sub allowed {
1.1172.2.146. .1(raebu 8407:22): my ($priv,$uri,$symb,$role,$clientip,$noblockcheck,$ignorecache,$nodeeplinkcheck,$nodeeplinkout)=@_;
1.705 albertel 8408: my $ver_orguri=$uri;
1.439 www 8409: $uri=&deversion($uri);
1.152 www 8410: my $orguri=$uri;
1.52 www 8411: $uri=&declutter($uri);
1.809 raeburn 8412:
1.810 raeburn 8413: if ($priv eq 'evb') {
1.1172.2.146. .3(raebu 8414:22): # Evade communication block restrictions for specified role in a course or domain
1.810 raeburn 8415: if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
8416: return $1;
8417: } else {
8418: return;
8419: }
8420: }
8421:
1.620 albertel 8422: if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54 www 8423: # Free bre access to adm and meta resources
1.1172.2.146. .1(raebu 8424:22): if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard|viewclasslist|aboutme|ext\.tool)$}))
1.769 albertel 8425: || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) ))
8426: && ($priv eq 'bre')) {
1.14 www 8427: return 'F';
1.159 www 8428: }
8429:
1.545 banghart 8430: # Free bre access to user's own portfolio contents
1.714 raeburn 8431: my ($space,$domain,$name,@dir)=split('/',$uri);
1.647 raeburn 8432: if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) &&
1.714 raeburn 8433: ($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814 raeburn 8434: my %setters;
1.1172.2.142 raeburn 8435: my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) =
8436: &Apache::loncommon::blockcheck(\%setters,'port',$clientip);
8437: if (($startblock && $endblock) || ($by_ip)) {
1.814 raeburn 8438: return 'B';
8439: } else {
8440: return 'F';
8441: }
1.545 banghart 8442: }
8443:
1.762 raeburn 8444: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714 raeburn 8445: if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups')
8446: && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
8447: if (exists($env{'request.course.id'})) {
8448: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
8449: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
8450: if (($domain eq $cdom) && ($name eq $cnum)) {
8451: my $courseprivid=$env{'request.course.id'};
8452: $courseprivid=~s/\_/\//;
8453: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
8454: .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
8455: return $1;
1.762 raeburn 8456: } else {
8457: if ($env{'request.course.sec'}) {
8458: $courseprivid.='/'.$env{'request.course.sec'};
8459: }
8460: if ($env{'user.priv.'.$env{'request.role'}.'./'.
8461: $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
8462: return $2;
8463: }
1.714 raeburn 8464: }
8465: }
8466: }
8467: }
8468:
1.159 www 8469: # Free bre to public access
8470:
8471: if ($priv eq 'bre') {
1.1172.2.146. .1(raebu 8472:22): my $copyright;
8473:22): unless ($uri =~ /ext\.tool/) {
8474:22): $copyright=&metadata($uri,'copyright');
8475:22): }
1.620 albertel 8476: if (($copyright eq 'public') && (!$env{'request.course.id'})) {
1.301 www 8477: return 'F';
8478: }
1.238 www 8479: if ($copyright eq 'priv') {
8480: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 8481: unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238 www 8482: return '';
8483: }
8484: }
8485: if ($copyright eq 'domain') {
8486: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 8487: unless (($env{'user.domain'} eq $1) ||
8488: ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238 www 8489: return '';
8490: }
1.262 matthew 8491: }
1.620 albertel 8492: if ($env{'request.role'}=~ /li\.\//) {
1.262 matthew 8493: # Library role, so allow browsing of resources in this domain.
8494: return 'F';
1.238 www 8495: }
1.341 www 8496: if ($copyright eq 'custom') {
8497: unless (&customaccess($priv,$uri)) { return ''; }
8498: }
1.14 www 8499: }
1.264 matthew 8500: # Domain coordinator is trying to create a course
1.620 albertel 8501: if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264 matthew 8502: # uri is the requested domain in this case.
8503: # comparison to 'request.role.domain' shows if the user has selected
1.678 raeburn 8504: # a role of dc for the domain in question.
1.620 albertel 8505: return 'F' if ($uri eq $env{'request.role.domain'});
1.264 matthew 8506: }
1.29 www 8507:
1.52 www 8508: my $thisallowed='';
8509: my $statecond=0;
8510: my $courseprivid='';
8511:
1.1039 raeburn 8512: my $ownaccess;
1.1043 raeburn 8513: # Community Coordinator or Assistant Co-author browsing resource space.
1.1039 raeburn 8514: if (($priv eq 'bro') && ($env{'user.author'})) {
8515: if ($uri eq '') {
8516: $ownaccess = 1;
8517: } else {
8518: if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
8519: my $udom = $env{'user.domain'};
8520: my $uname = $env{'user.name'};
8521: if ($uri =~ m{^\Q$udom\E/?$}) {
8522: $ownaccess = 1;
1.1040 raeburn 8523: } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
1.1039 raeburn 8524: unless ($uri =~ m{\.\./}) {
8525: $ownaccess = 1;
8526: }
8527: } elsif (($udom ne 'public') && ($uname ne 'public')) {
8528: my $now = time;
8529: if ($uri =~ m{^([^/]+)/?$}) {
8530: my $adom = $1;
8531: foreach my $key (keys(%env)) {
1.1042 raeburn 8532: if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
1.1172.2.142 raeburn 8533: my ($start,$end) = split(/\./,$env{$key});
8534: if (($now >= $start) && (!$end || $end > $now)) {
1.1039 raeburn 8535: $ownaccess = 1;
8536: last;
8537: }
8538: }
8539: }
8540: } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
8541: my $adom = $1;
8542: my $aname = $2;
1.1042 raeburn 8543: foreach my $role ('ca','aa') {
8544: if ($env{"user.role.$role./$adom/$aname"}) {
8545: my ($start,$end) =
1.1172.2.142 raeburn 8546: split(/\./,$env{"user.role.$role./$adom/$aname"});
8547: if (($now >= $start) && (!$end || $end > $now)) {
1.1042 raeburn 8548: $ownaccess = 1;
8549: last;
8550: }
1.1039 raeburn 8551: }
8552: }
8553: }
8554: }
8555: }
8556: }
8557: }
8558:
1.52 www 8559: # Course
8560:
1.620 albertel 8561: if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 8562: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 8563: $thisallowed.=$1;
8564: }
1.52 www 8565: }
1.29 www 8566:
1.52 www 8567: # Domain
8568:
1.620 albertel 8569: if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479 albertel 8570: =~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 8571: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 8572: $thisallowed.=$1;
8573: }
1.12 www 8574: }
1.52 www 8575:
1.1141 raeburn 8576: # User who is not author or co-author might still be able to edit
8577: # resource of an author in the domain (e.g., if Domain Coordinator).
8578: if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
8579: (&allowed('mdc',$env{'request.course.id'}))) {
8580: if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
8581: $thisallowed.=$1;
8582: }
8583: }
8584:
1.52 www 8585: # Course: uri itself is a course
1.66 www 8586: my $courseuri=$uri;
8587: $courseuri=~s/\_(\d)/\/$1/;
1.83 www 8588: $courseuri=~s/^([^\/])/\/$1/;
1.81 www 8589:
1.620 albertel 8590: if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479 albertel 8591: =~/\Q$priv\E\&([^\:]*)/) {
1.1172.2.115 raeburn 8592: if ($priv eq 'mip') {
8593: my $rem = $1;
8594: if (($uri ne '') && ($env{'request.course.id'} eq $uri) &&
8595: ($env{'course.'.$env{'request.course.id'}.'.internal.courseowner'} eq $env{'user.name'}.':'.$env{'user.domain'})) {
8596: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
8597: if ($cdom ne '') {
8598: my %passwdconf = &get_passwdconf($cdom);
8599: if (ref($passwdconf{'crsownerchg'}) eq 'HASH') {
8600: if (ref($passwdconf{'crsownerchg'}{'by'}) eq 'ARRAY') {
8601: if (@{$passwdconf{'crsownerchg'}{'by'}}) {
8602: my @inststatuses = split(':',$env{'environment.inststatus'});
8603: unless (@inststatuses) {
8604: @inststatuses = ('default');
8605: }
8606: foreach my $status (@inststatuses) {
8607: if (grep(/^\Q$status\E$/,@{$passwdconf{'crsownerchg'}{'by'}})) {
8608: $thisallowed.=$rem;
8609: }
8610: }
8611: }
8612: }
8613: }
8614: }
8615: }
8616: } else {
8617: unless (($priv eq 'bro') && (!$ownaccess)) {
8618: $thisallowed.=$1;
8619: }
1.1039 raeburn 8620: }
1.12 www 8621: }
1.29 www 8622:
1.665 albertel 8623: # URI is an uploaded document for this course, default permissions don't matter
1.611 albertel 8624: # not allowing 'edit' access (editupload) to uploaded course docs
1.492 albertel 8625: if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665 albertel 8626: $thisallowed='';
1.671 raeburn 8627: my ($match)=&is_on_map($uri);
8628: if ($match) {
8629: if ($env{'user.priv.'.$env{'request.role'}.'./'}
8630: =~/\Q$priv\E\&([^\:]*)/) {
1.1172.2.65 raeburn 8631: my $value = $1;
1.1172.2.146. .1(raebu 8632:22): my $deeplinkblock;
8633:22): unless ($nodeeplinkcheck) {
8634:22): $deeplinkblock = &deeplink_check($priv,$symb,$uri);
8635:22): }
8636:22): if ($deeplinkblock) {
8637:22): $thisallowed='D';
8638:22): } elsif ($noblockcheck) {
1.1172.2.65 raeburn 8639: $thisallowed.=$value;
1.1162 raeburn 8640: } else {
1.1172.2.126 raeburn 8641: my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
1.1172.2.65 raeburn 8642: if (@blockers > 0) {
8643: $thisallowed = 'B';
8644: } else {
8645: $thisallowed.=$value;
8646: }
1.1162 raeburn 8647: }
1.671 raeburn 8648: }
8649: } else {
1.705 albertel 8650: my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671 raeburn 8651: if ($refuri) {
8652: if ($refuri =~ m|^/adm/|) {
1.669 raeburn 8653: $thisallowed='F';
1.671 raeburn 8654: } else {
8655: $refuri=&declutter($refuri);
8656: my ($match) = &is_on_map($refuri);
8657: if ($match) {
1.1172.2.146. .1(raebu 8658:22): my $deeplinkblock;
8659:22): unless ($nodeeplinkcheck) {
8660:22): $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
8661:22): }
8662:22): if ($deeplinkblock) {
8663:22): $thisallowed='D';
8664:22): } elsif ($noblockcheck) {
1.1162 raeburn 8665: $thisallowed='F';
1.1172.2.65 raeburn 8666: } else {
1.1172.2.127 raeburn 8667: my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
1.1172.2.65 raeburn 8668: if (@blockers > 0) {
8669: $thisallowed = 'B';
8670: } else {
8671: $thisallowed='F';
8672: }
1.1162 raeburn 8673: }
1.671 raeburn 8674: }
1.669 raeburn 8675: }
1.671 raeburn 8676: }
8677: }
1.314 www 8678: }
1.492 albertel 8679:
1.766 albertel 8680: if ($priv eq 'bre'
8681: && $thisallowed ne 'F'
8682: && $thisallowed ne '2'
8683: && &is_portfolio_url($uri)) {
1.1172.2.77 raeburn 8684: $thisallowed = &portfolio_access($uri,$clientip);
1.766 albertel 8685: }
8686:
1.52 www 8687: # Full access at system, domain or course-wide level? Exit.
1.29 www 8688: if ($thisallowed=~/F/) {
8689: return 'F';
8690: }
8691:
1.52 www 8692: # If this is generating or modifying users, exit with special codes
1.29 www 8693:
1.1172.2.146. .22(raeb 8694:-24): if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa::vca:vaa:'=~/\:\Q$priv\E\:/) {
1.643 www 8695: if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642 albertel 8696: my ($audom,$auname)=split('/',$uri);
1.643 www 8697: # no author name given, so this just checks on the general right to make a co-author in this domain
8698: unless ($auname) { return $thisallowed; }
8699: # an author name is given, so we are about to actually make a co-author for a certain account
1.642 albertel 8700: if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
8701: (($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
8702: ($audom ne $env{'request.role.domain'}))) { return ''; }
1.1172.2.146. .22(raeb 8703:-24): } elsif (($priv eq 'vca') || ($priv eq 'vaa')) {
8704:-24): my ($audom,$auname)=split('/',$uri);
8705:-24): unless ($auname) { return $thisallowed; }
8706:-24): unless (($env{'request.role'} eq "dc./$audom") ||
8707:-24): ($env{'request.role'} eq "ca./$uri")) {
8708:-24): return '';
8709:-24): }
1.642 albertel 8710: }
1.52 www 8711: return $thisallowed;
8712: }
8713: #
1.103 harris41 8714: # Gathered so far: system, domain and course wide privileges
1.52 www 8715: #
8716: # Course: See if uri or referer is an individual resource that is part of
8717: # the course
8718:
1.620 albertel 8719: if ($env{'request.course.id'}) {
1.232 www 8720:
1.1172.2.146. .13(raeb 8721:-23): if ($priv eq 'bre') {
8722:-23): if (&is_coursetool_logo($uri)) {
8723:-23): return 'F';
8724:-23): }
8725:-23): }
8726:-23):
1.1172.2.115 raeburn 8727: # If this is modifying password (internal auth) domains must match for user and user's role.
8728:
8729: if ($priv eq 'mip') {
8730: if ($env{'user.domain'} eq $env{'request.role.domain'}) {
8731: return $thisallowed;
8732: } else {
8733: return '';
8734: }
8735: }
8736:
1.620 albertel 8737: $courseprivid=$env{'request.course.id'};
8738: if ($env{'request.course.sec'}) {
8739: $courseprivid.='/'.$env{'request.course.sec'};
1.52 www 8740: }
8741: $courseprivid=~s/\_/\//;
8742: my $checkreferer=1;
1.232 www 8743: my ($match,$cond)=&is_on_map($uri);
8744: if ($match) {
8745: $statecond=$cond;
1.620 albertel 8746: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 8747: =~/\Q$priv\E\&([^\:]*)/) {
1.1162 raeburn 8748: my $value = $1;
8749: if ($priv eq 'bre') {
1.1172.2.146. .1(raebu 8750:22): my $deeplinkblock;
8751:22): unless ($nodeeplinkcheck) {
8752:22): $deeplinkblock = &deeplink_check($priv,$symb,$uri);
8753:22): }
8754:22): if ($deeplinkblock) {
8755:22): $thisallowed = 'D';
8756:22): } elsif ($noblockcheck) {
1.1162 raeburn 8757: $thisallowed.=$value;
1.1172.2.65 raeburn 8758: } else {
1.1172.2.126 raeburn 8759: my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
1.1172.2.65 raeburn 8760: if (@blockers > 0) {
8761: $thisallowed = 'B';
8762: } else {
8763: $thisallowed.=$value;
8764: }
1.1162 raeburn 8765: }
8766: } else {
8767: $thisallowed.=$value;
8768: }
1.52 www 8769: $checkreferer=0;
8770: }
1.29 www 8771: }
1.1172.2.126 raeburn 8772:
1.148 www 8773: if ($checkreferer) {
1.620 albertel 8774: my $refuri=$env{'httpref.'.$orguri};
1.148 www 8775: unless ($refuri) {
1.800 albertel 8776: foreach my $key (keys(%env)) {
8777: if ($key=~/^httpref\..*\*/) {
8778: my $pattern=$key;
1.156 www 8779: $pattern=~s/^httpref\.\/res\///;
1.148 www 8780: $pattern=~s/\*/\[\^\/\]\+/g;
8781: $pattern=~s/\//\\\//g;
1.152 www 8782: if ($orguri=~/$pattern/) {
1.800 albertel 8783: $refuri=$env{$key};
1.148 www 8784: }
8785: }
1.191 harris41 8786: }
1.148 www 8787: }
1.232 www 8788:
1.148 www 8789: if ($refuri) {
1.152 www 8790: $refuri=&declutter($refuri);
1.232 www 8791: my ($match,$cond)=&is_on_map($refuri);
8792: if ($match) {
8793: my $refstatecond=$cond;
1.620 albertel 8794: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 8795: =~/\Q$priv\E\&([^\:]*)/) {
1.1162 raeburn 8796: my $value = $1;
8797: if ($priv eq 'bre') {
1.1172.2.146. .1(raebu 8798:22): my $deeplinkblock;
8799:22): unless ($nodeeplinkcheck) {
8800:22): $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
8801:22): }
8802:22): if ($deeplinkblock) {
8803:22): $thisallowed = 'D';
8804:22): } elsif ($noblockcheck) {
1.1162 raeburn 8805: $thisallowed.=$value;
1.1172.2.65 raeburn 8806: } else {
1.1172.2.127 raeburn 8807: my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
1.1172.2.65 raeburn 8808: if (@blockers > 0) {
8809: $thisallowed = 'B';
8810: } else {
8811: $thisallowed.=$value;
8812: }
1.1162 raeburn 8813: }
8814: } else {
8815: $thisallowed.=$value;
8816: }
1.53 www 8817: $uri=$refuri;
8818: $statecond=$refstatecond;
1.52 www 8819: }
8820: }
1.148 www 8821: }
1.29 www 8822: }
1.52 www 8823: }
1.29 www 8824:
1.52 www 8825: #
1.103 harris41 8826: # Gathered now: all privileges that could apply, and condition number
1.52 www 8827: #
8828: #
8829: # Full or no access?
8830: #
1.29 www 8831:
1.52 www 8832: if ($thisallowed=~/F/) {
8833: return 'F';
8834: }
1.29 www 8835:
1.52 www 8836: unless ($thisallowed) {
8837: return '';
8838: }
1.29 www 8839:
1.52 www 8840: # Restrictions exist, deal with them
8841: #
8842: # C:according to course preferences
8843: # R:according to resource settings
8844: # L:unless locked
8845: # X:according to user session state
8846: #
8847:
8848: # Possibly locked functionality, check all courses
1.1172.2.142 raeburn 8849: # In roles.tab, L (unless locked) available for bre, pch, plc, pac and sma.
1.54 www 8850: # Locks might take effect only after 10 minutes cache expiration for other
1.1172.2.142 raeburn 8851: # courses, and 2 minutes for current course, in which user has st or ta role
8852: # which is neither expired nor a future role (unless current course).
1.52 www 8853:
1.1172.2.142 raeburn 8854: my ($needlockcheck,$now,$crsonly);
1.52 www 8855: if ($thisallowed=~/L/) {
1.1172.2.142 raeburn 8856: $now = time;
8857: if ($priv eq 'bre') {
8858: if ($uri ne '') {
8859: if ($orguri =~ m{^/+res/}) {
8860: if ($uri =~ m{^lib/templates/}) {
8861: if ($env{'request.course.id'}) {
8862: $crsonly = 1;
8863: $needlockcheck = 1;
8864: }
8865: } else {
8866: $needlockcheck = 1;
8867: }
8868: } elsif ($env{'request.course.id'}) {
8869: my ($crsdom,$crsnum) = split('_',$env{'request.course.id'});
8870: if (($uri =~ m{^(adm|uploaded|public)/$crsdom/$crsnum/}) ||
8871: ($uri =~ m{^adm/$match_domain/$match_username/\d+/(smppg|bulletinboard)$})) {
8872: $crsonly = 1;
8873: }
8874: $needlockcheck = 1;
8875: }
8876: }
8877: } elsif (($priv eq 'pch') || ($priv eq 'plc') || ($priv eq 'pac') || ($priv eq 'sma')) {
8878: $needlockcheck = 1;
8879: }
8880: }
8881: if ($needlockcheck) {
8882: foreach my $envkey (keys(%env)) {
1.54 www 8883: if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
8884: my $courseid=$2;
8885: my $roleid=$1.'.'.$2;
1.92 www 8886: $courseid=~s/^\///;
1.1172.2.142 raeburn 8887: unless ($env{'request.role'} eq $roleid) {
8888: my ($start,$end) = split(/\./,$env{$envkey});
8889: next unless (($now >= $start) && (!$end || $end > $now));
8890: }
1.54 www 8891: my $expiretime=600;
1.620 albertel 8892: if ($env{'request.role'} eq $roleid) {
1.54 www 8893: $expiretime=120;
8894: }
8895: my ($cdom,$cnum,$csec)=split(/\//,$courseid);
8896: my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620 albertel 8897: if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731 albertel 8898: &coursedescription($courseid,{'freshen_cache' => 1});
1.54 www 8899: }
1.620 albertel 8900: if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
8901: || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
8902: if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
8903: &log($env{'user.domain'},$env{'user.name'},
8904: $env{'user.home'},
1.57 www 8905: 'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52 www 8906: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 8907: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 8908: return '';
8909: }
8910: }
1.620 albertel 8911: if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
8912: || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
1.1172.2.142 raeburn 8913: if ($env{$prefix.'priv.'.$priv.'.lock.expire'}>time) {
1.620 albertel 8914: &log($env{'user.domain'},$env{'user.name'},
8915: $env{'user.home'},
1.57 www 8916: 'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52 www 8917: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 8918: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 8919: return '';
8920: }
8921: }
8922: }
1.29 www 8923: }
1.52 www 8924: }
1.1172.2.126 raeburn 8925:
1.52 www 8926: #
8927: # Rest of the restrictions depend on selected course
8928: #
8929:
1.620 albertel 8930: unless ($env{'request.course.id'}) {
1.766 albertel 8931: if ($thisallowed eq 'A') {
8932: return 'A';
1.814 raeburn 8933: } elsif ($thisallowed eq 'B') {
8934: return 'B';
1.766 albertel 8935: } else {
8936: return '1';
8937: }
1.52 www 8938: }
1.29 www 8939:
1.52 www 8940: #
8941: # Now user is definitely in a course
8942: #
1.53 www 8943:
8944:
8945: # Course preferences
8946:
8947: if ($thisallowed=~/C/) {
1.620 albertel 8948: my $rolecode=(split(/\./,$env{'request.role'}))[0];
8949: my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
8950: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479 albertel 8951: =~/\Q$rolecode\E/) {
1.1103 raeburn 8952: if (($priv ne 'pch') && ($priv ne 'plc')) {
1.689 albertel 8953: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
8954: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
8955: $env{'request.course.id'});
8956: }
1.237 www 8957: return '';
8958: }
8959:
1.620 albertel 8960: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479 albertel 8961: =~/\Q$unamedom\E/) {
1.1103 raeburn 8962: if (($priv ne 'pch') && ($priv ne 'plc')) {
1.689 albertel 8963: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
8964: 'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
8965: $env{'request.course.id'});
8966: }
1.54 www 8967: return '';
8968: }
1.53 www 8969: }
8970:
8971: # Resource preferences
8972:
8973: if ($thisallowed=~/R/) {
1.620 albertel 8974: my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479 albertel 8975: if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.1103 raeburn 8976: if (($priv ne 'pch') && ($priv ne 'plc')) {
1.689 albertel 8977: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
8978: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
8979: }
8980: return '';
1.54 www 8981: }
1.53 www 8982: }
1.30 www 8983:
1.1172.2.146. .1(raebu 8984:22): # Restricted for deeplinked session?
8985:22):
8986:22): if ($env{'request.deeplink.login'}) {
8987:22): if ($env{'acc.deeplinkout'} && !$nodeeplinkout) {
8988:22): if (!$symb) { $symb=&symbread($uri,1); }
8989:22): if (($symb) && ($env{'acc.deeplinkout'}=~/\&\Q$symb\E\&/)) {
8990:22): return '';
8991:22): }
8992:22): }
8993:22): }
8994:22):
1.246 www 8995: # Restricted by state or randomout?
1.30 www 8996:
1.52 www 8997: if ($thisallowed=~/X/) {
1.620 albertel 8998: if ($env{'acc.randomout'}) {
1.579 albertel 8999: if (!$symb) { $symb=&symbread($uri,1); }
1.620 albertel 9000: if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) {
1.248 www 9001: return '';
9002: }
1.247 www 9003: }
9004: if (&condval($statecond)) {
1.52 www 9005: return '2';
9006: } else {
9007: return '';
9008: }
9009: }
1.30 www 9010:
1.766 albertel 9011: if ($thisallowed eq 'A') {
9012: return 'A';
1.814 raeburn 9013: } elsif ($thisallowed eq 'B') {
9014: return 'B';
1.1172.2.146. .1(raebu 9015:22): } elsif ($thisallowed eq 'D') {
9016:22): return 'D';
1.766 albertel 9017: }
1.52 www 9018: return 'F';
1.232 www 9019: }
1.1162 raeburn 9020:
1.1172.2.13 raeburn 9021: # ------------------------------------------- Check construction space access
9022:
9023: sub constructaccess {
9024: my ($url,$setpriv)=@_;
9025:
9026: # We do not allow editing of previous versions of files
9027: if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
9028:
9029: # Get username and domain from URL
9030: my ($ownername,$ownerdomain,$ownerhome);
9031:
9032: ($ownerdomain,$ownername) =
1.1172.2.146. .19(raeb 9033:-24): ($url=~ m{^(?:\Q$perlvar{'lonDocRoot'}\E|)(?:/daxepage|/daxeopen)?/priv/($match_domain)/($match_username)(?:/|$)});
1.1172.2.13 raeburn 9034:
9035: # The URL does not really point to any authorspace, forget it
9036: unless (($ownername) && ($ownerdomain)) { return ''; }
9037:
9038: # Now we need to see if the user has access to the authorspace of
9039: # $ownername at $ownerdomain
9040:
9041: if (($ownername eq $env{'user.name'}) && ($ownerdomain eq $env{'user.domain'})) {
9042: # Real author for this?
9043: $ownerhome = $env{'user.home'};
9044: if (exists($env{'user.priv.au./'.$ownerdomain.'/./'})) {
9045: return ($ownername,$ownerdomain,$ownerhome);
9046: }
9047: } else {
9048: # Co-author for this?
9049: if (exists($env{'user.priv.ca./'.$ownerdomain.'/'.$ownername.'./'}) ||
9050: exists($env{'user.priv.aa./'.$ownerdomain.'/'.$ownername.'./'}) ) {
9051: $ownerhome = &homeserver($ownername,$ownerdomain);
9052: return ($ownername,$ownerdomain,$ownerhome);
9053: }
9054: }
9055:
9056: # We don't have any access right now. If we are not possibly going to do anything about this,
9057: # we might as well leave
9058: unless ($setpriv) { return ''; }
9059:
9060: # Backdoor access?
9061: my $allowed=&allowed('eco',$ownerdomain);
9062: # Nope
9063: unless ($allowed) { return ''; }
9064: # Looks like we may have access, but could be locked by the owner of the construction space
9065: if ($allowed eq 'U') {
9066: my %blocked=&get('environment',['domcoord.author'],
9067: $ownerdomain,$ownername);
9068: # Is blocked by owner
9069: if ($blocked{'domcoord.author'} eq 'blocked') { return ''; }
9070: }
9071: if (($allowed eq 'F') || ($allowed eq 'U')) {
9072: # Grant temporary access
9073: my $then=$env{'user.login.time'};
1.1172.2.16 raeburn 9074: my $update=$env{'user.update.time'};
1.1172.2.13 raeburn 9075: if (!$update) { $update = $then; }
9076: my $refresh=$env{'user.refresh.time'};
9077: if (!$refresh) { $refresh = $update; }
9078: my $now = time;
9079: &check_adhoc_privs($ownerdomain,$ownername,$update,$refresh,
9080: $now,'ca','constructaccess');
9081: $ownerhome = &homeserver($ownername,$ownerdomain);
9082: return($ownername,$ownerdomain,$ownerhome);
9083: }
9084: # No business here
9085: return '';
9086: }
9087:
1.1172.2.66 raeburn 9088: # ----------------------------------------------------------- Content Blocking
9089:
9090: {
9091: # Caches for faster Course Contents display where content blocking
9092: # is in operation (i.e., interval param set) for timed quiz.
9093: #
9094: # User for whom data are being temporarily cached.
9095: my $cacheduser='';
1.1172.2.126 raeburn 9096: # Course for which data are being temporarily cached.
9097: my $cachedcid='';
1.1172.2.66 raeburn 9098: # Cached blockers for this user (a hash of blocking items).
9099: my %cachedblockers=();
9100: # When the data were last cached.
9101: my $cachedlast='';
9102:
9103: sub load_all_blockers {
1.1172.2.128 raeburn 9104: my ($uname,$udom)=@_;
1.1172.2.66 raeburn 9105: if (($uname ne '') && ($udom ne '')) {
9106: if (($cacheduser eq $uname.':'.$udom) &&
1.1172.2.126 raeburn 9107: ($cachedcid eq $env{'request.course.id'}) &&
1.1172.2.128 raeburn 9108: (abs($cachedlast-time)<5)) {
1.1172.2.66 raeburn 9109: return;
9110: }
9111: }
9112: $cachedlast=time;
9113: $cacheduser=$uname.':'.$udom;
1.1172.2.126 raeburn 9114: $cachedcid=$env{'request.course.id'};
1.1172.2.128 raeburn 9115: %cachedblockers = &get_commblock_resources();
1.1172.2.126 raeburn 9116: return;
1.1172.2.66 raeburn 9117: }
9118:
1.1162 raeburn 9119: sub get_comm_blocks {
9120: my ($cdom,$cnum) = @_;
9121: if ($cdom eq '' || $cnum eq '') {
9122: return unless ($env{'request.course.id'});
9123: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
9124: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
9125: }
9126: my %commblocks;
9127: my $hashid=$cdom.'_'.$cnum;
9128: my ($blocksref,$cached)=&is_cached_new('comm_block',$hashid);
9129: if ((defined($cached)) && (ref($blocksref) eq 'HASH')) {
9130: %commblocks = %{$blocksref};
9131: } else {
9132: %commblocks = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
9133: my $cachetime = 600;
9134: &do_cache_new('comm_block',$hashid,\%commblocks,$cachetime);
9135: }
9136: return %commblocks;
9137: }
9138:
1.1172.2.66 raeburn 9139: sub get_commblock_resources {
9140: my ($blocks) = @_;
9141: my %blockers = ();
9142: return %blockers unless ($env{'request.course.id'});
1.1172.2.142 raeburn 9143: my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
9144: if ($env{'request.course.sec'}) {
9145: $courseurl .= '/'.$env{'request.course.sec'};
9146: }
9147: return %blockers if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseurl} =~/evb\&([^\:]*)/);
1.1162 raeburn 9148: my %commblocks;
9149: if (ref($blocks) eq 'HASH') {
9150: %commblocks = %{$blocks};
9151: } else {
9152: %commblocks = &get_comm_blocks();
9153: }
1.1172.2.66 raeburn 9154: return %blockers unless (keys(%commblocks) > 0);
1.1163 raeburn 9155: my $navmap = Apache::lonnavmaps::navmap->new();
1.1172.2.66 raeburn 9156: return %blockers unless (ref($navmap));
9157: my $now = time;
1.1162 raeburn 9158: foreach my $block (keys(%commblocks)) {
9159: if ($block =~ /^(\d+)____(\d+)$/) {
9160: my ($start,$end) = ($1,$2);
9161: if ($start <= $now && $end >= $now) {
9162: if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
9163: if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
9164: if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
1.1172.2.66 raeburn 9165: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
9166: $blockers{$block}{maps} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
1.1162 raeburn 9167: }
9168: }
9169: if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
1.1172.2.66 raeburn 9170: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
9171: $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
1.1162 raeburn 9172: }
9173: }
9174: }
9175: }
9176: }
9177: } elsif ($block =~ /^firstaccess____(.+)$/) {
9178: my $item = $1;
9179: if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
9180: if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
1.1172.2.142 raeburn 9181: my (@interval,$mapname);
1.1172.2.66 raeburn 9182: my $type = 'map';
9183: if ($item eq 'course') {
9184: $type = 'course';
9185: @interval=&EXT("resource.0.interval");
9186: } else {
9187: if ($item =~ /___\d+___/) {
9188: $type = 'resource';
9189: @interval=&EXT("resource.0.interval",$item);
1.1162 raeburn 9190: } else {
1.1172.2.142 raeburn 9191: $mapname = &deversion($item);
9192: if (ref($navmap)) {
9193: my $timelimit = $navmap->get_mapparam(undef,$mapname,'0.interval');
9194: @interval = ($timelimit,'map');
1.1162 raeburn 9195: }
9196: }
1.1172.2.66 raeburn 9197: }
1.1172.2.146. .1(raebu 9198:22): if ($interval[0] =~ /^(\d+)/) {
9199:22): my $timelimit = $1;
1.1172.2.66 raeburn 9200: my $first_access;
9201: if ($type eq 'resource') {
9202: $first_access=&get_first_access($interval[1],$item);
9203: } elsif ($type eq 'map') {
9204: $first_access=&get_first_access($interval[1],undef,$item);
9205: } else {
9206: $first_access=&get_first_access($interval[1]);
9207: }
9208: if ($first_access) {
1.1172.2.146. .1(raebu 9209:22): my $timesup = $first_access+$timelimit;
1.1172.2.66 raeburn 9210: if ($timesup > $now) {
9211: my $activeblock;
1.1172.2.142 raeburn 9212: if ($type eq 'resource') {
9213: if (ref($navmap)) {
9214: my $res = $navmap->getBySymb($item);
9215: if ($res->answerable()) {
9216: $activeblock = 1;
9217: }
9218: }
9219: } elsif ($type eq 'map') {
9220: my $mapsymb = &symbread($mapname,1);
9221: if (($mapsymb) && (ref($navmap))) {
9222: my $mapres = $navmap->getBySymb($mapsymb);
9223: if (ref($mapres)) {
9224: my $first = $mapres->map_start();
9225: my $finish = $mapres->map_finish();
9226: my $it = $navmap->getIterator($first,$finish,undef,0,0);
9227: if (ref($it)) {
9228: my $res;
9229: while ($res = $it->next(undef,1)) {
9230: next unless (ref($res));
9231: my $symb = $res->symb();
9232: next if (($symb eq $mapsymb) || ($symb eq ''));
9233: @interval=&EXT("resource.0.interval",$symb);
9234: if ($interval[1] eq 'map') {
9235: if ($res->answerable()) {
9236: $activeblock = 1;
9237: last;
9238: }
9239: }
9240: }
9241: }
9242: }
1.1172.2.66 raeburn 9243: }
9244: }
9245: if ($activeblock) {
9246: if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
9247: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
9248: $blockers{$block}{'maps'} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
9249: }
9250: }
9251: if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
9252: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
9253: $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
1.1163 raeburn 9254: }
1.1162 raeburn 9255: }
9256: }
9257: }
9258: }
9259: }
9260: }
9261: }
9262: }
9263: }
1.1172.2.66 raeburn 9264: return %blockers;
1.1162 raeburn 9265: }
9266:
1.1172.2.66 raeburn 9267: sub has_comm_blocking {
1.1172.2.128 raeburn 9268: my ($priv,$symb,$uri,$ignoresymbdb,$noenccheck,$blocked,$blocks) = @_;
1.1172.2.66 raeburn 9269: my @blockers;
9270: return unless ($env{'request.course.id'});
9271: return unless ($priv eq 'bre');
9272: return if ($env{'request.state'} eq 'construct');
1.1172.2.142 raeburn 9273: my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
9274: if ($env{'request.course.sec'}) {
9275: $courseurl .= '/'.$env{'request.course.sec'};
9276: }
9277: return if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseurl} =~/evb\&([^\:]*)/);
1.1172.2.128 raeburn 9278: my %blockinfo;
9279: if (ref($blocks) eq 'HASH') {
9280: %blockinfo = &get_commblock_resources($blocks);
9281: } else {
9282: &load_all_blockers($env{'user.name'},$env{'user.domain'});
9283: %blockinfo = %cachedblockers;
9284: }
9285: return unless (keys(%blockinfo) > 0);
1.1172.2.66 raeburn 9286: my (%possibles,@symbs);
9287: if (!$symb) {
1.1172.2.128 raeburn 9288: $symb = &symbread($uri,1,1,1,\%possibles,$ignoresymbdb,$noenccheck);
1.1162 raeburn 9289: }
1.1172.2.66 raeburn 9290: if ($symb) {
9291: @symbs = ($symb);
9292: } elsif (keys(%possibles)) {
9293: @symbs = keys(%possibles);
9294: }
9295: my $noblock;
9296: foreach my $symb (@symbs) {
9297: last if ($noblock);
9298: my ($map,$resid,$resurl)=&decode_symb($symb);
1.1172.2.128 raeburn 9299: foreach my $block (keys(%blockinfo)) {
1.1172.2.66 raeburn 9300: if ($block =~ /^firstaccess____(.+)$/) {
9301: my $item = $1;
1.1172.2.126 raeburn 9302: unless ($blocked) {
9303: if (($item eq $map) || ($item eq $symb)) {
9304: $noblock = 1;
9305: last;
9306: }
1.1172.2.66 raeburn 9307: }
1.1162 raeburn 9308: }
1.1172.2.128 raeburn 9309: if (ref($blockinfo{$block}) eq 'HASH') {
9310: if (ref($blockinfo{$block}{'resources'}) eq 'HASH') {
9311: if ($blockinfo{$block}{'resources'}{$symb}) {
1.1172.2.66 raeburn 9312: unless (grep(/^\Q$block\E$/,@blockers)) {
9313: push(@blockers,$block);
9314: }
9315: }
9316: }
1.1172.2.128 raeburn 9317: if (ref($blockinfo{$block}{'maps'}) eq 'HASH') {
9318: if ($blockinfo{$block}{'maps'}{$map}) {
1.1172.2.126 raeburn 9319: unless (grep(/^\Q$block\E$/,@blockers)) {
9320: push(@blockers,$block);
9321: }
1.1172.2.66 raeburn 9322: }
9323: }
1.1162 raeburn 9324: }
9325: }
9326: }
1.1172.2.126 raeburn 9327: unless ($noblock) {
9328: return @blockers;
9329: }
9330: return;
1.1172.2.66 raeburn 9331: }
1.1162 raeburn 9332: }
9333:
1.1172.2.146. .1(raebu 9334:22): sub deeplink_check {
9335:22): my ($priv,$symb,$uri) = @_;
9336:22): return unless ($env{'request.course.id'});
9337:22): return unless ($priv eq 'bre');
9338:22): return if ($env{'request.state'} eq 'construct');
9339:22): return if ($env{'request.role.adv'});
9340:22): my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
9341:22): my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
9342:22): my (%possibles,@symbs);
9343:22): if (!$symb) {
9344:22): $symb = &symbread($uri,1,1,1,\%possibles);
9345:22): }
9346:22): if ($symb) {
9347:22): @symbs = ($symb);
9348:22): } elsif (keys(%possibles)) {
9349:22): @symbs = keys(%possibles);
9350:22): }
9351:22):
9352:22): my ($deeplink_symb,$allow);
9353:22): if ($env{'request.deeplink.login'}) {
9354:22): $deeplink_symb = &Apache::loncommon::deeplink_login_symb($cnum,$cdom);
9355:22): }
9356:22): foreach my $symb (@symbs) {
9357:22): last if ($allow);
9358:22): my $deeplink = &EXT("resource.0.deeplink",$symb);
9359:22): if ($deeplink eq '') {
9360:22): $allow = 1;
9361:22): } else {
9362:22): my ($state,$others,$listed,$scope,$protect) = split(/,/,$deeplink);
9363:22): if ($state ne 'only') {
9364:22): $allow = 1;
9365:22): } else {
9366:22): my $check_deeplink_entry;
9367:22): if ($protect ne 'none') {
9368:22): my ($acctype,$item) = split(/:/,$protect);
9369:22): if (($acctype eq 'ltic') && ($env{'user.linkprotector'})) {
9370:22): if (grep(/^\Q$item\Ec$/,split(/,/,$env{'user.linkprotector'}))) {
9371:22): $check_deeplink_entry = 1
9372:22): }
9373:22): } elsif (($acctype eq 'ltid') && ($env{'user.linkprotector'})) {
9374:22): if (grep(/^\Q$item\Ed$/,split(/,/,$env{'user.linkprotector'}))) {
9375:22): $check_deeplink_entry = 1;
9376:22): }
9377:22): } elsif (($acctype eq 'key') && ($env{'user.deeplinkkey'})) {
9378:22): if (grep(/^\Q$item\E$/,split(/,/,$env{'user.deeplinkkey'}))) {
9379:22): $check_deeplink_entry = 1;
9380:22): }
9381:22): }
9382:22): }
9383:22): if (($protect eq 'none') || ($check_deeplink_entry)) {
9384:22): if ($scope eq 'res') {
9385:22): if ($symb eq $deeplink_symb) {
9386:22): $allow = 1;
9387:22): }
9388:22): } elsif (($scope eq 'map') || ($scope eq 'rec')) {
9389:22): my ($map_from_symb,$map_from_login);
9390:22): $map_from_symb = &deversion((&decode_symb($symb))[0]);
9391:22): if ($deeplink_symb =~ /\.(page|sequence)$/) {
9392:22): $map_from_login = &deversion((&decode_symb($deeplink_symb))[2]);
9393:22): } else {
9394:22): $map_from_login = &deversion((&decode_symb($deeplink_symb))[0]);
9395:22): }
9396:22): if (($map_from_symb) && ($map_from_login)) {
9397:22): if ($map_from_symb eq $map_from_login) {
9398:22): $allow = 1;
9399:22): } elsif ($scope eq 'rec') {
9400:22): my @recurseup = &get_map_hierarchy($map_from_symb,$env{'request.course.id'});
9401:22): if (grep(/^\Q$map_from_login\E$/,@recurseup)) {
9402:22): $allow = 1;
9403:22): }
9404:22): }
9405:22): }
9406:22): }
9407:22): }
9408:22): }
9409:22): }
9410:22): }
9411:22): return if ($allow);
9412:22): return 1;
9413:22): }
9414:22):
1.1172.2.66 raeburn 9415: # -------------------------------- Deversion and split uri into path an filename
9416:
1.1133 foxr 9417: #
1.1172.2.66 raeburn 9418: # Removes the version from a URI and
1.1133 foxr 9419: # splits it in to its filename and path to the filename.
9420: # Seems like File::Basename could have done this more clearly.
9421: # Parameters:
9422: # $uri - input URI
9423: # Returns:
9424: # Two element list consisting of
9425: # $pathname - the URI up to and excluding the trailing /
9426: # $filename - The part of the URI following the last /
9427: # NOTE:
9428: # Another realization of this is simply:
9429: # use File::Basename;
9430: # ...
9431: # $uri = shift;
9432: # $filename = basename($uri);
9433: # $path = dirname($uri);
9434: # return ($filename, $path);
9435: #
9436: # The implementation below is probably faster however.
9437: #
1.710 albertel 9438: sub split_uri_for_cond {
9439: my $uri=&deversion(&declutter(shift));
9440: my @uriparts=split(/\//,$uri);
9441: my $filename=pop(@uriparts);
9442: my $pathname=join('/',@uriparts);
9443: return ($pathname,$filename);
9444: }
1.232 www 9445: # --------------------------------------------------- Is a resource on the map?
9446:
9447: sub is_on_map {
1.710 albertel 9448: my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289 bowersj2 9449: #Trying to find the conditional for the file
1.620 albertel 9450: my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289 bowersj2 9451: /\&\Q$filename\E\:([\d\|]+)\&/);
1.232 www 9452: if ($match) {
1.289 bowersj2 9453: return (1,$1);
9454: } else {
1.434 www 9455: return (0,0);
1.289 bowersj2 9456: }
1.12 www 9457: }
9458:
1.427 www 9459: # --------------------------------------------------------- Get symb from alias
9460:
9461: sub get_symb_from_alias {
9462: my $symb=shift;
9463: my ($map,$resid,$url)=&decode_symb($symb);
9464: # Already is a symb
9465: if ($url) { return $symb; }
9466: # Must be an alias
9467: my $aliassymb='';
9468: my %bighash;
1.620 albertel 9469: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427 www 9470: &GDBM_READER(),0640)) {
9471: my $rid=$bighash{'mapalias_'.$symb};
9472: if ($rid) {
9473: my ($mapid,$resid)=split(/\./,$rid);
1.429 albertel 9474: $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
9475: $resid,$bighash{'src_'.$rid});
1.427 www 9476: }
9477: untie %bighash;
9478: }
9479: return $aliassymb;
9480: }
9481:
1.12 www 9482: # ----------------------------------------------------------------- Define Role
9483:
9484: sub definerole {
9485: if (allowed('mcr','/')) {
1.1172.2.84 raeburn 9486: my ($rolename,$sysrole,$domrole,$courole,$uname,$udom)=@_;
1.800 albertel 9487: foreach my $role (split(':',$sysrole)) {
9488: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 9489: if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
9490: if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
9491: if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 9492: return "refused:s:$crole&$cqual";
9493: }
9494: }
1.191 harris41 9495: }
1.800 albertel 9496: foreach my $role (split(':',$domrole)) {
9497: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 9498: if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
9499: if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
9500: if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) {
1.21 www 9501: return "refused:d:$crole&$cqual";
9502: }
9503: }
1.191 harris41 9504: }
1.800 albertel 9505: foreach my $role (split(':',$courole)) {
9506: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 9507: if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
9508: if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
9509: if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 9510: return "refused:c:$crole&$cqual";
9511: }
9512: }
1.191 harris41 9513: }
1.1172.2.84 raeburn 9514: my $uhome;
9515: if (($uname ne '') && ($udom ne '')) {
9516: $uhome = &homeserver($uname,$udom);
9517: return $uhome if ($uhome eq 'no_host');
9518: } else {
9519: $uname = $env{'user.name'};
9520: $udom = $env{'user.domain'};
9521: $uhome = $env{'user.home'};
9522: }
1.620 albertel 9523: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.1172.2.84 raeburn 9524: "$udom:$uname:rolesdef_$rolename=".
1.21 www 9525: escape($sysrole.'_'.$domrole.'_'.$courole);
1.1172.2.84 raeburn 9526: return reply($command,$uhome);
1.12 www 9527: } else {
9528: return 'refused';
9529: }
1.105 harris41 9530: }
9531:
9532: # ---------------- Make a metadata query against the network of library servers
9533:
9534: sub metadata_query {
1.1172.2.33 raeburn 9535: my ($query,$custom,$customshow,$server_array,$domains_hash)=@_;
1.120 harris41 9536: my %rhash;
1.845 albertel 9537: my %libserv = &all_library();
1.244 matthew 9538: my @server_list = (defined($server_array) ? @$server_array
9539: : keys(%libserv) );
9540: for my $server (@server_list) {
1.1172.2.33 raeburn 9541: my $domains = '';
9542: if (ref($domains_hash) eq 'HASH') {
9543: $domains = $domains_hash->{$server};
9544: }
1.118 harris41 9545: unless ($custom or $customshow) {
1.1172.2.33 raeburn 9546: my $reply=&reply("querysend:".&escape($query).':::'.&escape($domains),$server);
1.118 harris41 9547: $rhash{$server}=$reply;
9548: }
9549: else {
9550: my $reply=&reply("querysend:".&escape($query).':'.
1.1172.2.33 raeburn 9551: &escape($custom).':'.&escape($customshow).':'.&escape($domains),
1.118 harris41 9552: $server);
9553: $rhash{$server}=$reply;
9554: }
1.112 harris41 9555: }
1.118 harris41 9556: return \%rhash;
1.240 www 9557: }
9558:
9559: # ----------------------------------------- Send log queries and wait for reply
9560:
9561: sub log_query {
9562: my ($uname,$udom,$query,%filters)=@_;
9563: my $uhome=&homeserver($uname,$udom);
9564: if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838 albertel 9565: my $uhost=&hostname($uhome);
1.800 albertel 9566: my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240 www 9567: my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
9568: $uhome);
1.479 albertel 9569: unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242 www 9570: return get_query_reply($queryid);
9571: }
9572:
1.818 raeburn 9573: # -------------------------- Update MySQL table for portfolio file
9574:
9575: sub update_portfolio_table {
1.821 raeburn 9576: my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.970 raeburn 9577: if ($group ne '') {
9578: $file_name =~s /^\Q$group\E//;
9579: }
1.818 raeburn 9580: my $homeserver = &homeserver($uname,$udom);
9581: my $queryid=
1.821 raeburn 9582: &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
9583: ':'.&escape($file_name).':'.$action,$homeserver);
1.818 raeburn 9584: my $reply = &get_query_reply($queryid);
9585: return $reply;
9586: }
9587:
1.899 raeburn 9588: # -------------------------- Update MySQL allusers table
9589:
9590: sub update_allusers_table {
9591: my ($uname,$udom,$names) = @_;
9592: my $homeserver = &homeserver($uname,$udom);
9593: my $queryid=
9594: &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
9595: 'lastname='.&escape($names->{'lastname'}).'%%'.
9596: 'firstname='.&escape($names->{'firstname'}).'%%'.
9597: 'middlename='.&escape($names->{'middlename'}).'%%'.
9598: 'generation='.&escape($names->{'generation'}).'%%'.
9599: 'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
9600: 'id='.&escape($names->{'id'}),$homeserver);
1.1075 raeburn 9601: return;
1.899 raeburn 9602: }
9603:
1.508 raeburn 9604: # ------- Request retrieval of institutional classlists for course(s)
1.506 raeburn 9605:
9606: sub fetch_enrollment_query {
1.511 raeburn 9607: my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.1172.2.79 raeburn 9608: my ($homeserver,$sleep,$loopmax);
1.547 raeburn 9609: my $maxtries = 1;
1.508 raeburn 9610: if ($context eq 'automated') {
9611: $homeserver = $perlvar{'lonHostID'};
1.1172.2.79 raeburn 9612: $sleep = 2;
9613: $loopmax = 100;
1.547 raeburn 9614: $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508 raeburn 9615: } else {
9616: $homeserver = &homeserver($cnum,$dom);
9617: }
1.838 albertel 9618: my $host=&hostname($homeserver);
1.506 raeburn 9619: my $cmd = '';
1.1000 raeburn 9620: foreach my $affiliate (keys(%{$affiliatesref})) {
1.800 albertel 9621: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506 raeburn 9622: }
9623: $cmd =~ s/%%$//;
9624: $cmd = &escape($cmd);
9625: my $query = 'fetchenrollment';
1.620 albertel 9626: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526 raeburn 9627: unless ($queryid=~/^\Q$host\E\_/) {
9628: &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum);
9629: return 'error: '.$queryid;
9630: }
1.1172.2.93 raeburn 9631: my $reply = &get_query_reply($queryid,$sleep,$loopmax);
1.547 raeburn 9632: my $tries = 1;
9633: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
1.1172.2.79 raeburn 9634: $reply = &get_query_reply($queryid,$sleep,$loopmax);
1.547 raeburn 9635: $tries ++;
9636: }
1.526 raeburn 9637: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620 albertel 9638: &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526 raeburn 9639: } else {
1.901 albertel 9640: my @responses = split(/:/,$reply);
1.1172.2.82 raeburn 9641: if (grep { $_ eq $homeserver } ¤t_machine_ids()) {
1.800 albertel 9642: foreach my $line (@responses) {
9643: my ($key,$value) = split(/=/,$line,2);
1.515 raeburn 9644: $$replyref{$key} = $value;
9645: }
9646: } else {
1.1117 foxr 9647: my $pathname = LONCAPA::tempdir();
1.800 albertel 9648: foreach my $line (@responses) {
9649: my ($key,$value) = split(/=/,$line);
1.506 raeburn 9650: $$replyref{$key} = $value;
9651: if ($value > 0) {
1.800 albertel 9652: foreach my $item (@{$$affiliatesref{$key}}) {
9653: my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506 raeburn 9654: my $destname = $pathname.'/'.$filename;
9655: my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526 raeburn 9656: if ($xml_classlist =~ /^error/) {
9657: &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
9658: } else {
1.1172.2.96 raeburn 9659: if ( open(FILE,">",$destname) ) {
1.506 raeburn 9660: print FILE &unescape($xml_classlist);
9661: close(FILE);
1.526 raeburn 9662: } else {
9663: &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506 raeburn 9664: }
9665: }
9666: }
9667: }
9668: }
9669: }
9670: return 'ok';
9671: }
9672: return 'error';
9673: }
9674:
1.242 www 9675: sub get_query_reply {
1.1172.2.79 raeburn 9676: my ($queryid,$sleep,$loopmax) = @_;
9677: if (($sleep eq '') || ($sleep !~ /^\d+\.?\d*$/)) {
9678: $sleep = 0.2;
9679: }
9680: if (($loopmax eq '') || ($loopmax =~ /\D/)) {
9681: $loopmax = 100;
9682: }
1.1117 foxr 9683: my $replyfile=LONCAPA::tempdir().$queryid;
1.240 www 9684: my $reply='';
1.1172.2.79 raeburn 9685: for (1..$loopmax) {
9686: sleep($sleep);
1.240 www 9687: if (-e $replyfile.'.end') {
1.1172.2.96 raeburn 9688: if (open(my $fh,"<",$replyfile)) {
1.904 albertel 9689: $reply = join('',<$fh>);
9690: close($fh);
1.240 www 9691: } else { return 'error: reply_file_error'; }
1.242 www 9692: return &unescape($reply);
9693: }
1.240 www 9694: }
1.242 www 9695: return 'timeout:'.$queryid;
1.240 www 9696: }
9697:
9698: sub courselog_query {
1.241 www 9699: #
9700: # possible filters:
9701: # url: url or symb
9702: # username
9703: # domain
9704: # action: view, submit, grade
9705: # start: timestamp
9706: # end: timestamp
9707: #
1.240 www 9708: my (%filters)=@_;
1.620 albertel 9709: unless ($env{'request.course.id'}) { return 'no_course'; }
1.241 www 9710: if ($filters{'url'}) {
9711: $filters{'url'}=&symbclean(&declutter($filters{'url'}));
9712: $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
9713: $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
9714: }
1.620 albertel 9715: my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
9716: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240 www 9717: return &log_query($cname,$cdom,'courselog',%filters);
9718: }
9719:
9720: sub userlog_query {
1.858 raeburn 9721: #
9722: # possible filters:
9723: # action: log check role
9724: # start: timestamp
9725: # end: timestamp
9726: #
1.240 www 9727: my ($uname,$udom,%filters)=@_;
9728: return &log_query($uname,$udom,'userlog',%filters);
1.12 www 9729: }
9730:
1.506 raeburn 9731: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course
9732:
9733: sub auto_run {
1.508 raeburn 9734: my ($cnum,$cdom) = @_;
1.876 raeburn 9735: my $response = 0;
9736: my $settings;
9737: my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
9738: if (ref($domconfig{'autoenroll'}) eq 'HASH') {
9739: $settings = $domconfig{'autoenroll'};
9740: if ($settings->{'run'} eq '1') {
9741: $response = 1;
9742: }
9743: } else {
1.934 raeburn 9744: my $homeserver;
9745: if (&is_course($cdom,$cnum)) {
9746: $homeserver = &homeserver($cnum,$cdom);
9747: } else {
9748: $homeserver = &domain($cdom,'primary');
9749: }
9750: if ($homeserver ne 'no_host') {
9751: $response = &reply('autorun:'.$cdom,$homeserver);
9752: }
1.876 raeburn 9753: }
1.506 raeburn 9754: return $response;
9755: }
1.776 albertel 9756:
1.506 raeburn 9757: sub auto_get_sections {
1.508 raeburn 9758: my ($cnum,$cdom,$inst_coursecode) = @_;
1.1007 raeburn 9759: my $homeserver;
9760: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
9761: $homeserver = &homeserver($cnum,$cdom);
9762: }
9763: if (!defined($homeserver)) {
9764: if ($cdom =~ /^$match_domain$/) {
9765: $homeserver = &domain($cdom,'primary');
9766: }
9767: }
9768: my @secs;
9769: if (defined($homeserver)) {
9770: my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
9771: unless ($response eq 'refused') {
9772: @secs = split(/:/,$response);
9773: }
1.506 raeburn 9774: }
9775: return @secs;
9776: }
1.776 albertel 9777:
1.506 raeburn 9778: sub auto_new_course {
1.1099 raeburn 9779: my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
1.508 raeburn 9780: my $homeserver = &homeserver($cnum,$cdom);
1.1099 raeburn 9781: my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
1.506 raeburn 9782: return $response;
9783: }
1.776 albertel 9784:
1.506 raeburn 9785: sub auto_validate_courseID {
1.508 raeburn 9786: my ($cnum,$cdom,$inst_course_id) = @_;
9787: my $homeserver = &homeserver($cnum,$cdom);
1.511 raeburn 9788: my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506 raeburn 9789: return $response;
9790: }
1.776 albertel 9791:
1.1007 raeburn 9792: sub auto_validate_instcode {
1.1020 raeburn 9793: my ($cnum,$cdom,$instcode,$owner) = @_;
1.1007 raeburn 9794: my ($homeserver,$response);
9795: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
9796: $homeserver = &homeserver($cnum,$cdom);
9797: }
9798: if (!defined($homeserver)) {
9799: if ($cdom =~ /^$match_domain$/) {
9800: $homeserver = &domain($cdom,'primary');
9801: }
9802: }
1.1065 raeburn 9803: $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
9804: &escape($instcode).':'.&escape($owner),$homeserver));
1.1172.2.19 raeburn 9805: my ($outcome,$description,$defaultcredits) = map { &unescape($_); } split('&',$response,3);
9806: return ($outcome,$description,$defaultcredits);
1.1007 raeburn 9807: }
9808:
1.1172.2.141 raeburn 9809: sub auto_validate_inst_crosslist {
9810: my ($cnum,$cdom,$instcode,$inst_xlist,$coowner) = @_;
9811: my ($homeserver,$response);
9812: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
9813: $homeserver = &homeserver($cnum,$cdom);
9814: }
9815: if (!defined($homeserver)) {
9816: if ($cdom =~ /^$match_domain$/) {
9817: $homeserver = &domain($cdom,'primary');
9818: }
9819: }
9820: unless (($homeserver eq '') || ($homeserver eq 'no_host')) {
9821: $response=&reply('autovalidateinstcrosslist:'.$cdom.':'.
9822: &escape($instcode).':'.&escape($inst_xlist).':'.
9823: &escape($coowner),$homeserver);
9824: }
9825: return $response;
9826: }
9827:
1.506 raeburn 9828: sub auto_create_password {
1.873 raeburn 9829: my ($cnum,$cdom,$authparam,$udom) = @_;
9830: my ($homeserver,$response);
1.506 raeburn 9831: my $create_passwd = 0;
9832: my $authchk = '';
1.873 raeburn 9833: if ($udom =~ /^$match_domain$/) {
9834: $homeserver = &domain($udom,'primary');
9835: }
9836: if ($homeserver eq '') {
9837: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
9838: $homeserver = &homeserver($cnum,$cdom);
9839: }
9840: }
9841: if ($homeserver eq '') {
9842: $authchk = 'nodomain';
1.506 raeburn 9843: } else {
1.873 raeburn 9844: $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
9845: if ($response eq 'refused') {
9846: $authchk = 'refused';
9847: } else {
1.901 albertel 9848: ($authparam,$create_passwd,$authchk) = split(/:/,$response);
1.873 raeburn 9849: }
1.506 raeburn 9850: }
9851: return ($authparam,$create_passwd,$authchk);
9852: }
9853:
1.706 raeburn 9854: sub auto_photo_permission {
9855: my ($cnum,$cdom,$students) = @_;
9856: my $homeserver = &homeserver($cnum,$cdom);
1.707 albertel 9857: my ($outcome,$perm_reqd,$conditions) =
9858: split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709 albertel 9859: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
9860: return (undef,undef);
9861: }
1.706 raeburn 9862: return ($outcome,$perm_reqd,$conditions);
9863: }
9864:
9865: sub auto_checkphotos {
9866: my ($uname,$udom,$pid) = @_;
9867: my $homeserver = &homeserver($uname,$udom);
9868: my ($result,$resulttype);
9869: my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707 albertel 9870: &escape($uname).':'.&escape($pid),
9871: $homeserver));
1.709 albertel 9872: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
9873: return (undef,undef);
9874: }
1.706 raeburn 9875: if ($outcome) {
9876: ($result,$resulttype) = split(/:/,$outcome);
9877: }
9878: return ($result,$resulttype);
9879: }
9880:
9881: sub auto_photochoice {
9882: my ($cnum,$cdom) = @_;
9883: my $homeserver = &homeserver($cnum,$cdom);
9884: my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707 albertel 9885: &escape($cdom),
9886: $homeserver)));
1.709 albertel 9887: if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
9888: return (undef,undef);
9889: }
1.706 raeburn 9890: return ($update,$comment);
9891: }
9892:
9893: sub auto_photoupdate {
9894: my ($affiliatesref,$dom,$cnum,$photo) = @_;
9895: my $homeserver = &homeserver($cnum,$dom);
1.838 albertel 9896: my $host=&hostname($homeserver);
1.706 raeburn 9897: my $cmd = '';
9898: my $maxtries = 1;
1.800 albertel 9899: foreach my $affiliate (keys(%{$affiliatesref})) {
9900: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706 raeburn 9901: }
9902: $cmd =~ s/%%$//;
9903: $cmd = &escape($cmd);
9904: my $query = 'institutionalphotos';
9905: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
9906: unless ($queryid=~/^\Q$host\E\_/) {
9907: &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
9908: return 'error: '.$queryid;
9909: }
9910: my $reply = &get_query_reply($queryid);
9911: my $tries = 1;
9912: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
9913: $reply = &get_query_reply($queryid);
9914: $tries ++;
9915: }
9916: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
9917: &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
9918: } else {
9919: my @responses = split(/:/,$reply);
9920: my $outcome = shift(@responses);
9921: foreach my $item (@responses) {
9922: my ($key,$value) = split(/=/,$item);
9923: $$photo{$key} = $value;
9924: }
9925: return $outcome;
9926: }
9927: return 'error';
9928: }
9929:
1.521 raeburn 9930: sub auto_instcode_format {
1.793 albertel 9931: my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
9932: $cat_order) = @_;
1.521 raeburn 9933: my $courses = '';
1.772 raeburn 9934: my @homeservers;
1.521 raeburn 9935: if ($caller eq 'global') {
1.841 albertel 9936: my %servers = &get_servers($codedom,'library');
9937: foreach my $tryserver (keys(%servers)) {
9938: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
9939: push(@homeservers,$tryserver);
9940: }
1.584 raeburn 9941: }
1.1022 raeburn 9942: } elsif ($caller eq 'requests') {
9943: if ($codedom =~ /^$match_domain$/) {
9944: my $chome = &domain($codedom,'primary');
9945: unless ($chome eq 'no_host') {
9946: push(@homeservers,$chome);
9947: }
9948: }
1.521 raeburn 9949: } else {
1.772 raeburn 9950: push(@homeservers,&homeserver($caller,$codedom));
1.521 raeburn 9951: }
1.793 albertel 9952: foreach my $code (keys(%{$instcodes})) {
9953: $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521 raeburn 9954: }
9955: chop($courses);
1.772 raeburn 9956: my $ok_response = 0;
9957: my $response;
9958: while (@homeservers > 0 && $ok_response == 0) {
9959: my $server = shift(@homeservers);
9960: $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
9961: if ($response !~ /(con_lost|error|no_such_host|refused)/) {
9962: my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) =
1.901 albertel 9963: split(/:/,$response);
1.772 raeburn 9964: %{$codes} = (%{$codes},&str2hash($codes_str));
9965: push(@{$codetitles},&str2array($codetitles_str));
9966: %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
9967: %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
9968: $ok_response = 1;
9969: }
9970: }
9971: if ($ok_response) {
1.521 raeburn 9972: return 'ok';
1.772 raeburn 9973: } else {
9974: return $response;
1.521 raeburn 9975: }
9976: }
9977:
1.792 raeburn 9978: sub auto_instcode_defaults {
9979: my ($domain,$returnhash,$code_order) = @_;
9980: my @homeservers;
1.841 albertel 9981:
9982: my %servers = &get_servers($domain,'library');
9983: foreach my $tryserver (keys(%servers)) {
9984: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
9985: push(@homeservers,$tryserver);
9986: }
1.792 raeburn 9987: }
1.841 albertel 9988:
1.792 raeburn 9989: my $response;
1.841 albertel 9990: foreach my $server (@homeservers) {
1.792 raeburn 9991: $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841 albertel 9992: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
9993:
9994: foreach my $pair (split(/\&/,$response)) {
9995: my ($name,$value)=split(/\=/,$pair);
9996: if ($name eq 'code_order') {
9997: @{$code_order} = split(/\&/,&unescape($value));
9998: } else {
9999: $returnhash->{&unescape($name)}=&unescape($value);
10000: }
10001: }
10002: return 'ok';
1.792 raeburn 10003: }
1.841 albertel 10004:
10005: return $response;
1.1003 raeburn 10006: }
10007:
10008: sub auto_possible_instcodes {
1.1007 raeburn 10009: my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
10010: unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') &&
10011: (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
10012: return;
10013: }
1.1003 raeburn 10014: my (@homeservers,$uhome);
10015: if (defined(&domain($domain,'primary'))) {
10016: $uhome=&domain($domain,'primary');
10017: push(@homeservers,&domain($domain,'primary'));
10018: } else {
10019: my %servers = &get_servers($domain,'library');
10020: foreach my $tryserver (keys(%servers)) {
10021: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
10022: push(@homeservers,$tryserver);
10023: }
10024: }
10025: }
10026: my $response;
10027: foreach my $server (@homeservers) {
10028: $response=&reply('autopossibleinstcodes:'.$domain,$server);
10029: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
1.1007 raeburn 10030: my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) =
10031: split(':',$response);
10032: @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
10033: @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
1.1003 raeburn 10034: foreach my $item (split('&',$cat_title)) {
1.1005 raeburn 10035: my ($name,$value)=split('=',$item);
10036: $cat_titles->{&unescape($name)}=&thaw_unescape($value);
1.1003 raeburn 10037: }
10038: foreach my $item (split('&',$cat_order)) {
1.1005 raeburn 10039: my ($name,$value)=split('=',$item);
10040: $cat_orders->{&unescape($name)}=&thaw_unescape($value);
1.1003 raeburn 10041: }
10042: return 'ok';
10043: }
10044: return $response;
10045: }
1.792 raeburn 10046:
1.1010 raeburn 10047: sub auto_courserequest_checks {
10048: my ($dom) = @_;
1.1020 raeburn 10049: my ($homeserver,%validations);
10050: if ($dom =~ /^$match_domain$/) {
10051: $homeserver = &domain($dom,'primary');
10052: }
10053: unless ($homeserver eq 'no_host') {
10054: my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
10055: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
10056: my @items = split(/&/,$response);
10057: foreach my $item (@items) {
10058: my ($key,$value) = split('=',$item);
10059: $validations{&unescape($key)} = &thaw_unescape($value);
10060: }
10061: }
10062: }
1.1010 raeburn 10063: return %validations;
10064: }
10065:
1.1020 raeburn 10066: sub auto_courserequest_validation {
1.1172.2.43 raeburn 10067: my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist,$custominfo) = @_;
1.1020 raeburn 10068: my ($homeserver,$response);
10069: if ($dom =~ /^$match_domain$/) {
10070: $homeserver = &domain($dom,'primary');
10071: }
1.1172.2.43 raeburn 10072: unless ($homeserver eq 'no_host') {
10073: my $customdata;
10074: if (ref($custominfo) eq 'HASH') {
10075: $customdata = &freeze_escape($custominfo);
10076: }
1.1020 raeburn 10077: $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
1.1021 raeburn 10078: ':'.&escape($crstype).':'.&escape($inststatuslist).
1.1172.2.43 raeburn 10079: ':'.&escape($instcode).':'.&escape($instseclist).':'.
10080: $customdata,$homeserver));
1.1020 raeburn 10081: }
10082: return $response;
10083: }
10084:
1.777 albertel 10085: sub auto_validate_class_sec {
1.918 raeburn 10086: my ($cdom,$cnum,$owners,$inst_class) = @_;
1.773 raeburn 10087: my $homeserver = &homeserver($cnum,$cdom);
1.918 raeburn 10088: my $ownerlist;
10089: if (ref($owners) eq 'ARRAY') {
10090: $ownerlist = join(',',@{$owners});
10091: } else {
10092: $ownerlist = $owners;
10093: }
1.773 raeburn 10094: my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.918 raeburn 10095: &escape($ownerlist).':'.$cdom,$homeserver);
1.773 raeburn 10096: return $response;
10097: }
10098:
1.1172.2.141 raeburn 10099: sub auto_instsec_reformat {
10100: my ($cdom,$action,$instsecref) = @_;
10101: return unless(($action eq 'clutter') || ($action eq 'declutter'));
10102: my @homeservers;
10103: if (defined(&domain($cdom,'primary'))) {
10104: push(@homeservers,&domain($cdom,'primary'));
10105: } else {
10106: my %servers = &get_servers($cdom,'library');
10107: foreach my $tryserver (keys(%servers)) {
10108: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
10109: push(@homeservers,$tryserver);
10110: }
10111: }
10112: }
10113: my $response;
10114: my %reformatted = %{$instsecref};
10115: foreach my $server (@homeservers) {
10116: if (ref($instsecref) eq 'HASH') {
10117: my $info = &freeze_escape($instsecref);
10118: my $response=&reply('autoinstsecreformat:'.$cdom.':'.
10119: $action.':'.$info,$server);
10120: next if ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/);
10121: my @items = split(/&/,$response);
10122: foreach my $item (@items) {
10123: my ($key,$value) = split(/=/,$item);
10124: $reformatted{&unescape($key)} = &thaw_unescape($value);
10125: }
10126: }
10127: }
10128: return %reformatted;
10129: }
10130:
1.1172.2.94 raeburn 10131: sub auto_validate_instclasses {
10132: my ($cdom,$cnum,$owners,$classesref) = @_;
10133: my ($homeserver,%validations);
10134: $homeserver = &homeserver($cnum,$cdom);
10135: unless ($homeserver eq 'no_host') {
10136: my $ownerlist;
10137: if (ref($owners) eq 'ARRAY') {
10138: $ownerlist = join(',',@{$owners});
10139: } else {
10140: $ownerlist = $owners;
10141: }
10142: if (ref($classesref) eq 'HASH') {
10143: my $classes = &freeze_escape($classesref);
10144: my $response=&reply('autovalidateinstclasses:'.&escape($ownerlist).
10145: ':'.$cdom.':'.$classes,$homeserver);
10146: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
10147: my @items = split(/&/,$response);
10148: foreach my $item (@items) {
10149: my ($key,$value) = split('=',$item);
10150: $validations{&unescape($key)} = &thaw_unescape($value);
10151: }
10152: }
10153: }
10154: }
10155: return %validations;
10156: }
10157:
1.1172.2.38 raeburn 10158: sub auto_crsreq_update {
10159: my ($cdom,$cnum,$crstype,$action,$ownername,$ownerdomain,$fullname,$title,
1.1172.2.45 raeburn 10160: $code,$accessstart,$accessend,$inbound) = @_;
1.1172.2.38 raeburn 10161: my ($homeserver,%crsreqresponse);
10162: if ($cdom =~ /^$match_domain$/) {
10163: $homeserver = &domain($cdom,'primary');
10164: }
10165: unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
10166: my $info;
10167: if (ref($inbound) eq 'HASH') {
10168: $info = &freeze_escape($inbound);
10169: }
10170: my $response=&reply('autocrsrequpdate:'.$cdom.':'.$cnum.':'.&escape($crstype).
10171: ':'.&escape($action).':'.&escape($ownername).':'.
10172: &escape($ownerdomain).':'.&escape($fullname).':'.
1.1172.2.45 raeburn 10173: &escape($title).':'.&escape($code).':'.
10174: &escape($accessstart).':'.&escape($accessend).':'.$info,$homeserver);
1.1172.2.38 raeburn 10175: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
10176: my @items = split(/&/,$response);
10177: foreach my $item (@items) {
10178: my ($key,$value) = split('=',$item);
10179: $crsreqresponse{&unescape($key)} = &thaw_unescape($value);
10180: }
10181: }
10182: }
10183: return \%crsreqresponse;
10184: }
10185:
1.1172.2.78 raeburn 10186: sub auto_export_grades {
10187: my ($cdom,$cnum,$inforef,$gradesref) = @_;
10188: my ($homeserver,%exportresponse);
10189: if ($cdom =~ /^$match_domain$/) {
10190: $homeserver = &domain($cdom,'primary');
10191: }
10192: unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
10193: my $info;
10194: if (ref($inforef) eq 'HASH') {
10195: $info = &freeze_escape($inforef);
10196: }
10197: if (ref($gradesref) eq 'HASH') {
10198: my $grades = &freeze_escape($gradesref);
10199: my $response=&reply('encrypt:autoexportgrades:'.$cdom.':'.$cnum.':'.
10200: $info.':'.$grades,$homeserver);
10201: unless ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/) {
10202: my @items = split(/&/,$response);
10203: foreach my $item (@items) {
10204: my ($key,$value) = split('=',$item);
10205: $exportresponse{&unescape($key)} = &thaw_unescape($value);
10206: }
10207: }
10208: }
10209: }
10210: return \%exportresponse;
10211: }
10212:
1.1172.2.68 raeburn 10213: sub check_instcode_cloning {
10214: my ($codedefaults,$code_order,$cloner,$clonefromcode,$clonetocode) = @_;
10215: unless ((ref($codedefaults) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
10216: return;
10217: }
10218: my $canclone;
10219: if (@{$code_order} > 0) {
10220: my $instcoderegexp ='^';
10221: my @clonecodes = split(/\&/,$cloner);
10222: foreach my $item (@{$code_order}) {
10223: if (grep(/^\Q$item\E=/,@clonecodes)) {
10224: foreach my $pair (@clonecodes) {
10225: my ($key,$val) = split(/\=/,$pair,2);
10226: $val = &unescape($val);
10227: if ($key eq $item) {
10228: $instcoderegexp .= '('.$val.')';
10229: last;
10230: }
10231: }
10232: } else {
10233: $instcoderegexp .= $codedefaults->{$item};
10234: }
10235: }
10236: $instcoderegexp .= '$';
10237: my (@from,@to);
10238: eval {
10239: (@from) = ($clonefromcode =~ /$instcoderegexp/);
10240: (@to) = ($clonetocode =~ /$instcoderegexp/);
10241: };
10242: if ((@from > 0) && (@to > 0)) {
10243: my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
10244: if (!@diffs) {
10245: $canclone = 1;
10246: }
10247: }
10248: }
10249: return $canclone;
10250: }
10251:
10252: sub default_instcode_cloning {
10253: my ($clonedom,$domdefclone,$clonefromcode,$clonetocode,$codedefaultsref,$codeorderref) = @_;
10254: my (%codedefaults,@code_order,$canclone);
10255: if ((ref($codedefaultsref) eq 'HASH') && (ref($codeorderref) eq 'ARRAY')) {
10256: %codedefaults = %{$codedefaultsref};
10257: @code_order = @{$codeorderref};
10258: } elsif ($clonedom) {
10259: &auto_instcode_defaults($clonedom,\%codedefaults,\@code_order);
10260: }
10261: if (($domdefclone) && (@code_order)) {
10262: my @clonecodes = split(/\+/,$domdefclone);
10263: my $instcoderegexp ='^';
10264: foreach my $item (@code_order) {
10265: if (grep(/^\Q$item\E$/,@clonecodes)) {
10266: $instcoderegexp .= '('.$codedefaults{$item}.')';
10267: } else {
10268: $instcoderegexp .= $codedefaults{$item};
10269: }
10270: }
10271: $instcoderegexp .= '$';
10272: my (@from,@to);
10273: eval {
10274: (@from) = ($clonefromcode =~ /$instcoderegexp/);
10275: (@to) = ($clonetocode =~ /$instcoderegexp/);
10276: };
10277: if ((@from > 0) && (@to > 0)) {
10278: my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
10279: if (!@diffs) {
10280: $canclone = 1;
10281: }
10282: }
10283: }
10284: return $canclone;
10285: }
10286:
1.679 raeburn 10287: # ------------------------------------------------------- Course Group routines
10288:
10289: sub get_coursegroups {
1.809 raeburn 10290: my ($cdom,$cnum,$group,$namespace) = @_;
10291: return(&dump($namespace,$cdom,$cnum,$group));
1.805 raeburn 10292: }
10293:
1.679 raeburn 10294: sub modify_coursegroup {
10295: my ($cdom,$cnum,$groupsettings) = @_;
10296: return(&put('coursegroups',$groupsettings,$cdom,$cnum));
10297: }
10298:
1.809 raeburn 10299: sub toggle_coursegroup_status {
10300: my ($cdom,$cnum,$group,$action) = @_;
10301: my ($from_namespace,$to_namespace);
10302: if ($action eq 'delete') {
10303: $from_namespace = 'coursegroups';
10304: $to_namespace = 'deleted_groups';
10305: } else {
10306: $from_namespace = 'deleted_groups';
10307: $to_namespace = 'coursegroups';
10308: }
10309: my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805 raeburn 10310: if (my $tmp = &error(%curr_group)) {
10311: &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
10312: return ('read error',$tmp);
10313: } else {
10314: my %savedsettings = %curr_group;
1.809 raeburn 10315: my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805 raeburn 10316: my $deloutcome;
10317: if ($result eq 'ok') {
1.809 raeburn 10318: $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805 raeburn 10319: } else {
10320: return ('write error',$result);
10321: }
10322: if ($deloutcome eq 'ok') {
10323: return 'ok';
10324: } else {
10325: return ('delete error',$deloutcome);
10326: }
10327: }
10328: }
10329:
1.679 raeburn 10330: sub modify_group_roles {
1.957 raeburn 10331: my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
1.679 raeburn 10332: my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
10333: my $role = 'gr/'.&escape($userprivs);
10334: my ($uname,$udom) = split(/:/,$user);
1.957 raeburn 10335: my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
1.684 raeburn 10336: if ($result eq 'ok') {
10337: &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
10338: }
1.679 raeburn 10339: return $result;
10340: }
10341:
10342: sub modify_coursegroup_membership {
10343: my ($cdom,$cnum,$membership) = @_;
10344: my $result = &put('groupmembership',$membership,$cdom,$cnum);
10345: return $result;
10346: }
10347:
1.682 raeburn 10348: sub get_active_groups {
10349: my ($udom,$uname,$cdom,$cnum) = @_;
10350: my $now = time;
10351: my %groups = ();
10352: foreach my $key (keys(%env)) {
1.811 albertel 10353: if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682 raeburn 10354: my ($start,$end) = split(/\./,$env{$key});
10355: if (($end!=0) && ($end<$now)) { next; }
10356: if (($start!=0) && ($start>$now)) { next; }
10357: if ($1 eq $cdom && $2 eq $cnum) {
10358: $groups{$3} = $env{$key} ;
10359: }
10360: }
10361: }
10362: return %groups;
10363: }
10364:
1.683 raeburn 10365: sub get_group_membership {
10366: my ($cdom,$cnum,$group) = @_;
10367: return(&dump('groupmembership',$cdom,$cnum,$group));
10368: }
10369:
10370: sub get_users_groups {
10371: my ($udom,$uname,$courseid) = @_;
1.733 raeburn 10372: my @usersgroups;
1.683 raeburn 10373: my $cachetime=1800;
10374:
10375: my $hashid="$udom:$uname:$courseid";
1.733 raeburn 10376: my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
10377: if (defined($cached)) {
1.734 albertel 10378: @usersgroups = split(/:/,$grouplist);
1.733 raeburn 10379: } else {
10380: $grouplist = '';
1.816 raeburn 10381: my $courseurl = &courseid_to_courseurl($courseid);
1.1166 raeburn 10382: my %roleshash = &dump('roles',$udom,$uname,$courseurl);
1.817 raeburn 10383: my $access_end = $env{'course.'.$courseid.
10384: '.default_enrollment_end_date'};
10385: my $now = time;
10386: foreach my $key (keys(%roleshash)) {
10387: if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
10388: my $group = $1;
10389: if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
10390: my $start = $2;
10391: my $end = $1;
10392: if ($start == -1) { next; } # deleted from group
10393: if (($start!=0) && ($start>$now)) { next; }
10394: if (($end!=0) && ($end<$now)) {
10395: if ($access_end && $access_end < $now) {
10396: if ($access_end - $end < 86400) {
10397: push(@usersgroups,$group);
1.733 raeburn 10398: }
10399: }
1.817 raeburn 10400: next;
1.733 raeburn 10401: }
1.817 raeburn 10402: push(@usersgroups,$group);
1.683 raeburn 10403: }
10404: }
10405: }
1.817 raeburn 10406: @usersgroups = &sort_course_groups($courseid,@usersgroups);
10407: $grouplist = join(':',@usersgroups);
10408: &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683 raeburn 10409: }
1.733 raeburn 10410: return @usersgroups;
1.683 raeburn 10411: }
10412:
10413: sub devalidate_getgroups_cache {
10414: my ($udom,$uname,$cdom,$cnum)=@_;
10415: my $courseid = $cdom.'_'.$cnum;
1.807 albertel 10416:
1.683 raeburn 10417: my $hashid="$udom:$uname:$courseid";
10418: &devalidate_cache_new('getgroups',$hashid);
10419: }
10420:
1.12 www 10421: # ------------------------------------------------------------------ Plain Text
10422:
10423: sub plaintext {
1.988 raeburn 10424: my ($short,$type,$cid,$forcedefault) = @_;
1.1046 raeburn 10425: if ($short =~ m{^cr/}) {
1.758 albertel 10426: return (split('/',$short))[-1];
10427: }
1.742 raeburn 10428: if (!defined($cid)) {
10429: $cid = $env{'request.course.id'};
10430: }
10431: my %rolenames = (
1.1008 raeburn 10432: Course => 'std',
10433: Community => 'alt1',
1.742 raeburn 10434: );
1.1037 raeburn 10435: if ($cid ne '') {
10436: if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
10437: unless ($forcedefault) {
10438: my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'};
10439: &Apache::lonlocal::mt_escape(\$roletext);
10440: return &Apache::lonlocal::mt($roletext);
10441: }
10442: }
10443: }
10444: if ((defined($type)) && (defined($rolenames{$type})) &&
10445: (defined($rolenames{$type})) &&
10446: (defined($prp{$short}{$rolenames{$type}}))) {
1.742 raeburn 10447: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
1.1037 raeburn 10448: } elsif ($cid ne '') {
10449: my $crstype = $env{'course.'.$cid.'.type'};
10450: if (($crstype ne '') && (defined($rolenames{$crstype})) &&
10451: (defined($prp{$short}{$rolenames{$crstype}}))) {
10452: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
10453: }
1.742 raeburn 10454: }
1.1037 raeburn 10455: return &Apache::lonlocal::mt($prp{$short}{'std'});
1.12 www 10456: }
10457:
10458: # ----------------------------------------------------------------- Assign Role
10459:
10460: sub assignrole {
1.957 raeburn 10461: my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
10462: $context)=@_;
1.1172.2.146. .22(raeb 10463:-24): my ($mrole,$rolelogcontext);
1.21 www 10464: if ($role =~ /^cr\//) {
1.393 www 10465: my $cwosec=$url;
1.811 albertel 10466: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.393 www 10467: unless (&allowed('ccr',$cwosec)) {
1.1026 raeburn 10468: my $refused = 1;
10469: if ($context eq 'requestcourses') {
10470: if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
10471: if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
10472: if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
10473: my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10474: my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10475: if ($crsenv{'internal.courseowner'} eq
10476: $env{'user.name'}.':'.$env{'user.domain'}) {
10477: $refused = '';
10478: }
10479: }
10480: }
10481: }
10482: }
10483: if ($refused) {
10484: &logthis('Refused custom assignrole: '.
10485: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
10486: ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
10487: return 'refused';
10488: }
1.104 www 10489: }
1.21 www 10490: $mrole='cr';
1.678 raeburn 10491: } elsif ($role =~ /^gr\//) {
10492: my $cwogrp=$url;
1.811 albertel 10493: $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.678 raeburn 10494: unless (&allowed('mdg',$cwogrp)) {
10495: &logthis('Refused group assignrole: '.
10496: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
10497: $env{'user.name'}.' at '.$env{'user.domain'});
10498: return 'refused';
10499: }
10500: $mrole='gr';
1.21 www 10501: } else {
1.82 www 10502: my $cwosec=$url;
1.811 albertel 10503: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.932 raeburn 10504: if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
10505: my $refused;
10506: if (($env{'request.course.sec'} ne '') && ($role eq 'st')) {
10507: if (!(&allowed('c'.$role,$url))) {
10508: $refused = 1;
10509: }
10510: } else {
10511: $refused = 1;
10512: }
1.947 raeburn 10513: if ($refused) {
1.1045 raeburn 10514: my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10515: if (!$selfenroll && $context eq 'course') {
10516: my %crsenv;
10517: if ($role eq 'cc' || $role eq 'co') {
10518: %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10519: if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
10520: if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
10521: if ($crsenv{'internal.courseowner'} eq
10522: $env{'user.name'}.':'.$env{'user.domain'}) {
10523: $refused = '';
10524: }
10525: }
10526: } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) {
10527: if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
10528: if ($crsenv{'internal.courseowner'} eq
10529: $env{'user.name'}.':'.$env{'user.domain'}) {
10530: $refused = '';
10531: }
10532: }
10533: }
10534: }
1.1172.2.146. .13(raeb 10535:-23): } elsif (($selfenroll == 1) && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
10536:-23): if ($role eq 'st') {
10537:-23): $refused = '';
10538:-23): } elsif (($context eq 'ltienroll') && ($env{'request.lti.login'})) {
10539:-23): $refused = '';
10540:-23): }
1.1017 raeburn 10541: } elsif ($context eq 'requestcourses') {
1.1041 raeburn 10542: my @possroles = ('st','ta','ep','in','cc','co');
1.1026 raeburn 10543: if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
1.1041 raeburn 10544: my $wrongcc;
10545: if ($cnum =~ /^$match_community$/) {
10546: $wrongcc = 1 if ($role eq 'cc');
10547: } else {
10548: $wrongcc = 1 if ($role eq 'co');
10549: }
10550: unless ($wrongcc) {
10551: my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10552: if ($crsenv{'internal.courseowner'} eq
10553: $env{'user.name'}.':'.$env{'user.domain'}) {
10554: $refused = '';
10555: }
1.1017 raeburn 10556: }
10557: }
1.1172.2.9 raeburn 10558: } elsif ($context eq 'requestauthor') {
10559: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
10560: ($url eq '/'.$udom.'/') && ($role eq 'au')) {
10561: if ($env{'environment.requestauthor'} eq 'automatic') {
10562: $refused = '';
10563: } else {
10564: my %domdefaults = &get_domain_defaults($udom);
10565: if (ref($domdefaults{'requestauthor'}) eq 'HASH') {
10566: my $checkbystatus;
10567: if ($env{'user.adv'}) {
10568: my $disposition = $domdefaults{'requestauthor'}{'_LC_adv'};
10569: if ($disposition eq 'automatic') {
10570: $refused = '';
10571: } elsif ($disposition eq '') {
10572: $checkbystatus = 1;
10573: }
10574: } else {
10575: $checkbystatus = 1;
10576: }
10577: if ($checkbystatus) {
10578: if ($env{'environment.inststatus'}) {
10579: my @inststatuses = split(/,/,$env{'environment.inststatus'});
10580: foreach my $type (@inststatuses) {
10581: if (($type ne '') &&
10582: ($domdefaults{'requestauthor'}{$type} eq 'automatic')) {
10583: $refused = '';
10584: }
10585: }
10586: } elsif ($domdefaults{'requestauthor'}{'default'} eq 'automatic') {
10587: $refused = '';
10588: }
10589: }
10590: }
10591: }
10592: }
1.1172.2.146. .22(raeb 10593:-24): } elsif (($context eq 'author') && (($role eq 'ca' || $role eq 'aa'))) {
10594:-24): if ($url =~ m{^/($match_domain)/($match_username)$}) {
10595:-24): my ($audom,$auname) = ($1,$2);
10596:-24): if ((&Apache::lonnet::allowed('v'.$role,"$audom/$auname")) &&
10597:-24): ($env{"environment.internal.manager.$url"})) {
10598:-24): $refused = '';
10599:-24): $rolelogcontext = 'coauthor';
10600:-24): }
10601:-24): }
1.1017 raeburn 10602: }
10603: if ($refused) {
1.947 raeburn 10604: &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
10605: ' '.$role.' '.$end.' '.$start.' by '.
10606: $env{'user.name'}.' at '.$env{'user.domain'});
10607: return 'refused';
10608: }
1.932 raeburn 10609: }
1.1131 raeburn 10610: } elsif ($role eq 'au') {
10611: if ($url ne '/'.$udom.'/') {
10612: &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
10613: ' to assign author role for '.$uname.':'.$udom.
10614: ' in domain: '.$url.' refused (wrong domain).');
10615: return 'refused';
10616: }
1.104 www 10617: }
1.21 www 10618: $mrole=$role;
10619: }
1.620 albertel 10620: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21 www 10621: "$udom:$uname:$url".'_'."$mrole=$role";
1.81 www 10622: if ($end) { $command.='_'.$end; }
1.21 www 10623: if ($start) {
10624: if ($end) {
1.81 www 10625: $command.='_'.$start;
1.21 www 10626: } else {
1.81 www 10627: $command.='_0_'.$start;
1.21 www 10628: }
10629: }
1.739 raeburn 10630: my $origstart = $start;
10631: my $origend = $end;
1.957 raeburn 10632: my $delflag;
1.357 www 10633: # actually delete
10634: if ($deleteflag) {
1.373 www 10635: if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357 www 10636: # modify command to delete the role
1.620 albertel 10637: $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357 www 10638: "$udom:$uname:$url".'_'."$mrole";
1.620 albertel 10639: &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom");
1.357 www 10640: # set start and finish to negative values for userrolelog
10641: $start=-1;
10642: $end=-1;
1.957 raeburn 10643: $delflag = 1;
1.357 www 10644: }
10645: }
10646: # send command
1.349 www 10647: my $answer=&reply($command,&homeserver($uname,$udom));
1.357 www 10648: # log new user role if status is ok
1.349 www 10649: if ($answer eq 'ok') {
1.663 raeburn 10650: &userrolelog($role,$uname,$udom,$url,$start,$end);
1.1172.2.9 raeburn 10651: if (($role eq 'cc') || ($role eq 'in') ||
10652: ($role eq 'ep') || ($role eq 'ad') ||
10653: ($role eq 'ta') || ($role eq 'st') ||
10654: ($role=~/^cr/) || ($role eq 'gr') ||
10655: ($role eq 'co')) {
1.1172.2.13 raeburn 10656: # for course roles, perform group memberships changes triggered by role change.
10657: unless ($role =~ /^gr/) {
10658: &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
10659: $origstart,$selfenroll,$context);
10660: }
1.1172.2.9 raeburn 10661: &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10662: $selfenroll,$context);
10663: } elsif (($role eq 'li') || ($role eq 'dg') || ($role eq 'sc') ||
1.1172.2.90 raeburn 10664: ($role eq 'au') || ($role eq 'dc') || ($role eq 'dh') ||
10665: ($role eq 'da')) {
1.1172.2.9 raeburn 10666: &domainrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10667: $context);
10668: } elsif (($role eq 'ca') || ($role eq 'aa')) {
1.1172.2.146. .22(raeb 10669:-24): if ($rolelogcontext eq '') {
10670:-24): $rolelogcontext = $context;
10671:-24): }
1.1172.2.9 raeburn 10672: &coauthorrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
1.1172.2.146. .22(raeb 10673:-24): $rolelogcontext);
1.1172.2.9 raeburn 10674: }
1.1053 raeburn 10675: if ($role eq 'cc') {
10676: &autoupdate_coowners($url,$end,$start,$uname,$udom);
10677: }
1.349 www 10678: }
10679: return $answer;
1.169 harris41 10680: }
10681:
1.1053 raeburn 10682: sub autoupdate_coowners {
10683: my ($url,$end,$start,$uname,$udom) = @_;
10684: my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
10685: if (($cdom ne '') && ($cnum ne '')) {
10686: my $now = time;
10687: my %domdesign = &Apache::loncommon::get_domainconf($cdom);
10688: if ($domdesign{$cdom.'.autoassign.co-owners'}) {
10689: my %coursehash = &coursedescription($cdom.'_'.$cnum);
10690: my $instcode = $coursehash{'internal.coursecode'};
1.1172.2.141 raeburn 10691: my $xlists = $coursehash{'internal.crosslistings'};
1.1053 raeburn 10692: if ($instcode ne '') {
1.1056 raeburn 10693: if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
10694: unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
1.1053 raeburn 10695: my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
1.1056 raeburn 10696: my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
1.1172.2.141 raeburn 10697: unless ($result eq 'valid') {
10698: if ($xlists ne '') {
10699: foreach my $xlist (split(',',$xlists)) {
10700: my ($inst_crosslist,$lcsec) = split(':',$xlist);
10701: $result =
10702: &auto_validate_inst_crosslist($cnum,$cdom,$instcode,
10703: $inst_crosslist,$uname.':'.$udom);
10704: last if ($result eq 'valid');
10705: }
10706: }
10707: }
1.1056 raeburn 10708: if ($result eq 'valid') {
10709: if ($coursehash{'internal.co-owners'}) {
1.1053 raeburn 10710: foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10711: push(@newcoowners,$coowner);
10712: }
10713: unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
10714: push(@newcoowners,$uname.':'.$udom);
10715: }
10716: @newcoowners = sort(@newcoowners);
10717: } else {
10718: push(@newcoowners,$uname.':'.$udom);
10719: }
1.1172.2.141 raeburn 10720: } elsif ($coursehash{'internal.co-owners'}) {
10721: foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10722: unless ($coowner eq $uname.':'.$udom) {
10723: push(@newcoowners,$coowner);
1.1053 raeburn 10724: }
10725: }
1.1172.2.141 raeburn 10726: unless (@newcoowners > 0) {
10727: $delcoowners = 1;
10728: $coowners = '';
10729: }
1.1053 raeburn 10730: }
10731: if (@newcoowners || $delcoowners) {
10732: &store_coowners($cdom,$cnum,$coursehash{'home'},
10733: $delcoowners,@newcoowners);
10734: }
10735: }
10736: }
10737: }
10738: }
10739: }
10740: }
10741:
10742: sub store_coowners {
10743: my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
10744: my $cid = $cdom.'_'.$cnum;
10745: my ($coowners,$delresult,$putresult);
10746: if (@newcoowners) {
10747: $coowners = join(',',@newcoowners);
10748: my %coownershash = (
10749: 'internal.co-owners' => $coowners,
10750: );
10751: $putresult = &put('environment',\%coownershash,$cdom,$cnum);
10752: if ($putresult eq 'ok') {
10753: if ($env{'course.'.$cid.'.num'} eq $cnum) {
10754: &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
10755: }
10756: }
10757: }
10758: if ($delcoowners) {
10759: $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
10760: if ($delresult eq 'ok') {
10761: if ($env{'course.'.$cid.'.internal.co-owners'}) {
10762: &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
10763: }
10764: }
10765: }
10766: if (($putresult eq 'ok') || ($delresult eq 'ok')) {
10767: my %crsinfo =
10768: &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
10769: if (ref($crsinfo{$cid}) eq 'HASH') {
10770: $crsinfo{$cid}{'co-owners'} = \@newcoowners;
10771: my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
10772: }
10773: }
10774: }
10775:
1.169 harris41 10776: # -------------------------------------------------- Modify user authentication
1.197 www 10777: # Overrides without validation
10778:
1.169 harris41 10779: sub modifyuserauth {
10780: my ($udom,$uname,$umode,$upass)=@_;
10781: my $uhome=&homeserver($uname,$udom);
1.1172.2.115 raeburn 10782: my $allowed;
10783: if (&allowed('mau',$udom)) {
10784: $allowed = 1;
10785: } elsif (($umode eq 'internal') && ($udom eq $env{'user.domain'}) &&
10786: ($env{'request.course.id'}) && (&allowed('mip',$env{'request.course.id'})) &&
10787: (!$env{'course.'.$env{'request.course.id'}.'.internal.nopasswdchg'})) {
10788: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
10789: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
10790: if (($cdom ne '') && ($cnum ne '')) {
10791: my $is_owner = &is_course_owner($cdom,$cnum);
10792: if ($is_owner) {
10793: $allowed = 1;
10794: }
10795: }
10796: }
10797: unless ($allowed) { return 'refused'; }
1.197 www 10798: &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620 albertel 10799: $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10800: ' in domain '.$env{'request.role.domain'});
1.169 harris41 10801: my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
10802: &escape($upass),$uhome);
1.1172.2.134 raeburn 10803: my $ip = &get_requestor_ip();
1.620 albertel 10804: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197 www 10805: 'Authentication changed for '.$udom.', '.$uname.', '.$umode.
1.1172.2.134 raeburn 10806: '(Remote '.$ip.'): '.$reply);
1.197 www 10807: &log($udom,,$uname,$uhome,
1.620 albertel 10808: 'Authentication changed by '.$env{'user.domain'}.', '.
10809: $env{'user.name'}.', '.$umode.
1.1172.2.134 raeburn 10810: '(Remote '.$ip.'): '.$reply);
1.169 harris41 10811: unless ($reply eq 'ok') {
1.197 www 10812: &logthis('Authentication mode error: '.$reply);
1.169 harris41 10813: return 'error: '.$reply;
10814: }
1.170 harris41 10815: return 'ok';
1.80 www 10816: }
10817:
1.81 www 10818: # --------------------------------------------------------------- Modify a user
1.80 www 10819:
1.81 www 10820: sub modifyuser {
1.206 matthew 10821: my ($udom, $uname, $uid,
10822: $umode, $upass, $first,
10823: $middle, $last, $gene,
1.1058 raeburn 10824: $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
1.807 albertel 10825: $udom= &LONCAPA::clean_domain($udom);
10826: $uname=&LONCAPA::clean_username($uname);
1.1059 raeburn 10827: my $showcandelete = 'none';
10828: if (ref($candelete) eq 'ARRAY') {
10829: if (@{$candelete} > 0) {
10830: $showcandelete = join(', ',@{$candelete});
10831: }
10832: }
1.81 www 10833: &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80 www 10834: $umode.', '.$first.', '.$middle.', '.
1.1059 raeburn 10835: $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
1.206 matthew 10836: (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
10837: ' desiredhome not specified').
1.620 albertel 10838: ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10839: ' in domain '.$env{'request.role.domain'});
1.230 stredwic 10840: my $uhome=&homeserver($uname,$udom,'true');
1.1075 raeburn 10841: my $newuser;
10842: if ($uhome eq 'no_host') {
10843: $newuser = 1;
1.1172.2.146. .13(raeb 10844:-23): unless (($umode && ($upass ne '')) || ($umode eq 'localauth') ||
10845:-23): ($umode eq 'lti')) {
10846:-23): return 'error: more information needed to create new user';
10847:-23): }
1.1075 raeburn 10848: }
1.80 www 10849: # ----------------------------------------------------------------- Create User
1.406 albertel 10850: if (($uhome eq 'no_host') &&
1.1172.2.146. .13(raeb 10851:-23): (($umode && $upass) || ($umode eq 'localauth') || ($umode eq 'lti'))) {
1.80 www 10852: my $unhome='';
1.844 albertel 10853: if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) {
1.209 matthew 10854: $unhome = $desiredhome;
1.620 albertel 10855: } elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
10856: $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209 matthew 10857: } else { # load balancing routine for determining $unhome
1.81 www 10858: my $loadm=10000000;
1.841 albertel 10859: my %servers = &get_servers($udom,'library');
10860: foreach my $tryserver (keys(%servers)) {
10861: my $answer=reply('load',$tryserver);
10862: if (($answer=~/\d+/) && ($answer<$loadm)) {
10863: $loadm=$answer;
10864: $unhome=$tryserver;
10865: }
1.80 www 10866: }
10867: }
10868: if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206 matthew 10869: return 'error: unable to find a home server for '.$uname.
10870: ' in domain '.$udom;
1.80 www 10871: }
10872: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
10873: &escape($upass),$unhome);
10874: unless ($reply eq 'ok') {
10875: return 'error: '.$reply;
10876: }
1.230 stredwic 10877: $uhome=&homeserver($uname,$udom,'true');
1.80 www 10878: if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386 matthew 10879: return 'error: unable verify users home machine.';
1.80 www 10880: }
1.209 matthew 10881: } # End of creation of new user
1.80 www 10882: # ---------------------------------------------------------------------- Add ID
10883: if ($uid) {
10884: $uid=~tr/A-Z/a-z/;
10885: my %uidhash=&idrget($udom,$uname);
1.196 www 10886: if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/)
10887: && (!$forceid)) {
1.80 www 10888: unless ($uid eq $uidhash{$uname}) {
1.386 matthew 10889: return 'error: user id "'.$uid.'" does not match '.
10890: 'current user id "'.$uidhash{$uname}.'".';
1.80 www 10891: }
10892: } else {
10893: &idput($udom,($uname => $uid));
10894: }
10895: }
10896: # -------------------------------------------------------------- Add names, etc
1.313 matthew 10897: my @tmp=&get('environment',
1.899 raeburn 10898: ['firstname','middlename','lastname','generation','id',
1.963 raeburn 10899: 'permanentemail','inststatus'],
1.134 albertel 10900: $udom,$uname);
1.1075 raeburn 10901: my (%names,%oldnames);
1.313 matthew 10902: if ($tmp[0] =~ m/^error:.*/) {
10903: %names=();
10904: } else {
10905: %names = @tmp;
1.1075 raeburn 10906: %oldnames = %names;
1.313 matthew 10907: }
1.388 www 10908: #
1.1058 raeburn 10909: # If name, email and/or uid are blank (e.g., because an uploaded file
10910: # of users did not contain them), do not overwrite existing values
10911: # unless field is in $candelete array ref.
10912: #
10913:
10914: my @fields = ('firstname','middlename','lastname','generation',
10915: 'permanentemail','id');
10916: my %newvalues;
10917: if (ref($candelete) eq 'ARRAY') {
10918: foreach my $field (@fields) {
10919: if (grep(/^\Q$field\E$/,@{$candelete})) {
10920: if ($field eq 'firstname') {
10921: $names{$field} = $first;
10922: } elsif ($field eq 'middlename') {
10923: $names{$field} = $middle;
10924: } elsif ($field eq 'lastname') {
10925: $names{$field} = $last;
10926: } elsif ($field eq 'generation') {
10927: $names{$field} = $gene;
10928: } elsif ($field eq 'permanentemail') {
10929: $names{$field} = $email;
10930: } elsif ($field eq 'id') {
10931: $names{$field} = $uid;
10932: }
10933: }
10934: }
10935: }
1.388 www 10936: if ($first) { $names{'firstname'} = $first; }
1.385 matthew 10937: if (defined($middle)) { $names{'middlename'} = $middle; }
1.388 www 10938: if ($last) { $names{'lastname'} = $last; }
1.385 matthew 10939: if (defined($gene)) { $names{'generation'} = $gene; }
1.592 www 10940: if ($email) {
10941: $email=~s/[^\w\@\.\-\,]//gs;
1.963 raeburn 10942: if ($email=~/\@/) { $names{'permanentemail'} = $email; }
1.592 www 10943: }
1.899 raeburn 10944: if ($uid) { $names{'id'} = $uid; }
1.989 raeburn 10945: if (defined($inststatus)) {
10946: $names{'inststatus'} = '';
10947: my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
10948: if (ref($usertypes) eq 'HASH') {
10949: my @okstatuses;
10950: foreach my $item (split(/:/,$inststatus)) {
10951: if (defined($usertypes->{$item})) {
10952: push(@okstatuses,$item);
10953: }
10954: }
10955: if (@okstatuses) {
10956: $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
10957: }
10958: }
10959: }
1.1075 raeburn 10960: my $logmsg = $udom.', '.$uname.', '.$uid.', '.
1.963 raeburn 10961: $umode.', '.$first.', '.$middle.', '.
1.1075 raeburn 10962: $last.', '.$gene.', '.$email.', '.$inststatus;
1.963 raeburn 10963: if ($env{'user.name'} ne '' && $env{'user.domain'}) {
10964: $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
10965: } else {
10966: $logmsg .= ' during self creation';
10967: }
1.1075 raeburn 10968: my $changed;
10969: if ($newuser) {
10970: $changed = 1;
10971: } else {
10972: foreach my $field (@fields) {
10973: if ($names{$field} ne $oldnames{$field}) {
10974: $changed = 1;
10975: last;
10976: }
10977: }
10978: }
10979: unless ($changed) {
10980: $logmsg = 'No changes in user information needed for: '.$logmsg;
10981: &logthis($logmsg);
10982: return 'ok';
10983: }
10984: my $reply = &put('environment', \%names, $udom,$uname);
10985: if ($reply ne 'ok') {
10986: return 'error: '.$reply;
10987: }
1.1087 raeburn 10988: if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
10989: &Apache::lonnet::devalidate_cache_new('emailscache',$uname.':'.$udom);
10990: }
1.1075 raeburn 10991: my $sqlresult = &update_allusers_table($uname,$udom,\%names);
10992: &devalidate_cache_new('namescache',$uname.':'.$udom);
10993: $logmsg = 'Success modifying user '.$logmsg;
1.963 raeburn 10994: &logthis($logmsg);
1.134 albertel 10995: return 'ok';
1.80 www 10996: }
10997:
1.81 www 10998: # -------------------------------------------------------------- Modify student
1.80 www 10999:
1.81 www 11000: sub modifystudent {
11001: my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.957 raeburn 11002: $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
1.1172.2.76 raeburn 11003: $selfenroll,$context,$inststatus,$credits,$instsec)=@_;
1.455 albertel 11004: if (!$cid) {
1.620 albertel 11005: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 11006: return 'not_in_class';
11007: }
1.80 www 11008: }
11009: # --------------------------------------------------------------- Make the user
1.81 www 11010: my $reply=&modifyuser
1.209 matthew 11011: ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.990 raeburn 11012: $desiredhome,$email,$inststatus);
1.80 www 11013: unless ($reply eq 'ok') { return $reply; }
1.297 matthew 11014: # This will cause &modify_student_enrollment to get the uid from the
1.1172.2.31 raeburn 11015: # student's environment
1.297 matthew 11016: $uid = undef if (!$forceid);
1.455 albertel 11017: $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.1172.2.19 raeburn 11018: $gene,$usec,$end,$start,$type,$locktype,
1.1172.2.76 raeburn 11019: $cid,$selfenroll,$context,$credits,$instsec);
1.297 matthew 11020: return $reply;
11021: }
11022:
11023: sub modify_student_enrollment {
1.1172.2.23 raeburn 11024: my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,
1.1172.2.76 raeburn 11025: $locktype,$cid,$selfenroll,$context,$credits,$instsec) = @_;
1.455 albertel 11026: my ($cdom,$cnum,$chome);
11027: if (!$cid) {
1.620 albertel 11028: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 11029: return 'not_in_class';
11030: }
1.620 albertel 11031: $cdom=$env{'course.'.$cid.'.domain'};
11032: $cnum=$env{'course.'.$cid.'.num'};
1.455 albertel 11033: } else {
11034: ($cdom,$cnum)=split(/_/,$cid);
11035: }
1.620 albertel 11036: $chome=$env{'course.'.$cid.'.home'};
1.455 albertel 11037: if (!$chome) {
1.457 raeburn 11038: $chome=&homeserver($cnum,$cdom);
1.297 matthew 11039: }
1.455 albertel 11040: if (!$chome) { return 'unknown_course'; }
1.297 matthew 11041: # Make sure the user exists
1.81 www 11042: my $uhome=&homeserver($uname,$udom);
11043: if (($uhome eq '') || ($uhome eq 'no_host')) {
11044: return 'error: no such user';
11045: }
1.297 matthew 11046: # Get student data if we were not given enough information
11047: if (!defined($first) || $first eq '' ||
11048: !defined($last) || $last eq '' ||
11049: !defined($uid) || $uid eq '' ||
11050: !defined($middle) || $middle eq '' ||
11051: !defined($gene) || $gene eq '') {
1.294 matthew 11052: # They did not supply us with enough data to enroll the student, so
11053: # we need to pick up more information.
1.297 matthew 11054: my %tmp = &get('environment',
1.294 matthew 11055: ['firstname','middlename','lastname', 'generation','id']
1.297 matthew 11056: ,$udom,$uname);
11057:
1.800 albertel 11058: #foreach my $key (keys(%tmp)) {
11059: # &logthis("key $key = ".$tmp{$key});
1.455 albertel 11060: #}
1.294 matthew 11061: $first = $tmp{'firstname'} if (!defined($first) || $first eq '');
11062: $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
11063: $last = $tmp{'lastname'} if (!defined($last) || $last eq '');
1.297 matthew 11064: $gene = $tmp{'generation'} if (!defined($gene) || $gene eq '');
1.294 matthew 11065: $uid = $tmp{'id'} if (!defined($uid) || $uid eq '');
11066: }
1.556 albertel 11067: my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.1148 raeburn 11068: my $user = "$uname:$udom";
11069: my %old_entry = &Apache::lonnet::get('classlist',[$user],$cdom,$cnum);
1.487 albertel 11070: my $reply=cput('classlist',
1.1148 raeburn 11071: {$user =>
1.1172.2.76 raeburn 11072: join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype,$credits,$instsec) },
1.487 albertel 11073: $cdom,$cnum);
1.1148 raeburn 11074: if (($reply eq 'ok') || ($reply eq 'delayed')) {
11075: &devalidate_getsection_cache($udom,$uname,$cid);
11076: } else {
1.81 www 11077: return 'error: '.$reply;
11078: }
1.297 matthew 11079: # Add student role to user
1.83 www 11080: my $uurl='/'.$cid;
1.81 www 11081: $uurl=~s/\_/\//g;
11082: if ($usec) {
11083: $uurl.='/'.$usec;
11084: }
1.1148 raeburn 11085: my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
11086: $selfenroll,$context);
11087: if ($result ne 'ok') {
11088: if ($old_entry{$user} ne '') {
11089: $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
11090: } else {
11091: $reply = &del('classlist',[$user],$cdom,$cnum);
11092: }
11093: }
11094: return $result;
1.21 www 11095: }
11096:
1.556 albertel 11097: sub format_name {
11098: my ($firstname,$middlename,$lastname,$generation,$first)=@_;
11099: my $name;
11100: if ($first ne 'lastname') {
11101: $name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
11102: } else {
11103: if ($lastname=~/\S/) {
11104: $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
11105: $name=~s/\s+,/,/;
11106: } else {
11107: $name.= $firstname.' '.$middlename.' '.$generation;
11108: }
11109: }
11110: $name=~s/^\s+//;
11111: $name=~s/\s+$//;
11112: $name=~s/\s+/ /g;
11113: return $name;
11114: }
11115:
1.84 www 11116: # ------------------------------------------------- Write to course preferences
11117:
11118: sub writecoursepref {
11119: my ($courseid,%prefs)=@_;
11120: $courseid=~s/^\///;
11121: $courseid=~s/\_/\//g;
11122: my ($cdomain,$cnum)=split(/\//,$courseid);
11123: my $chome=homeserver($cnum,$cdomain);
11124: if (($chome eq '') || ($chome eq 'no_host')) {
11125: return 'error: no such course';
11126: }
11127: my $cstring='';
1.800 albertel 11128: foreach my $pref (keys(%prefs)) {
11129: $cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191 harris41 11130: }
1.84 www 11131: $cstring=~s/\&$//;
11132: return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
11133: }
11134:
11135: # ---------------------------------------------------------- Make/modify course
11136:
11137: sub createcourse {
1.741 raeburn 11138: my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
1.1172.2.146. .1(raebu 11139:22): $course_owner,$crstype,$cnum,$context,$category,$callercontext)=@_;
1.84 www 11140: $url=&declutter($url);
11141: my $cid='';
1.1028 raeburn 11142: if ($context eq 'requestcourses') {
11143: my $can_create = 0;
11144: my ($ownername,$ownerdom) = split(':',$course_owner);
11145: if ($udom eq $ownerdom) {
1.1172.2.146. .1(raebu 11146:22): my $reload;
11147:22): if (($callercontext eq 'auto') &&
11148:22): ($ownerdom eq $env{'user.domain'}) && ($ownername eq $env{'user.name'})) {
11149:22): $reload = 'reload';
11150:22): }
11151:22): if (&usertools_access($ownername,$ownerdom,$category,$reload,
1.1028 raeburn 11152: $context)) {
11153: $can_create = 1;
11154: }
11155: } else {
11156: my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
11157: $category);
11158: if ($userenv{'reqcrsotherdom.'.$category} ne '') {
11159: my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
11160: if (@curr > 0) {
11161: my @options = qw(approval validate autolimit);
11162: my $optregex = join('|',@options);
11163: if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
11164: $can_create = 1;
11165: }
11166: }
11167: }
11168: }
11169: if ($can_create) {
11170: unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
11171: unless (&allowed('ccc',$udom)) {
11172: return 'refused';
11173: }
1.1017 raeburn 11174: }
11175: } else {
11176: return 'refused';
11177: }
1.1028 raeburn 11178: } elsif (!&allowed('ccc',$udom)) {
11179: return 'refused';
1.84 www 11180: }
1.1011 raeburn 11181: # --------------------------------------------------------------- Get Unique ID
11182: my $uname;
11183: if ($cnum =~ /^$match_courseid$/) {
11184: my $chome=&homeserver($cnum,$udom,'true');
11185: if (($chome eq '') || ($chome eq 'no_host')) {
11186: $uname = $cnum;
11187: } else {
1.1038 raeburn 11188: $uname = &generate_coursenum($udom,$crstype);
1.1011 raeburn 11189: }
11190: } else {
1.1038 raeburn 11191: $uname = &generate_coursenum($udom,$crstype);
1.1011 raeburn 11192: }
11193: return $uname if ($uname =~ /^error/);
11194: # -------------------------------------------------- Check supplied server name
1.1052 raeburn 11195: if (!defined($course_server)) {
11196: if (defined(&domain($udom,'primary'))) {
11197: $course_server = &domain($udom,'primary');
11198: } else {
11199: $course_server = $env{'user.home'};
11200: }
11201: }
11202: my %host_servers =
11203: &Apache::lonnet::get_servers($udom,'library');
11204: unless ($host_servers{$course_server}) {
11205: return 'error: invalid home server for course: '.$course_server;
1.264 matthew 11206: }
1.84 www 11207: # ------------------------------------------------------------- Make the course
11208: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264 matthew 11209: $course_server);
1.84 www 11210: unless ($reply eq 'ok') { return 'error: '.$reply; }
1.1011 raeburn 11211: my $uhome=&homeserver($uname,$udom,'true');
1.84 www 11212: if (($uhome eq '') || ($uhome eq 'no_host')) {
11213: return 'error: no such course';
11214: }
1.271 www 11215: # ----------------------------------------------------------------- Course made
1.516 raeburn 11216: # log existence
1.1029 raeburn 11217: my $now = time;
1.918 raeburn 11218: my $newcourse = {
11219: $udom.'_'.$uname => {
1.921 raeburn 11220: description => $description,
11221: inst_code => $inst_code,
11222: owner => $course_owner,
11223: type => $crstype,
1.1029 raeburn 11224: creator => $env{'user.name'}.':'.
11225: $env{'user.domain'},
11226: created => $now,
11227: context => $context,
1.918 raeburn 11228: },
11229: };
1.921 raeburn 11230: &courseidput($udom,$newcourse,$uhome,'notime');
1.358 www 11231: # set toplevel url
1.271 www 11232: my $topurl=$url;
11233: unless ($nonstandard) {
11234: # ------------------------------------------ For standard courses, make top url
11235: my $mapurl=&clutter($url);
1.278 www 11236: if ($mapurl eq '/res/') { $mapurl=''; }
1.620 albertel 11237: $env{'form.initmap'}=(<<ENDINITMAP);
1.271 www 11238: <map>
11239: <resource id="1" type="start"></resource>
11240: <resource id="2" src="$mapurl"></resource>
11241: <resource id="3" type="finish"></resource>
11242: <link index="1" from="1" to="2"></link>
11243: <link index="2" from="2" to="3"></link>
11244: </map>
11245: ENDINITMAP
11246: $topurl=&declutter(
1.638 albertel 11247: &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271 www 11248: );
11249: }
11250: # ----------------------------------------------------------- Write preferences
1.84 www 11251: &writecoursepref($udom.'_'.$uname,
1.1056 raeburn 11252: ('description' => $description,
11253: 'url' => $topurl,
11254: 'internal.creator' => $env{'user.name'}.':'.
11255: $env{'user.domain'},
11256: 'internal.created' => $now,
11257: 'internal.creationcontext' => $context)
11258: );
1.84 www 11259: return '/'.$udom.'/'.$uname;
11260: }
11261:
1.1011 raeburn 11262: # ------------------------------------------------------------------- Create ID
11263: sub generate_coursenum {
1.1038 raeburn 11264: my ($udom,$crstype) = @_;
1.1011 raeburn 11265: my $domdesc = &domain($udom);
11266: return 'error: invalid domain' if ($domdesc eq '');
1.1038 raeburn 11267: my $first;
11268: if ($crstype eq 'Community') {
11269: $first = '0';
11270: } else {
11271: $first = int(1+rand(9));
11272: }
11273: my $uname=$first.
1.1011 raeburn 11274: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
11275: substr($$.time,0,5).unpack("H8",pack("I32",time)).
11276: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
11277: # ----------------------------------------------- Make sure that does not exist
11278: my $uhome=&homeserver($uname,$udom,'true');
11279: unless (($uhome eq '') || ($uhome eq 'no_host')) {
1.1038 raeburn 11280: if ($crstype eq 'Community') {
11281: $first = '0';
11282: } else {
11283: $first = int(1+rand(9));
11284: }
11285: $uname=$first.
1.1011 raeburn 11286: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
11287: substr($$.time,0,5).unpack("H8",pack("I32",time)).
11288: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
11289: $uhome=&homeserver($uname,$udom,'true');
11290: unless (($uhome eq '') || ($uhome eq 'no_host')) {
11291: return 'error: unable to generate unique course-ID';
11292: }
11293: }
11294: return $uname;
11295: }
11296:
1.813 albertel 11297: sub is_course {
1.1167 droeschl 11298: my ($cdom, $cnum) = scalar(@_) == 1 ?
11299: ($_[0] =~ /^($match_domain)_($match_courseid)$/) : @_;
1.1172.2.95 raeburn 11300: return unless (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/));
11301: my $uhome=&homeserver($cnum,$cdom);
11302: my $iscourse;
11303: if (grep { $_ eq $uhome } current_machine_ids()) {
11304: $iscourse = &LONCAPA::Lond::is_course($cdom,$cnum);
11305: } else {
11306: my $hashid = $cdom.':'.$cnum;
11307: ($iscourse,my $cached) = &is_cached_new('iscourse',$hashid);
11308: unless (defined($cached)) {
11309: my %courses = &courseiddump($cdom, '.', 1, '.', '.',
11310: $cnum,undef,undef,'.');
11311: $iscourse = 0;
11312: if (exists($courses{$cdom.'_'.$cnum})) {
11313: $iscourse = 1;
11314: }
11315: &do_cache_new('iscourse',$hashid,$iscourse,3600);
11316: }
11317: }
11318: return unless($iscourse);
1.1167 droeschl 11319: return wantarray ? ($cdom, $cnum) : $cdom.'_'.$cnum;
1.813 albertel 11320: }
11321:
1.1015 raeburn 11322: sub store_userdata {
11323: my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
1.1013 raeburn 11324: my $result;
1.1016 raeburn 11325: if ($datakey ne '') {
1.1013 raeburn 11326: if (ref($storehash) eq 'HASH') {
1.1017 raeburn 11327: if ($udom eq '' || $uname eq '') {
11328: $udom = $env{'user.domain'};
11329: $uname = $env{'user.name'};
11330: }
11331: my $uhome=&homeserver($uname,$udom);
1.1013 raeburn 11332: if (($uhome eq '') || ($uhome eq 'no_host')) {
11333: $result = 'error: no_host';
11334: } else {
1.1172.2.138 raeburn 11335: $storehash->{'ip'} = &get_requestor_ip();
1.1013 raeburn 11336: $storehash->{'host'} = $perlvar{'lonHostID'};
11337:
11338: my $namevalue='';
11339: foreach my $key (keys(%{$storehash})) {
11340: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
11341: }
11342: $namevalue=~s/\&$//;
1.1172.2.26 raeburn 11343: unless ($namespace eq 'courserequests') {
11344: $datakey = &escape($datakey);
11345: }
1.1105 raeburn 11346: $result = &reply("store:$udom:$uname:$namespace:$datakey:".
11347: $namevalue,$uhome);
1.1013 raeburn 11348: }
11349: } else {
11350: $result = 'error: data to store was not a hash reference';
11351: }
11352: } else {
11353: $result= 'error: invalid requestkey';
11354: }
11355: return $result;
11356: }
11357:
1.21 www 11358: # ---------------------------------------------------------- Assign Custom Role
11359:
11360: sub assigncustomrole {
1.957 raeburn 11361: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
1.21 www 11362: return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.957 raeburn 11363: $end,$start,$deleteflag,$selfenroll,$context);
1.21 www 11364: }
11365:
11366: # ----------------------------------------------------------------- Revoke Role
11367:
11368: sub revokerole {
1.957 raeburn 11369: my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
1.21 www 11370: my $now=time;
1.965 raeburn 11371: return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
1.21 www 11372: }
11373:
11374: # ---------------------------------------------------------- Revoke Custom Role
11375:
11376: sub revokecustomrole {
1.957 raeburn 11377: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
1.21 www 11378: my $now=time;
1.357 www 11379: return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
1.957 raeburn 11380: $deleteflag,$selfenroll,$context);
1.17 www 11381: }
11382:
1.533 banghart 11383: # ------------------------------------------------------------ Disk usage
1.535 albertel 11384: sub diskusage {
1.955 raeburn 11385: my ($udom,$uname,$directorypath,$getpropath)=@_;
11386: $directorypath =~ s/\/$//;
11387: my $listing=&reply('du2:'.&escape($directorypath).':'
11388: .&escape($getpropath).':'.&escape($uname).':'
11389: .&escape($udom),homeserver($uname,$udom));
11390: if ($listing eq 'unknown_cmd') {
11391: if ($getpropath) {
11392: $directorypath = &propath($udom,$uname).'/'.$directorypath;
11393: }
11394: $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
11395: }
1.514 albertel 11396: return $listing;
1.512 banghart 11397: }
11398:
1.566 banghart 11399: sub is_locked {
1.1096 raeburn 11400: my ($file_name, $domain, $user, $which) = @_;
1.566 banghart 11401: my @check;
11402: my $is_locked;
1.1093 raeburn 11403: push (@check,$file_name);
1.613 albertel 11404: my %locked = &get('file_permissions',\@check,
1.620 albertel 11405: $env{'user.domain'},$env{'user.name'});
1.615 albertel 11406: my ($tmp)=keys(%locked);
11407: if ($tmp=~/^error:/) { undef(%locked); }
1.745 raeburn 11408:
1.566 banghart 11409: if (ref($locked{$file_name}) eq 'ARRAY') {
1.745 raeburn 11410: $is_locked = 'false';
11411: foreach my $entry (@{$locked{$file_name}}) {
1.1096 raeburn 11412: if (ref($entry) eq 'ARRAY') {
1.746 raeburn 11413: $is_locked = 'true';
1.1096 raeburn 11414: if (ref($which) eq 'ARRAY') {
11415: push(@{$which},$entry);
11416: } else {
11417: last;
11418: }
1.745 raeburn 11419: }
11420: }
1.566 banghart 11421: } else {
11422: $is_locked = 'false';
11423: }
1.1093 raeburn 11424: return $is_locked;
1.566 banghart 11425: }
11426:
1.759 albertel 11427: sub declutter_portfile {
11428: my ($file) = @_;
1.833 albertel 11429: $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759 albertel 11430: return $file;
11431: }
11432:
1.559 banghart 11433: # ------------------------------------------------------------- Mark as Read Only
11434:
11435: sub mark_as_readonly {
11436: my ($domain,$user,$files,$what) = @_;
1.613 albertel 11437: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 11438: my ($tmp)=keys(%current_permissions);
11439: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560 banghart 11440: foreach my $file (@{$files}) {
1.759 albertel 11441: $file = &declutter_portfile($file);
1.561 banghart 11442: push(@{$current_permissions{$file}},$what);
1.559 banghart 11443: }
1.613 albertel 11444: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 11445: return;
11446: }
11447:
1.572 banghart 11448: # ------------------------------------------------------------Save Selected Files
11449:
11450: sub save_selected_files {
11451: my ($user, $path, @files) = @_;
11452: my $filename = $user."savedfiles";
1.573 banghart 11453: my @other_files = &files_not_in_path($user, $path);
1.1172.2.96 raeburn 11454: open (OUT,'>',LONCAPA::tempdir().$filename);
1.573 banghart 11455: foreach my $file (@files) {
1.620 albertel 11456: print (OUT $env{'form.currentpath'}.$file."\n");
1.573 banghart 11457: }
11458: foreach my $file (@other_files) {
1.574 banghart 11459: print (OUT $file."\n");
1.572 banghart 11460: }
1.574 banghart 11461: close (OUT);
1.572 banghart 11462: return 'ok';
11463: }
11464:
1.574 banghart 11465: sub clear_selected_files {
11466: my ($user) = @_;
11467: my $filename = $user."savedfiles";
1.1172.2.96 raeburn 11468: open (OUT,'>',LONCAPA::tempdir().$filename);
1.574 banghart 11469: print (OUT undef);
11470: close (OUT);
11471: return ("ok");
11472: }
11473:
1.572 banghart 11474: sub files_in_path {
11475: my ($user, $path) = @_;
11476: my $filename = $user."savedfiles";
11477: my %return_files;
1.1172.2.96 raeburn 11478: open (IN,'<',LONCAPA::tempdir().$filename);
1.573 banghart 11479: while (my $line_in = <IN>) {
1.574 banghart 11480: chomp ($line_in);
11481: my @paths_and_file = split (m!/!, $line_in);
11482: my $file_part = pop (@paths_and_file);
11483: my $path_part = join ('/', @paths_and_file);
1.573 banghart 11484: $path_part.='/';
11485: my $path_and_file = $path_part.$file_part;
11486: if ($path_part eq $path) {
11487: $return_files{$file_part}= 'selected';
11488: }
11489: }
1.574 banghart 11490: close (IN);
11491: return (\%return_files);
1.572 banghart 11492: }
11493:
11494: # called in portfolio select mode, to show files selected NOT in current directory
11495: sub files_not_in_path {
11496: my ($user, $path) = @_;
11497: my $filename = $user."savedfiles";
11498: my @return_files;
11499: my $path_part;
1.1172.2.96 raeburn 11500: open(IN, '<',LONCAPA::tempdir().$filename);
1.800 albertel 11501: while (my $line = <IN>) {
1.572 banghart 11502: #ok, I know it's clunky, but I want it to work
1.800 albertel 11503: my @paths_and_file = split(m|/|, $line);
11504: my $file_part = pop(@paths_and_file);
11505: chomp($file_part);
11506: my $path_part = join('/', @paths_and_file);
1.572 banghart 11507: $path_part .= '/';
11508: my $path_and_file = $path_part.$file_part;
11509: if ($path_part ne $path) {
1.800 albertel 11510: push(@return_files, ($path_and_file));
1.572 banghart 11511: }
11512: }
1.800 albertel 11513: close(OUT);
1.574 banghart 11514: return (@return_files);
1.572 banghart 11515: }
11516:
1.745 raeburn 11517: #----------------------------------------------Get portfolio file permissions
1.629 banghart 11518:
1.745 raeburn 11519: sub get_portfile_permissions {
11520: my ($domain,$user) = @_;
1.613 albertel 11521: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 11522: my ($tmp)=keys(%current_permissions);
11523: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745 raeburn 11524: return \%current_permissions;
11525: }
11526:
11527: #---------------------------------------------Get portfolio file access controls
11528:
1.749 raeburn 11529: sub get_access_controls {
1.745 raeburn 11530: my ($current_permissions,$group,$file) = @_;
1.769 albertel 11531: my %access;
11532: my $real_file = $file;
11533: $file =~ s/\.meta$//;
1.745 raeburn 11534: if (defined($file)) {
1.749 raeburn 11535: if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
11536: foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769 albertel 11537: $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749 raeburn 11538: }
11539: }
1.745 raeburn 11540: } else {
1.749 raeburn 11541: foreach my $key (keys(%{$current_permissions})) {
11542: if ($key =~ /\0accesscontrol$/) {
11543: if (defined($group)) {
11544: if ($key !~ m-^\Q$group\E/-) {
11545: next;
11546: }
11547: }
11548: my ($fullpath) = split(/\0/,$key);
11549: if (ref($$current_permissions{$key}) eq 'HASH') {
11550: foreach my $control (keys(%{$$current_permissions{$key}})) {
11551: $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
11552: }
11553: }
11554: }
11555: }
11556: }
11557: return %access;
11558: }
11559:
11560: sub modify_access_controls {
11561: my ($file_name,$changes,$domain,$user)=@_;
11562: my ($outcome,$deloutcome);
11563: my %store_permissions;
11564: my %new_values;
11565: my %new_control;
11566: my %translation;
11567: my @deletions = ();
11568: my $now = time;
11569: if (exists($$changes{'activate'})) {
11570: if (ref($$changes{'activate'}) eq 'HASH') {
11571: my @newitems = sort(keys(%{$$changes{'activate'}}));
11572: my $numnew = scalar(@newitems);
11573: for (my $i=0; $i<$numnew; $i++) {
11574: my $newkey = $newitems[$i];
11575: my $newid = &Apache::loncommon::get_cgi_id();
1.797 raeburn 11576: if ($newkey =~ /^\d+:/) {
11577: $newkey =~ s/^(\d+)/$newid/;
11578: $translation{$1} = $newid;
11579: } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
11580: $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
11581: $translation{$1} = $newid;
11582: }
1.749 raeburn 11583: $new_values{$file_name."\0".$newkey} =
11584: $$changes{'activate'}{$newitems[$i]};
11585: $new_control{$newkey} = $now;
11586: }
11587: }
11588: }
11589: my %todelete;
11590: my %changed_items;
11591: foreach my $action ('delete','update') {
11592: if (exists($$changes{$action})) {
11593: if (ref($$changes{$action}) eq 'HASH') {
11594: foreach my $key (keys(%{$$changes{$action}})) {
11595: my ($itemnum) = ($key =~ /^([^:]+):/);
11596: if ($action eq 'delete') {
11597: $todelete{$itemnum} = 1;
11598: } else {
11599: $changed_items{$itemnum} = $key;
11600: }
11601: }
1.745 raeburn 11602: }
11603: }
1.749 raeburn 11604: }
11605: # get lock on access controls for file.
11606: my $lockhash = {
11607: $file_name."\0".'locked_access_records' => $env{'user.name'}.
11608: ':'.$env{'user.domain'},
11609: };
11610: my $tries = 0;
11611: my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
11612:
1.1172.2.79 raeburn 11613: while (($gotlock ne 'ok') && $tries < 10) {
1.749 raeburn 11614: $tries ++;
1.1172.2.79 raeburn 11615: sleep(0.1);
1.749 raeburn 11616: $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
11617: }
11618: if ($gotlock eq 'ok') {
11619: my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
11620: my ($tmp)=keys(%curr_permissions);
11621: if ($tmp=~/^error:/) { undef(%curr_permissions); }
11622: if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
11623: my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
11624: if (ref($curr_controls) eq 'HASH') {
11625: foreach my $control_item (keys(%{$curr_controls})) {
11626: my ($itemnum) = ($control_item =~ /^([^:]+):/);
11627: if (defined($todelete{$itemnum})) {
11628: push(@deletions,$file_name."\0".$control_item);
11629: } else {
11630: if (defined($changed_items{$itemnum})) {
11631: $new_control{$changed_items{$itemnum}} = $now;
11632: push(@deletions,$file_name."\0".$control_item);
11633: $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
11634: } else {
11635: $new_control{$control_item} = $$curr_controls{$control_item};
11636: }
11637: }
1.745 raeburn 11638: }
11639: }
11640: }
1.970 raeburn 11641: my ($group);
11642: if (&is_course($domain,$user)) {
11643: ($group,my $file) = split(/\//,$file_name,2);
11644: }
1.749 raeburn 11645: $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
11646: $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
11647: $outcome = &put('file_permissions',\%new_values,$domain,$user);
11648: # remove lock
11649: my @del_lock = ($file_name."\0".'locked_access_records');
11650: my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818 raeburn 11651: my $sqlresult =
1.970 raeburn 11652: &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
1.818 raeburn 11653: $group);
1.749 raeburn 11654: } else {
11655: $outcome = "error: could not obtain lockfile\n";
1.745 raeburn 11656: }
1.749 raeburn 11657: return ($outcome,$deloutcome,\%new_values,\%translation);
1.745 raeburn 11658: }
11659:
1.827 raeburn 11660: sub make_public_indefinitely {
11661: my ($requrl) = @_;
11662: my $now = time;
11663: my $action = 'activate';
11664: my $aclnum = 0;
11665: if (&is_portfolio_url($requrl)) {
11666: my (undef,$udom,$unum,$file_name,$group) =
11667: &parse_portfolio_url($requrl);
11668: my $current_perms = &get_portfile_permissions($udom,$unum);
11669: my %access_controls = &get_access_controls($current_perms,
11670: $group,$file_name);
11671: foreach my $key (keys(%{$access_controls{$file_name}})) {
11672: my ($num,$scope,$end,$start) =
11673: ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
11674: if ($scope eq 'public') {
11675: if ($start <= $now && $end == 0) {
11676: $action = 'none';
11677: } else {
11678: $action = 'update';
11679: $aclnum = $num;
11680: }
11681: last;
11682: }
11683: }
11684: if ($action eq 'none') {
11685: return 'ok';
11686: } else {
11687: my %changes;
11688: my $newend = 0;
11689: my $newstart = $now;
11690: my $newkey = $aclnum.':public_'.$newend.'_'.$newstart;
11691: $changes{$action}{$newkey} = {
11692: type => 'public',
11693: time => {
11694: start => $newstart,
11695: end => $newend,
11696: },
11697: };
11698: my ($outcome,$deloutcome,$new_values,$translation) =
11699: &modify_access_controls($file_name,\%changes,$udom,$unum);
11700: return $outcome;
11701: }
11702: } else {
11703: return 'invalid';
11704: }
11705: }
11706:
1.745 raeburn 11707: #------------------------------------------------------Get Marked as Read Only
11708:
11709: sub get_marked_as_readonly {
11710: my ($domain,$user,$what,$group) = @_;
11711: my $current_permissions = &get_portfile_permissions($domain,$user);
1.563 banghart 11712: my @readonly_files;
1.629 banghart 11713: my $cmp1=$what;
11714: if (ref($what)) { $cmp1=join('',@{$what}) };
1.745 raeburn 11715: while (my ($file_name,$value) = each(%{$current_permissions})) {
11716: if (defined($group)) {
11717: if ($file_name !~ m-^\Q$group\E/-) {
11718: next;
11719: }
11720: }
1.561 banghart 11721: if (ref($value) eq "ARRAY"){
11722: foreach my $stored_what (@{$value}) {
1.629 banghart 11723: my $cmp2=$stored_what;
1.759 albertel 11724: if (ref($stored_what) eq 'ARRAY') {
1.746 raeburn 11725: $cmp2=join('',@{$stored_what});
1.745 raeburn 11726: }
1.629 banghart 11727: if ($cmp1 eq $cmp2) {
1.561 banghart 11728: push(@readonly_files, $file_name);
1.745 raeburn 11729: last;
1.563 banghart 11730: } elsif (!defined($what)) {
11731: push(@readonly_files, $file_name);
1.745 raeburn 11732: last;
1.561 banghart 11733: }
11734: }
1.745 raeburn 11735: }
1.561 banghart 11736: }
11737: return @readonly_files;
11738: }
1.577 banghart 11739: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561 banghart 11740:
1.577 banghart 11741: sub get_marked_as_readonly_hash {
1.745 raeburn 11742: my ($current_permissions,$group,$what) = @_;
1.577 banghart 11743: my %readonly_files;
1.745 raeburn 11744: while (my ($file_name,$value) = each(%{$current_permissions})) {
11745: if (defined($group)) {
11746: if ($file_name !~ m-^\Q$group\E/-) {
11747: next;
11748: }
11749: }
1.577 banghart 11750: if (ref($value) eq "ARRAY"){
11751: foreach my $stored_what (@{$value}) {
1.745 raeburn 11752: if (ref($stored_what) eq 'ARRAY') {
1.750 banghart 11753: foreach my $lock_descriptor(@{$stored_what}) {
11754: if ($lock_descriptor eq 'graded') {
11755: $readonly_files{$file_name} = 'graded';
11756: } elsif ($lock_descriptor eq 'handback') {
11757: $readonly_files{$file_name} = 'handback';
11758: } else {
11759: if (!exists($readonly_files{$file_name})) {
11760: $readonly_files{$file_name} = 'locked';
11761: }
11762: }
1.745 raeburn 11763: }
1.750 banghart 11764: }
1.577 banghart 11765: }
11766: }
11767: }
11768: return %readonly_files;
11769: }
1.559 banghart 11770: # ------------------------------------------------------------ Unmark as Read Only
11771:
11772: sub unmark_as_readonly {
1.629 banghart 11773: # unmarks $file_name (if $file_name is defined), or all files locked by $what
11774: # for portfolio submissions, $what contains [$symb,$crsid]
1.745 raeburn 11775: my ($domain,$user,$what,$file_name,$group) = @_;
1.759 albertel 11776: $file_name = &declutter_portfile($file_name);
1.634 albertel 11777: my $symb_crs = $what;
11778: if (ref($what)) { $symb_crs=join('',@$what); }
1.745 raeburn 11779: my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615 albertel 11780: my ($tmp)=keys(%current_permissions);
11781: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745 raeburn 11782: my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650 albertel 11783: foreach my $file (@readonly_files) {
1.759 albertel 11784: my $clean_file = &declutter_portfile($file);
11785: if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650 albertel 11786: my $current_locks = $current_permissions{$file};
1.563 banghart 11787: my @new_locks;
11788: my @del_keys;
11789: if (ref($current_locks) eq "ARRAY"){
11790: foreach my $locker (@{$current_locks}) {
1.632 albertel 11791: my $compare=$locker;
1.749 raeburn 11792: if (ref($locker) eq 'ARRAY') {
1.745 raeburn 11793: $compare=join('',@{$locker});
1.746 raeburn 11794: if ($compare ne $symb_crs) {
11795: push(@new_locks, $locker);
11796: }
1.563 banghart 11797: }
11798: }
1.650 albertel 11799: if (scalar(@new_locks) > 0) {
1.563 banghart 11800: $current_permissions{$file} = \@new_locks;
11801: } else {
11802: push(@del_keys, $file);
1.613 albertel 11803: &del('file_permissions',\@del_keys, $domain, $user);
1.650 albertel 11804: delete($current_permissions{$file});
1.563 banghart 11805: }
11806: }
1.561 banghart 11807: }
1.613 albertel 11808: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 11809: return;
11810: }
1.512 banghart 11811:
1.17 www 11812: # ------------------------------------------------------------ Directory lister
11813:
11814: sub dirlist {
1.955 raeburn 11815: my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
1.18 www 11816: $uri=~s/^\///;
11817: $uri=~s/\/$//;
1.253 stredwic 11818: my ($udom, $uname);
1.955 raeburn 11819: if ($getuserdir) {
1.253 stredwic 11820: $udom = $userdomain;
11821: $uname = $username;
1.955 raeburn 11822: } else {
11823: (undef,$udom,$uname)=split(/\//,$uri);
11824: if(defined($userdomain)) {
11825: $udom = $userdomain;
11826: }
11827: if(defined($username)) {
11828: $uname = $username;
11829: }
1.253 stredwic 11830: }
1.955 raeburn 11831: my ($dirRoot,$listing,@listing_results);
1.253 stredwic 11832:
1.955 raeburn 11833: $dirRoot = $perlvar{'lonDocRoot'};
11834: if (defined($getpropath)) {
11835: $dirRoot = &propath($udom,$uname);
1.253 stredwic 11836: $dirRoot =~ s/\/$//;
1.955 raeburn 11837: } elsif (defined($getuserdir)) {
11838: my $subdir=$uname.'__';
11839: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
11840: $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
11841: ."/$udom/$subdir/$uname";
11842: } elsif (defined($alternateRoot)) {
11843: $dirRoot = $alternateRoot;
1.751 banghart 11844: }
1.253 stredwic 11845:
11846: if($udom) {
11847: if($uname) {
1.1135 raeburn 11848: my $uhome = &homeserver($uname,$udom);
1.1136 raeburn 11849: if ($uhome eq 'no_host') {
11850: return ([],'no_host');
11851: }
1.955 raeburn 11852: $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
1.956 raeburn 11853: .$getuserdir.':'.&escape($dirRoot)
1.1135 raeburn 11854: .':'.&escape($uname).':'.&escape($udom),$uhome);
1.955 raeburn 11855: if ($listing eq 'unknown_cmd') {
1.1135 raeburn 11856: $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
1.955 raeburn 11857: } else {
11858: @listing_results = map { &unescape($_); } split(/:/,$listing);
11859: }
1.605 matthew 11860: if ($listing eq 'unknown_cmd') {
1.1135 raeburn 11861: $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
1.605 matthew 11862: @listing_results = split(/:/,$listing);
11863: } else {
11864: @listing_results = map { &unescape($_); } split(/:/,$listing);
11865: }
1.1135 raeburn 11866: if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
1.1136 raeburn 11867: ($listing eq 'rejected') || ($listing eq 'refused') ||
11868: ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
11869: return ([],$listing);
11870: } else {
11871: return (\@listing_results);
1.1135 raeburn 11872: }
1.955 raeburn 11873: } elsif(!$alternateRoot) {
1.1136 raeburn 11874: my (%allusers,%listerror);
1.841 albertel 11875: my %servers = &get_servers($udom,'library');
1.955 raeburn 11876: foreach my $tryserver (keys(%servers)) {
11877: $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
11878: &escape($udom),$tryserver);
11879: if ($listing eq 'unknown_cmd') {
11880: $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
11881: $udom, $tryserver);
11882: } else {
11883: @listing_results = map { &unescape($_); } split(/:/,$listing);
11884: }
1.841 albertel 11885: if ($listing eq 'unknown_cmd') {
11886: $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
11887: $udom, $tryserver);
11888: @listing_results = split(/:/,$listing);
11889: } else {
11890: @listing_results =
11891: map { &unescape($_); } split(/:/,$listing);
11892: }
1.1136 raeburn 11893: if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
11894: ($listing eq 'rejected') || ($listing eq 'refused') ||
11895: ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
11896: $listerror{$tryserver} = $listing;
11897: } else {
1.841 albertel 11898: foreach my $line (@listing_results) {
11899: my ($entry) = split(/&/,$line,2);
11900: $allusers{$entry} = 1;
11901: }
11902: }
1.253 stredwic 11903: }
1.1136 raeburn 11904: my @alluserslist=();
1.800 albertel 11905: foreach my $user (sort(keys(%allusers))) {
1.1136 raeburn 11906: push(@alluserslist,$user.'&user');
1.253 stredwic 11907: }
1.1172.2.80 raeburn 11908: if (!%listerror) {
11909: # no errors
11910: return (\@alluserslist);
11911: } elsif (scalar(keys(%servers)) == 1) {
11912: # one library server, one error
11913: my ($key) = keys(%listerror);
11914: return (\@alluserslist, $listerror{$key});
11915: } elsif ( grep { $_ eq 'con_lost' } values(%listerror) ) {
11916: # con_lost indicates that we might miss data from at least one
11917: # library server
11918: return (\@alluserslist, 'con_lost');
11919: } else {
11920: # multiple library servers and no con_lost -> data should be
11921: # complete.
11922: return (\@alluserslist);
11923: }
11924:
1.253 stredwic 11925: } else {
1.1136 raeburn 11926: return ([],'missing username');
1.253 stredwic 11927: }
1.955 raeburn 11928: } elsif(!defined($getpropath)) {
1.1136 raeburn 11929: my $path = $perlvar{'lonDocRoot'}.'/res/';
11930: my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
11931: return (\@all_domains);
1.955 raeburn 11932: } else {
1.1136 raeburn 11933: return ([],'missing domain');
1.275 stredwic 11934: }
11935: }
11936:
11937: # --------------------------------------------- GetFileTimestamp
11938: # This function utilizes dirlist and returns the date stamp for
11939: # when it was last modified. It will also return an error of -1
11940: # if an error occurs
11941:
11942: sub GetFileTimestamp {
1.955 raeburn 11943: my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
1.807 albertel 11944: $studentDomain = &LONCAPA::clean_domain($studentDomain);
11945: $studentName = &LONCAPA::clean_username($studentName);
1.1136 raeburn 11946: my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
11947: undef,$getuserdir);
11948: if (($error eq 'empty') || ($error eq 'no_such_dir')) {
11949: return -1;
11950: }
11951: if (ref($fileref) eq 'ARRAY') {
11952: my @stats = split('&',$fileref->[0]);
1.375 matthew 11953: # @stats contains first the filename, then the stat output
11954: return $stats[10]; # so this is 10 instead of 9.
1.275 stredwic 11955: } else {
11956: return -1;
1.253 stredwic 11957: }
1.26 www 11958: }
11959:
1.712 albertel 11960: sub stat_file {
11961: my ($uri) = @_;
1.787 albertel 11962: $uri = &clutter_with_no_wrapper($uri);
1.722 albertel 11963:
1.955 raeburn 11964: my ($udom,$uname,$file);
1.712 albertel 11965: if ($uri =~ m-^/(uploaded|editupload)/-) {
11966: ($udom,$uname,$file) =
1.811 albertel 11967: ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712 albertel 11968: $file = 'userfiles/'.$file;
11969: }
11970: if ($uri =~ m-^/res/-) {
11971: ($udom,$uname) =
1.807 albertel 11972: ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712 albertel 11973: $file = $uri;
11974: }
11975:
11976: if (!$udom || !$uname || !$file) {
11977: # unable to handle the uri
11978: return ();
11979: }
1.956 raeburn 11980: my $getpropath;
11981: if ($file =~ /^userfiles\//) {
11982: $getpropath = 1;
11983: }
1.1136 raeburn 11984: my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
11985: if (($error eq 'empty') || ($error eq 'no_such_dir')) {
11986: return ();
11987: } else {
11988: if (ref($listref) eq 'ARRAY') {
11989: my @stats = split('&',$listref->[0]);
11990: shift(@stats); #filename is first
11991: return @stats;
11992: }
1.712 albertel 11993: }
11994: return ();
11995: }
11996:
1.26 www 11997: # -------------------------------------------------------- Value of a Condition
11998:
1.713 albertel 11999: # gets the value of a specific preevaluated condition
12000: # stored in the string $env{user.state.<cid>}
12001: # or looks up a condition reference in the bighash and if if hasn't
12002: # already been evaluated recurses into docondval to get the value of
12003: # the condition, then memoizing it to
12004: # $env{user.state.<cid>.<condition>}
1.40 www 12005: sub directcondval {
12006: my $number=shift;
1.620 albertel 12007: if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555 albertel 12008: &Apache::lonuserstate::evalstate();
12009: }
1.713 albertel 12010: if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
12011: return $env{'user.state.'.$env{'request.course.id'}.".$number"};
12012: } elsif ($number =~ /^_/) {
12013: my $sub_condition;
12014: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
12015: &GDBM_READER(),0640)) {
12016: $sub_condition=$bighash{'conditions'.$number};
12017: untie(%bighash);
12018: }
12019: my $value = &docondval($sub_condition);
1.949 raeburn 12020: &appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
1.713 albertel 12021: return $value;
12022: }
1.620 albertel 12023: if ($env{'user.state.'.$env{'request.course.id'}}) {
12024: return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40 www 12025: } else {
12026: return 2;
12027: }
12028: }
12029:
1.713 albertel 12030: # get the collection of conditions for this resource
1.26 www 12031: sub condval {
12032: my $condidx=shift;
1.54 www 12033: my $allpathcond='';
1.713 albertel 12034: foreach my $cond (split(/\|/,$condidx)) {
12035: if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
12036: $allpathcond.=
12037: '('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
12038: }
1.191 harris41 12039: }
1.54 www 12040: $allpathcond=~s/\|$//;
1.713 albertel 12041: return &docondval($allpathcond);
12042: }
12043:
12044: #evaluates an expression of conditions
12045: sub docondval {
12046: my ($allpathcond) = @_;
12047: my $result=0;
12048: if ($env{'request.course.id'}
12049: && defined($allpathcond)) {
12050: my $operand='|';
12051: my @stack;
12052: foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
12053: if ($chunk eq '(') {
12054: push @stack,($operand,$result);
12055: } elsif ($chunk eq ')') {
12056: my $before=pop @stack;
12057: if (pop @stack eq '&') {
12058: $result=$result>$before?$before:$result;
12059: } else {
12060: $result=$result>$before?$result:$before;
12061: }
12062: } elsif (($chunk eq '&') || ($chunk eq '|')) {
12063: $operand=$chunk;
12064: } else {
12065: my $new=directcondval($chunk);
12066: if ($operand eq '&') {
12067: $result=$result>$new?$new:$result;
12068: } else {
12069: $result=$result>$new?$result:$new;
12070: }
12071: }
12072: }
1.26 www 12073: }
12074: return $result;
1.421 albertel 12075: }
12076:
12077: # ---------------------------------------------------- Devalidate courseresdata
12078:
12079: sub devalidatecourseresdata {
12080: my ($coursenum,$coursedomain)=@_;
12081: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 12082: &devalidate_cache_new('courseres',$hashid);
1.28 www 12083: }
12084:
1.763 www 12085:
1.200 www 12086: # --------------------------------------------------- Course Resourcedata Query
1.878 foxr 12087: #
12088: # Parameters:
12089: # $coursenum - Number of the course.
12090: # $coursedomain - Domain at which the course was created.
12091: # Returns:
12092: # A hash of the course parameters along (I think) with timestamps
12093: # and version info.
1.877 foxr 12094:
1.624 albertel 12095: sub get_courseresdata {
12096: my ($coursenum,$coursedomain)=@_;
1.200 www 12097: my $coursehom=&homeserver($coursenum,$coursedomain);
12098: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 12099: my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624 albertel 12100: my %dumpreply;
1.417 albertel 12101: unless (defined($cached)) {
1.624 albertel 12102: %dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417 albertel 12103: $result=\%dumpreply;
1.251 albertel 12104: my ($tmp) = keys(%dumpreply);
12105: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599 albertel 12106: &do_cache_new('courseres',$hashid,$result,600);
1.306 albertel 12107: } elsif ($tmp =~ /^(con_lost|no_such_host)/) {
12108: return $tmp;
1.416 albertel 12109: } elsif ($tmp =~ /^(error)/) {
1.417 albertel 12110: $result=undef;
1.599 albertel 12111: &do_cache_new('courseres',$hashid,$result,600);
1.250 albertel 12112: }
12113: }
1.624 albertel 12114: return $result;
12115: }
12116:
1.633 albertel 12117: sub devalidateuserresdata {
12118: my ($uname,$udom)=@_;
12119: my $hashid="$udom:$uname";
12120: &devalidate_cache_new('userres',$hashid);
12121: }
12122:
1.624 albertel 12123: sub get_userresdata {
12124: my ($uname,$udom)=@_;
12125: #most student don\'t have any data set, check if there is some data
12126: if (&EXT_cache_status($udom,$uname)) { return undef; }
12127:
12128: my $hashid="$udom:$uname";
12129: my ($result,$cached)=&is_cached_new('userres',$hashid);
12130: if (!defined($cached)) {
12131: my %resourcedata=&dump('resourcedata',$udom,$uname);
12132: $result=\%resourcedata;
12133: &do_cache_new('userres',$hashid,$result,600);
12134: }
12135: my ($tmp)=keys(%$result);
12136: if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
12137: return $result;
12138: }
12139: #error 2 occurs when the .db doesn't exist
12140: if ($tmp!~/error: 2 /) {
1.1172.2.71 raeburn 12141: if ((!defined($cached)) || ($tmp ne 'con_lost')) {
12142: &logthis("<font color=\"blue\">WARNING:".
12143: " Trying to get resource data for ".
12144: $uname." at ".$udom.": ".
12145: $tmp."</font>");
12146: }
1.624 albertel 12147: } elsif ($tmp=~/error: 2 /) {
1.633 albertel 12148: #&EXT_cache_set($udom,$uname);
12149: &do_cache_new('userres',$hashid,undef,600);
1.636 albertel 12150: undef($tmp); # not really an error so don't send it back
1.624 albertel 12151: }
12152: return $tmp;
12153: }
1.879 foxr 12154: #----------------------------------------------- resdata - return resource data
12155: # Purpose:
12156: # Return resource data for either users or for a course.
12157: # Parameters:
12158: # $name - Course/user name.
12159: # $domain - Name of the domain the user/course is registered on.
12160: # $type - Type of thing $name is (must be 'course' or 'user'
12161: # @which - Array of names of resources desired.
12162: # Returns:
12163: # The value of the first reasource in @which that is found in the
12164: # resource hash.
12165: # Exceptional Conditions:
12166: # If the $type passed in is not valid (not the string 'course' or
12167: # 'user', an undefined reference is returned.
12168: # If none of the resources are found, an undef is returned
1.624 albertel 12169: sub resdata {
12170: my ($name,$domain,$type,@which)=@_;
12171: my $result;
12172: if ($type eq 'course') {
12173: $result=&get_courseresdata($name,$domain);
12174: } elsif ($type eq 'user') {
12175: $result=&get_userresdata($name,$domain);
12176: }
12177: if (!ref($result)) { return $result; }
1.251 albertel 12178: foreach my $item (@which) {
1.927 albertel 12179: if (defined($result->{$item->[0]})) {
12180: return [$result->{$item->[0]},$item->[1]];
1.251 albertel 12181: }
1.250 albertel 12182: }
1.291 albertel 12183: return undef;
1.200 www 12184: }
12185:
1.1172.2.146. .1(raebu 12186:22): sub get_domain_lti {
12187:22): my ($cdom,$context) = @_;
.4(raebu 12188:22): my ($name,$cachename,%lti);
.1(raebu 12189:22): if ($context eq 'consumer') {
12190:22): $name = 'ltitools';
12191:22): } elsif ($context eq 'provider') {
12192:22): $name = 'lti';
.4(raebu 12193:22): } elsif ($context eq 'linkprot') {
12194:22): $name = 'ltisec';
.1(raebu 12195:22): } else {
12196:22): return %lti;
12197:22): }
.4(raebu 12198:22): if ($context eq 'linkprot') {
12199:22): $cachename = $context;
12200:22): } else {
12201:22): $cachename = $name;
12202:22): }
12203:22): my ($result,$cached)=&is_cached_new($cachename,$cdom);
.1(raebu 12204:22): if (defined($cached)) {
12205:22): if (ref($result) eq 'HASH') {
12206:22): %lti = %{$result};
12207:22): }
12208:22): } else {
12209:22): my %domconfig = &get_dom('configuration',[$name],$cdom);
12210:22): if (ref($domconfig{$name}) eq 'HASH') {
.4(raebu 12211:22): if ($context eq 'linkprot') {
12212:22): if (ref($domconfig{$name}{'linkprot'}) eq 'HASH') {
12213:22): %lti = %{$domconfig{$name}{'linkprot'}};
12214:22): }
12215:22): } else {
12216:22): %lti = %{$domconfig{$name}};
12217:22): }
.1(raebu 12218:22): }
12219:22): my $cachetime = 24*60*60;
.4(raebu 12220:22): &do_cache_new($cachename,$cdom,\%lti,$cachetime);
.1(raebu 12221:22): }
12222:22): return %lti;
12223:22): }
12224:22):
12225:22): sub get_course_lti {
.13(raeb 12226:-23): my ($cnum,$cdom,$context) = @_;
12227:-23): my ($name,$cachename,%lti);
12228:-23): if ($context eq 'consumer') {
12229:-23): $name = 'ltitools';
12230:-23): $cachename = 'courseltitools';
12231:-23): } elsif ($context eq 'provider') {
12232:-23): $name = 'lti';
12233:-23): $cachename = 'courselti';
12234:-23): } else {
12235:-23): return %lti;
12236:-23): }
.1(raebu 12237:22): my $hashid=$cdom.'_'.$cnum;
.13(raeb 12238:-23): my ($result,$cached)=&is_cached_new($cachename,$hashid);
.1(raebu 12239:22): if (defined($cached)) {
12240:22): if (ref($result) eq 'HASH') {
.13(raeb 12241:-23): %lti = %{$result};
.1(raebu 12242:22): }
12243:22): } else {
.13(raeb 12244:-23): %lti = &dump($name,$cdom,$cnum,undef,undef,undef,1);
.1(raebu 12245:22): my $cachetime = 24*60*60;
.13(raeb 12246:-23): &do_cache_new($cachename,$hashid,\%lti,$cachetime);
.1(raebu 12247:22): }
.13(raeb 12248:-23): return %lti;
.1(raebu 12249:22): }
12250:22):
.3(raebu 12251:22): sub courselti_itemid {
12252:22): my ($cnum,$cdom,$url,$method,$params,$context) = @_;
12253:22): my ($chome,$itemid);
12254:22): $chome = &homeserver($cnum,$cdom);
12255:22): return if ($chome eq 'no_host');
12256:22): if (ref($params) eq 'HASH') {
12257:22): my $rep;
12258:22): if (grep { $_ eq $chome } current_machine_ids()) {
12259:22): $rep = LONCAPA::Lond::crslti_itemid($cdom,$cnum,$url,$method,$params,$perlvar{'lonVersion'});
12260:22): } else {
12261:22): my $escurl = &escape($url);
12262:22): my $escmethod = &escape($method);
12263:22): my $items = &freeze_escape($params);
12264:22): $rep = &reply("encrypt:lti:$cdom:$cnum:$context:$escurl:$escmethod:$items",$chome);
12265:22): }
12266:22): unless (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
12267:22): ($rep eq 'unknown_cmd')) {
12268:22): $itemid = $rep;
12269:22): }
12270:22): }
12271:22): return $itemid;
12272:22): }
12273:22):
12274:22): sub domainlti_itemid {
12275:22): my ($cdom,$url,$method,$params,$context) = @_;
12276:22): my ($primary_id,$itemid);
12277:22): $primary_id = &domain($cdom,'primary');
12278:22): return if ($primary_id eq '');
12279:22): if (ref($params) eq 'HASH') {
12280:22): my $rep;
12281:22): if (grep { $_ eq $primary_id } current_machine_ids()) {
12282:22): $rep = LONCAPA::Lond::domlti_itemid($cdom,$context,$url,$method,$params,$perlvar{'lonVersion'});
12283:22): } else {
12284:22): my $cnum = '';
12285:22): my $escurl = &escape($url);
12286:22): my $escmethod = &escape($method);
12287:22): my $items = &freeze_escape($params);
12288:22): $rep = &reply("encrypt:lti:$cdom:$cnum:$context:$escurl:$escmethod:$items",$primary_id);
12289:22): }
12290:22): unless (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
12291:22): ($rep eq 'unknown_cmd')) {
12292:22): $itemid = $rep;
12293:22): }
12294:22): }
12295:22): return $itemid;
12296:22): }
12297:22):
.15(raeb 12298:-23): sub get_ltitools_id {
12299:-23): my ($context,$cdom,$cnum,$title) = @_;
12300:-23): my ($lockhash,$tries,$gotlock,$id,$error);
12301:-23):
12302:-23): # get lock on ltitools db
12303:-23): $lockhash = {
12304:-23): lock => $env{'user.name'}.
12305:-23): ':'.$env{'user.domain'},
12306:-23): };
12307:-23): $tries = 0;
12308:-23): if ($context eq 'domain') {
12309:-23): $gotlock = &newput_dom('ltitools',$lockhash,$cdom);
12310:-23): } else {
12311:-23): $gotlock = &newput('ltitools',$lockhash,$cdom,$cnum);
12312:-23): }
12313:-23): while (($gotlock ne 'ok') && ($tries<10)) {
12314:-23): $tries ++;
12315:-23): sleep (0.1);
12316:-23): if ($context eq 'domain') {
12317:-23): $gotlock = &newput_dom('ltitools',$lockhash,$cdom);
12318:-23): } else {
12319:-23): $gotlock = &newput('ltitools',$lockhash,$cdom,$cnum);
12320:-23): }
12321:-23): }
12322:-23): if ($gotlock eq 'ok') {
12323:-23): my %currids;
12324:-23): if ($context eq 'domain') {
12325:-23): %currids = &dump_dom('ltitools',$cdom);
12326:-23): } else {
12327:-23): %currids = &dump('ltitools',$cdom,$cnum);
12328:-23): }
12329:-23): if ($currids{'lock'}) {
12330:-23): delete($currids{'lock'});
12331:-23): if (keys(%currids)) {
12332:-23): my @curr = sort { $a <=> $b } keys(%currids);
12333:-23): if ($curr[-1] =~ /^\d+$/) {
12334:-23): $id = 1 + $curr[-1];
12335:-23): }
12336:-23): } else {
12337:-23): $id = 1;
12338:-23): }
12339:-23): if ($id) {
12340:-23): if ($context eq 'domain') {
12341:-23): unless (&newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
12342:-23): $error = 'nostore';
12343:-23): }
12344:-23): } else {
12345:-23): unless (&newput('ltitools',{ $id => $title },$cdom,$cnum) eq 'ok') {
12346:-23): $error = 'nostore';
12347:-23): }
12348:-23): }
12349:-23): } else {
12350:-23): $error = 'nonumber';
12351:-23): }
12352:-23): }
12353:-23): my $dellockoutcome;
12354:-23): if ($context eq 'domain') {
12355:-23): $dellockoutcome = &del_dom('ltitools',['lock'],$cdom);
12356:-23): } else {
12357:-23): $dellockoutcome = &del('ltitools',['lock'],$cdom,$cnum);
12358:-23): }
12359:-23): } else {
12360:-23): $error = 'nolock';
12361:-23): }
12362:-23): return ($id,$error);
12363:-23): }
12364:-23):
.9(raebu 12365:23): sub count_supptools {
12366:23): my ($cnum,$cdom,$ignorecache,$reload)=@_;
1.1172.2.31 raeburn 12367: my $hashid=$cnum.':'.$cdom;
1.1172.2.146. .9(raebu 12368:23): my ($numexttools,$cached);
1.1172.2.31 raeburn 12369: unless ($ignorecache) {
1.1172.2.146. .9(raebu 12370:23): ($numexttools,$cached) = &is_cached_new('supptools',$hashid);
1.1172.2.31 raeburn 12371: }
12372: unless (defined($cached)) {
12373: my $chome=&homeserver($cnum,$cdom);
1.1172.2.146. .9(raebu 12374:23): $numexttools = 0;
1.1172.2.31 raeburn 12375: unless ($chome eq 'no_host') {
1.1172.2.146. .9(raebu 12376:23): my ($supplemental) = &Apache::loncommon::get_supplemental($cnum,$cdom,$reload);
12377:23): if (ref($supplemental) eq 'HASH') {
12378:23): if ((ref($supplemental->{'ids'}) eq 'HASH') && (ref($supplemental->{'hidden'}) eq 'HASH')) {
12379:23): foreach my $key (keys(%{$supplemental->{'ids'}})) {
12380:23): if ($key =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
12381:23): $numexttools ++;
12382:23): }
12383:23): }
12384:23): }
12385:23): }
1.1172.2.31 raeburn 12386: }
1.1172.2.146. .9(raebu 12387:23): &do_cache_new('supptools',$hashid,$numexttools,600);
1.1172.2.31 raeburn 12388: }
1.1172.2.146. .9(raebu 12389:23): return $numexttools;
12390:23): }
12391:23):
12392:23): sub has_unhidden_suppfiles {
12393:23): my ($cnum,$cdom,$ignorecache,$possdel)=@_;
12394:23): my $hashid=$cnum.':'.$cdom;
12395:23): my ($showsupp,$cached);
12396:23): unless ($ignorecache) {
12397:23): ($showsupp,$cached) = &is_cached_new('showsupp',$hashid);
12398:23): }
12399:23): unless (defined($cached)) {
12400:23): my $chome=&homeserver($cnum,$cdom);
12401:23): unless ($chome eq 'no_host') {
12402:23): my ($supplemental) = &Apache::loncommon::get_supplemental($cnum,$cdom,$ignorecache,$possdel);
12403:23): if (ref($supplemental) eq 'HASH') {
12404:23): if ((ref($supplemental->{'ids'}) eq 'HASH') && (ref($supplemental->{'hidden'}) eq 'HASH')) {
12405:23): foreach my $key (keys(%{$supplemental->{'ids'}})) {
12406:23): next if ($key =~ /\.sequence$/);
12407:23): if (ref($supplemental->{'ids'}->{$key}) eq 'ARRAY') {
12408:23): foreach my $id (@{$supplemental->{'ids'}->{$key}}) {
12409:23): unless ($supplemental->{'hidden'}->{$id}) {
12410:23): $showsupp = 1;
12411:23): last;
12412:23): }
12413:23): }
12414:23): }
12415:23): last if ($showsupp);
12416:23): }
12417:23): }
12418:23): }
12419:23): }
12420:23): &do_cache_new('showsupp',$hashid,$showsupp,600);
12421:23): }
12422:23): return $showsupp;
1.1172.2.31 raeburn 12423: }
12424:
1.379 matthew 12425: #
12426: # EXT resource caching routines
12427: #
12428:
1.1172.2.146. .1(raebu 12429:22): {
12430:22): # Cache (5 seconds) of map hierarchy for speedup of navmaps display
12431:22): #
12432:22): # The course for which we cache
12433:22): my $cachedmapkey='';
12434:22): # The cached recursive maps for this course
12435:22): my %cachedmaps=();
12436:22): # When this was last done
12437:22): my $cachedmaptime='';
12438:22):
1.379 matthew 12439: sub clear_EXT_cache_status {
1.383 albertel 12440: &delenv('cache.EXT.');
1.379 matthew 12441: }
12442:
12443: sub EXT_cache_status {
12444: my ($target_domain,$target_user) = @_;
1.383 albertel 12445: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620 albertel 12446: if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379 matthew 12447: # We know already the user has no data
12448: return 1;
12449: } else {
12450: return 0;
12451: }
12452: }
12453:
12454: sub EXT_cache_set {
12455: my ($target_domain,$target_user) = @_;
1.383 albertel 12456: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.949 raeburn 12457: #&appenv({$cachename => time});
1.379 matthew 12458: }
12459:
1.28 www 12460: # --------------------------------------------------------- Value of a Variable
1.58 www 12461: sub EXT {
1.715 albertel 12462:
1.1172.2.28 raeburn 12463: my ($varname,$symbparm,$udom,$uname,$usection,$recurse,$cid)=@_;
1.68 www 12464: unless ($varname) { return ''; }
1.218 albertel 12465: #get real user name/domain, courseid and symb
12466: my $courseid;
1.359 albertel 12467: my $publicuser;
1.427 www 12468: if ($symbparm) {
12469: $symbparm=&get_symb_from_alias($symbparm);
12470: }
1.218 albertel 12471: if (!($uname && $udom)) {
1.790 albertel 12472: (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218 albertel 12473: if (!$symbparm) { $symbparm=$cursymb; }
12474: } else {
1.620 albertel 12475: $courseid=$env{'request.course.id'};
1.218 albertel 12476: }
1.48 www 12477: my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
12478: my $rest;
1.320 albertel 12479: if (defined($therest[0])) {
1.48 www 12480: $rest=join('.',@therest);
12481: } else {
12482: $rest='';
12483: }
1.320 albertel 12484:
1.57 www 12485: my $qualifierrest=$qualifier;
12486: if ($rest) { $qualifierrest.='.'.$rest; }
12487: my $spacequalifierrest=$space;
12488: if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28 www 12489: if ($realm eq 'user') {
1.48 www 12490: # --------------------------------------------------------------- user.resource
12491: if ($space eq 'resource') {
1.651 albertel 12492: if ( (defined($Apache::lonhomework::parsing_a_problem)
12493: || defined($Apache::lonhomework::parsing_a_task))
12494: &&
1.1172.2.142 raeburn 12495: ($symbparm eq &symbread()) ) {
1.744 albertel 12496: # if we are in the middle of processing the resource the
12497: # get the value we are planning on committing
12498: if (defined($Apache::lonhomework::results{$qualifierrest})) {
12499: return $Apache::lonhomework::results{$qualifierrest};
12500: } else {
12501: return $Apache::lonhomework::history{$qualifierrest};
12502: }
1.335 albertel 12503: } else {
1.359 albertel 12504: my %restored;
1.620 albertel 12505: if ($publicuser || $env{'request.state'} eq 'construct') {
1.359 albertel 12506: %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
12507: } else {
12508: %restored=&restore($symbparm,$courseid,$udom,$uname);
12509: }
1.335 albertel 12510: return $restored{$qualifierrest};
12511: }
1.48 www 12512: # ----------------------------------------------------------------- user.access
12513: } elsif ($space eq 'access') {
1.218 albertel 12514: # FIXME - not supporting calls for a specific user
1.48 www 12515: return &allowed($qualifier,$rest);
12516: # ------------------------------------------ user.preferences, user.environment
12517: } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620 albertel 12518: if (($uname eq $env{'user.name'}) &&
12519: ($udom eq $env{'user.domain'})) {
12520: return $env{join('.',('environment',$qualifierrest))};
1.218 albertel 12521: } else {
1.359 albertel 12522: my %returnhash;
12523: if (!$publicuser) {
12524: %returnhash=&userenvironment($udom,$uname,
12525: $qualifierrest);
12526: }
1.218 albertel 12527: return $returnhash{$qualifierrest};
12528: }
1.48 www 12529: # ----------------------------------------------------------------- user.course
12530: } elsif ($space eq 'course') {
1.218 albertel 12531: # FIXME - not supporting calls for a specific user
1.620 albertel 12532: return $env{join('.',('request.course',$qualifier))};
1.48 www 12533: # ------------------------------------------------------------------- user.role
12534: } elsif ($space eq 'role') {
1.218 albertel 12535: # FIXME - not supporting calls for a specific user
1.620 albertel 12536: my ($role,$where)=split(/\./,$env{'request.role'});
1.48 www 12537: if ($qualifier eq 'value') {
12538: return $role;
12539: } elsif ($qualifier eq 'extent') {
12540: return $where;
12541: }
12542: # ----------------------------------------------------------------- user.domain
12543: } elsif ($space eq 'domain') {
1.218 albertel 12544: return $udom;
1.48 www 12545: # ------------------------------------------------------------------- user.name
12546: } elsif ($space eq 'name') {
1.218 albertel 12547: return $uname;
1.48 www 12548: # ---------------------------------------------------- Any other user namespace
1.29 www 12549: } else {
1.359 albertel 12550: my %reply;
12551: if (!$publicuser) {
12552: %reply=&get($space,[$qualifierrest],$udom,$uname);
12553: }
12554: return $reply{$qualifierrest};
1.48 www 12555: }
1.236 www 12556: } elsif ($realm eq 'query') {
12557: # ---------------------------------------------- pull stuff out of query string
1.384 albertel 12558: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
12559: [$spacequalifierrest]);
1.620 albertel 12560: return $env{'form.'.$spacequalifierrest};
1.236 www 12561: } elsif ($realm eq 'request') {
1.48 www 12562: # ------------------------------------------------------------- request.browser
12563: if ($space eq 'browser') {
1.1145 bisitz 12564: return $env{'browser.'.$qualifier};
1.57 www 12565: # ------------------------------------------------------------ request.filename
12566: } else {
1.620 albertel 12567: return $env{'request.'.$spacequalifierrest};
1.29 www 12568: }
1.28 www 12569: } elsif ($realm eq 'course') {
1.48 www 12570: # ---------------------------------------------------------- course.description
1.620 albertel 12571: return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57 www 12572: } elsif ($realm eq 'resource') {
1.165 www 12573:
1.620 albertel 12574: if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539 albertel 12575: if (!$symbparm) { $symbparm=&symbread(); }
12576: }
1.693 albertel 12577:
1.1172.2.30 raeburn 12578: if ($qualifier eq '') {
12579: if ($space eq 'title') {
12580: if (!$symbparm) { $symbparm = $env{'request.filename'}; }
12581: return &gettitle($symbparm);
12582: }
1.693 albertel 12583:
1.1172.2.30 raeburn 12584: if ($space eq 'map') {
12585: my ($map) = &decode_symb($symbparm);
12586: return &symbread($map);
12587: }
12588: if ($space eq 'maptitle') {
12589: my ($map) = &decode_symb($symbparm);
12590: return &gettitle($map);
12591: }
12592: if ($space eq 'filename') {
12593: if ($symbparm) {
12594: return &clutter((&decode_symb($symbparm))[2]);
12595: }
12596: return &hreflocation('',$env{'request.filename'});
1.905 albertel 12597: }
1.1172.2.30 raeburn 12598:
12599: if ((defined($courseid)) && ($courseid eq $env{'request.course.id'}) && $symbparm) {
12600: if ($space eq 'visibleparts') {
12601: my $navmap = Apache::lonnavmaps::navmap->new();
12602: my $item;
12603: if (ref($navmap)) {
12604: my $res = $navmap->getBySymb($symbparm);
12605: my $parts = $res->parts();
12606: if (ref($parts) eq 'ARRAY') {
12607: $item = join(',',@{$parts});
12608: }
12609: undef($navmap);
12610: }
12611: return $item;
12612: }
12613: }
12614: }
1.693 albertel 12615:
12616: my ($section, $group, @groups);
1.593 albertel 12617: my ($courselevelm,$courselevel);
1.1172.2.28 raeburn 12618: if (($courseid eq '') && ($cid)) {
12619: $courseid = $cid;
12620: }
12621: if (($symbparm && $courseid) &&
12622: (($courseid eq $env{'request.course.id'}) || ($courseid eq $cid))) {
1.165 www 12623:
1.218 albertel 12624: #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165 www 12625:
1.60 www 12626: # ----------------------------------------------------- Cascading lookup scheme
1.218 albertel 12627: my $symbp=$symbparm;
1.735 albertel 12628: my $mapp=&deversion((&decode_symb($symbp))[0]);
1.218 albertel 12629:
12630: my $symbparm=$symbp.'.'.$spacequalifierrest;
12631: my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
12632:
1.620 albertel 12633: if (($env{'user.name'} eq $uname) &&
12634: ($env{'user.domain'} eq $udom)) {
12635: $section=$env{'request.course.sec'};
1.733 raeburn 12636: @groups = split(/:/,$env{'request.course.groups'});
12637: @groups=&sort_course_groups($courseid,@groups);
1.218 albertel 12638: } else {
1.539 albertel 12639: if (! defined($usection)) {
1.551 albertel 12640: $section=&getsection($udom,$uname,$courseid);
1.539 albertel 12641: } else {
12642: $section = $usection;
12643: }
1.733 raeburn 12644: @groups = &get_users_groups($udom,$uname,$courseid);
1.218 albertel 12645: }
12646:
12647: my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
12648: my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
12649: my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
12650:
1.593 albertel 12651: $courselevel=$courseid.'.'.$spacequalifierrest;
1.218 albertel 12652: my $courselevelr=$courseid.'.'.$symbparm;
1.593 albertel 12653: $courselevelm=$courseid.'.'.$mapparm;
1.69 www 12654:
1.60 www 12655: # ----------------------------------------------------------- first, check user
1.624 albertel 12656:
12657: my $userreply=&resdata($uname,$udom,'user',
1.927 albertel 12658: ([$courselevelr,'resource'],
12659: [$courselevelm,'map' ],
12660: [$courselevel, 'course' ]));
1.931 albertel 12661: if (defined($userreply)) { return &get_reply($userreply); }
1.95 www 12662:
1.594 albertel 12663: # ------------------------------------------------ second, check some of course
1.684 raeburn 12664: my $coursereply;
1.691 raeburn 12665: if (@groups > 0) {
12666: $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
12667: $mapparm,$spacequalifierrest);
1.927 albertel 12668: if (defined($coursereply)) { return &get_reply($coursereply); }
1.684 raeburn 12669: }
1.96 www 12670:
1.684 raeburn 12671: $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.927 albertel 12672: $env{'course.'.$courseid.'.domain'},
12673: 'course',
12674: ([$seclevelr, 'resource'],
12675: [$seclevelm, 'map' ],
12676: [$seclevel, 'course' ],
12677: [$courselevelr,'resource']));
12678: if (defined($coursereply)) { return &get_reply($coursereply); }
1.200 www 12679:
1.60 www 12680: # ------------------------------------------------------ third, check map parms
1.218 albertel 12681: my %parmhash=();
12682: my $thisparm='';
12683: if (tie(%parmhash,'GDBM_File',
1.620 albertel 12684: $env{'request.course.fn'}.'_parms.db',
1.256 albertel 12685: &GDBM_READER(),0640)) {
1.218 albertel 12686: $thisparm=$parmhash{$symbparm};
12687: untie(%parmhash);
12688: }
1.927 albertel 12689: if ($thisparm) { return &get_reply([$thisparm,'resource']); }
1.218 albertel 12690: }
1.594 albertel 12691: # ------------------------------------------ fourth, look in resource metadata
1.71 www 12692:
1.1172.2.146. .13(raeb 12693:-23): my $what = $spacequalifierrest;
12694:-23): $what=~s/\./\_/;
12695:-23): my $filename;
1.282 albertel 12696: if (!$symbparm) { $symbparm=&symbread(); }
12697: if ($symbparm) {
1.409 www 12698: $filename=(&decode_symb($symbparm))[2];
1.282 albertel 12699: } else {
1.620 albertel 12700: $filename=$env{'request.filename'};
1.282 albertel 12701: }
1.1172.2.146. .13(raeb 12702:-23): my $toolsymb;
12703:-23): if (($filename =~ /ext\.tool$/) && ($what ne '0_gradable')) {
12704:-23): $toolsymb = $symbparm;
12705:-23): }
12706:-23): my $metadata=&metadata($filename,$what,$toolsymb);
1.927 albertel 12707: if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.1172.2.146. .13(raeb 12708:-23): $metadata=&metadata($filename,'parameter_'.$what,$toolsymb);
1.927 albertel 12709: if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.142 www 12710:
1.1172.2.146. .13(raeb 12711:-23): # ----------------------------------------------- fifth, look in rest of course
1.593 albertel 12712: if ($symbparm && defined($courseid) &&
1.620 albertel 12713: $courseid eq $env{'request.course.id'}) {
1.624 albertel 12714: my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
12715: $env{'course.'.$courseid.'.domain'},
12716: 'course',
1.927 albertel 12717: ([$courselevelm,'map' ],
12718: [$courselevel, 'course']));
12719: if (defined($coursereply)) { return &get_reply($coursereply); }
1.593 albertel 12720: }
1.145 www 12721: # ------------------------------------------------------------------ Cascade up
1.218 albertel 12722: unless ($space eq '0') {
1.336 albertel 12723: my @parts=split(/_/,$space);
12724: my $id=pop(@parts);
12725: my $part=join('_',@parts);
12726: if ($part eq '') { $part='0'; }
1.927 albertel 12727: my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395 albertel 12728: $symbparm,$udom,$uname,$section,1);
1.938 raeburn 12729: if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
1.218 albertel 12730: }
1.395 albertel 12731: if ($recurse) { return undef; }
1.1172.2.146. .13(raeb 12732:-23): my $pack_def=&packages_tab_default($filename,$varname,$toolsymb);
1.927 albertel 12733: if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
1.48 www 12734: # ---------------------------------------------------- Any other user namespace
12735: } elsif ($realm eq 'environment') {
12736: # ----------------------------------------------------------------- environment
1.620 albertel 12737: if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
12738: return $env{'environment.'.$spacequalifierrest};
1.219 albertel 12739: } else {
1.770 albertel 12740: if ($uname eq 'anonymous' && $udom eq '') {
12741: return '';
12742: }
1.219 albertel 12743: my %returnhash=&userenvironment($udom,$uname,
12744: $spacequalifierrest);
12745: return $returnhash{$spacequalifierrest};
12746: }
1.28 www 12747: } elsif ($realm eq 'system') {
1.48 www 12748: # ----------------------------------------------------------------- system.time
12749: if ($space eq 'time') {
12750: return time;
12751: }
1.696 albertel 12752: } elsif ($realm eq 'server') {
12753: # ----------------------------------------------------------------- system.time
12754: if ($space eq 'name') {
12755: return $ENV{'SERVER_NAME'};
12756: }
1.1172.2.146. .1(raebu 12757:22): } elsif ($realm eq 'client') {
12758:22): if ($space eq 'remote_addr') {
12759:22): return &get_requestor_ip();
12760:22): }
1.28 www 12761: }
1.48 www 12762: return '';
1.61 www 12763: }
12764:
1.927 albertel 12765: sub get_reply {
12766: my ($reply_value) = @_;
1.940 raeburn 12767: if (ref($reply_value) eq 'ARRAY') {
12768: if (wantarray) {
12769: return @$reply_value;
12770: }
12771: return $reply_value->[0];
12772: } else {
12773: return $reply_value;
1.927 albertel 12774: }
12775: }
12776:
1.691 raeburn 12777: sub check_group_parms {
12778: my ($courseid,$groups,$symbparm,$mapparm,$what) = @_;
12779: my @groupitems = ();
12780: my $resultitem;
1.927 albertel 12781: my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$what,'course']);
1.691 raeburn 12782: foreach my $group (@{$groups}) {
12783: foreach my $level (@levels) {
1.927 albertel 12784: my $item = $courseid.'.['.$group.'].'.$level->[0];
12785: push(@groupitems,[$item,$level->[1]]);
1.691 raeburn 12786: }
12787: }
12788: my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
12789: $env{'course.'.$courseid.'.domain'},
12790: 'course',@groupitems);
12791: return $coursereply;
12792: }
12793:
1.1172.2.146. .1(raebu 12794:22): sub get_map_hierarchy {
12795:22): my ($mapname,$courseid) = @_;
12796:22): my @recurseup = ();
12797:22): if ($mapname) {
12798:22): if (($cachedmapkey eq $courseid) &&
12799:22): (abs($cachedmaptime-time)<5)) {
12800:22): if (ref($cachedmaps{$mapname}) eq 'ARRAY') {
12801:22): return @{$cachedmaps{$mapname}};
12802:22): }
12803:22): }
12804:22): my $navmap = Apache::lonnavmaps::navmap->new();
12805:22): if (ref($navmap)) {
12806:22): @recurseup = $navmap->recurseup_maps($mapname);
12807:22): undef($navmap);
12808:22): $cachedmaps{$mapname} = \@recurseup;
12809:22): $cachedmaptime=time;
12810:22): $cachedmapkey=$courseid;
12811:22): }
12812:22): }
12813:22): return @recurseup;
12814:22): }
12815:22):
.2(raebu 12816:22): }
.1(raebu 12817:22):
1.691 raeburn 12818: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733 raeburn 12819: my ($courseid,@groups) = @_;
12820: @groups = sort(@groups);
1.691 raeburn 12821: return @groups;
12822: }
12823:
1.395 albertel 12824: sub packages_tab_default {
1.1172.2.146. .13(raeb 12825:-23): my ($uri,$varname,$toolsymb)=@_;
1.395 albertel 12826: my (undef,$part,$name)=split(/\./,$varname);
1.738 albertel 12827:
12828: my (@extension,@specifics,$do_default);
1.1172.2.146. .13(raeb 12829:-23): foreach my $package (split(/,/,&metadata($uri,'packages',$toolsymb))) {
1.395 albertel 12830: my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738 albertel 12831: if ($pack_type eq 'default') {
12832: $do_default=1;
12833: } elsif ($pack_type eq 'extension') {
12834: push(@extension,[$package,$pack_type,$pack_part]);
1.885 albertel 12835: } elsif ($pack_part eq $part || $pack_type eq 'part') {
1.848 albertel 12836: # only look at packages defaults for packages that this id is
1.738 albertel 12837: push(@specifics,[$package,$pack_type,$pack_part]);
12838: }
12839: }
12840: # first look for a package that matches the requested part id
12841: foreach my $package (@specifics) {
12842: my (undef,$pack_type,$pack_part)=@{$package};
12843: next if ($pack_part ne $part);
12844: if (defined($packagetab{"$pack_type&$name&default"})) {
12845: return $packagetab{"$pack_type&$name&default"};
12846: }
12847: }
12848: # look for any possible matching non extension_ package
12849: foreach my $package (@specifics) {
12850: my (undef,$pack_type,$pack_part)=@{$package};
1.468 albertel 12851: if (defined($packagetab{"$pack_type&$name&default"})) {
12852: return $packagetab{"$pack_type&$name&default"};
12853: }
1.585 albertel 12854: if ($pack_type eq 'part') { $pack_part='0'; }
1.468 albertel 12855: if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
12856: return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395 albertel 12857: }
12858: }
1.738 albertel 12859: # look for any posible extension_ match
12860: foreach my $package (@extension) {
12861: my ($package,$pack_type)=@{$package};
12862: if (defined($packagetab{"$pack_type&$name&default"})) {
12863: return $packagetab{"$pack_type&$name&default"};
12864: }
12865: if (defined($packagetab{$package."&$name&default"})) {
12866: return $packagetab{$package."&$name&default"};
12867: }
12868: }
12869: # look for a global default setting
12870: if ($do_default && defined($packagetab{"default&$name&default"})) {
12871: return $packagetab{"default&$name&default"};
12872: }
1.395 albertel 12873: return undef;
12874: }
12875:
1.334 albertel 12876: sub add_prefix_and_part {
12877: my ($prefix,$part)=@_;
12878: my $keyroot;
12879: if (defined($prefix) && $prefix !~ /^__/) {
12880: # prefix that has a part already
12881: $keyroot=$prefix;
12882: } elsif (defined($prefix)) {
12883: # prefix that is missing a part
12884: if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
12885: } else {
12886: # no prefix at all
12887: if (defined($part)) { $keyroot='_'.$part; }
12888: }
12889: return $keyroot;
12890: }
12891:
1.71 www 12892: # ---------------------------------------------------------------- Get metadata
12893:
1.599 albertel 12894: my %metaentry;
1.1070 www 12895: my %importedpartids;
1.1172.2.99 raeburn 12896: my %importedrespids;
1.71 www 12897: sub metadata {
1.1172.2.146. .13(raeb 12898:-23): my ($uri,$what,$toolsymb,$liburi,$prefix,$depthcount)=@_;
1.71 www 12899: $uri=&declutter($uri);
1.288 albertel 12900: # if it is a non metadata possible uri return quickly
1.529 albertel 12901: if (($uri eq '') ||
12902: (($uri =~ m|^/*adm/|) &&
1.1172.2.146. .1(raebu 12903:22): ($uri !~ m|^adm/includes|) && ($uri !~ m{/(smppg|bulletinboard|ext\.tool)$})) ||
1.1108 raeburn 12904: ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
1.924 albertel 12905: return undef;
12906: }
1.1172.2.49 raeburn 12907: if (($uri =~ /^priv/ || $uri=~m{^home/httpd/html/priv})
1.924 albertel 12908: && &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
1.468 albertel 12909: return undef;
1.288 albertel 12910: }
1.73 www 12911: my $filename=$uri;
12912: $uri=~s/\.meta$//;
1.172 www 12913: #
12914: # Is the metadata already cached?
1.177 www 12915: # Look at timestamp of caching
1.172 www 12916: # Everything is cached by the main uri, libraries are never directly cached
12917: #
1.428 albertel 12918: if (!defined($liburi)) {
1.599 albertel 12919: my ($result,$cached)=&is_cached_new('meta',$uri);
1.428 albertel 12920: if (defined($cached)) { return $result->{':'.$what}; }
12921: }
1.1172.2.146. .13(raeb 12922:-23):
12923:-23): #
12924:-23): # If the uri is for an external tool the file from
12925:-23): # which metadata should be retrieved depends on whether
12926:-23): # the tool had been configured to be gradable (set in the Course
12927:-23): # Editor or Resource Editor).
12928:-23): #
12929:-23): # If a valid symb has been included as the third arg in the call
12930:-23): # to &metadata() that can be used to retrieve the value of
12931:-23): # parameter_0_gradable set for the resource, and included in the
12932:-23): # uploaded map containing the tool. The value is retrieved via
12933:-23): # &EXT(), if a valid symb is available. Otherwise the value of
12934:-23): # gradable in the exttool_$marker.db file for the tool instance
12935:-23): # is retrieved via &get().
12936:-23): #
12937:-23): # When lonuserstate::traceroute() calls lonnet::EXT() for
12938:-23): # hiddenresource and encrypturl (during course initialization)
12939:-23): # the map-level parameter for resource.0.gradable included in the
12940:-23): # uploaded map containing the tool will not yet have been stored
12941:-23): # in the user_course_parms.db file for the user's session, so in
12942:-23): # this case fall back to retrieving gradable status from the
12943:-23): # exttool_$marker.db file.
12944:-23): #
12945:-23): # In order to avoid an infinite loop, &metadata() will return
12946:-23): # before a call to &EXT(), if the uri is for an external tool
12947:-23): # and the $what for which metadata is being requested is
12948:-23): # parameter_0_gradable or 0_gradable.
12949:-23): #
12950:-23):
12951:-23): if ($uri =~ /ext\.tool$/) {
12952:-23): if (($what eq 'parameter_0_gradable') || ($what eq '0_gradable')) {
12953:-23): return;
12954:-23): } else {
12955:-23): my ($checked,$use_passback);
12956:-23): if ($toolsymb ne '') {
12957:-23): (undef,undef,my $tooluri) = &decode_symb($toolsymb);
12958:-23): if (($tooluri eq $uri) && (&EXT('resource.0.gradable',$toolsymb))) {
12959:-23): $checked = 1;
12960:-23): if (&EXT('resource.0.gradable',$toolsymb) =~ /^yes$/i) {
12961:-23): $use_passback = 1;
12962:-23): }
12963:-23): }
12964:-23): }
12965:-23): unless ($checked) {
12966:-23): my ($ignore,$cdom,$cnum,$marker) = split(m{/},$uri);
12967:-23): $marker=~s/\D//g;
12968:-23): if ($marker) {
12969:-23): my %toolsettings=&get('exttool_'.$marker,['gradable'],$cdom,$cnum);
12970:-23): $use_passback = $toolsettings{'gradable'};
12971:-23): }
12972:-23): }
12973:-23): if ($use_passback) {
12974:-23): $filename = '/home/httpd/html/res/lib/templates/LTIpassback.tool';
12975:-23): } else {
12976:-23): $filename = '/home/httpd/html/res/lib/templates/LTIstandard.tool';
12977:-23): }
12978:-23): }
12979:-23): }
12980:-23):
1.428 albertel 12981: {
1.1069 www 12982: # Imported parts would go here
1.1172.2.99 raeburn 12983: my @origfiletagids=();
1.1069 www 12984: my $importedparts=0;
1.1172.2.99 raeburn 12985:
12986: # Imported responseids would go here
12987: my $importedresponses=0;
1.172 www 12988: #
12989: # Is this a recursive call for a library?
12990: #
1.599 albertel 12991: # if (! exists($metacache{$uri})) {
12992: # $metacache{$uri}={};
12993: # }
1.924 albertel 12994: my $cachetime = 60*60;
1.171 www 12995: if ($liburi) {
12996: $liburi=&declutter($liburi);
12997: $filename=$liburi;
1.401 bowersj2 12998: } else {
1.599 albertel 12999: &devalidate_cache_new('meta',$uri);
13000: undef(%metaentry);
1.401 bowersj2 13001: }
1.140 www 13002: my %metathesekeys=();
1.73 www 13003: unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489 albertel 13004: my $metastring;
1.1140 www 13005: if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
1.929 albertel 13006: my $which = &hreflocation('','/'.($liburi || $uri));
1.924 albertel 13007: $metastring =
1.929 albertel 13008: &Apache::lonnet::ssi_body($which,
1.924 albertel 13009: ('grade_target' => 'meta'));
13010: $cachetime = 1; # only want this cached in the child not long term
1.1108 raeburn 13011: } elsif (($uri !~ m -^(editupload)/-) &&
13012: ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
1.543 albertel 13013: my $file=&filelocation('',&clutter($filename));
1.599 albertel 13014: #push(@{$metaentry{$uri.'.file'}},$file);
1.543 albertel 13015: $metastring=&getfile($file);
1.489 albertel 13016: }
1.208 albertel 13017: my $parser=HTML::LCParser->new(\$metastring);
1.71 www 13018: my $token;
1.140 www 13019: undef %metathesekeys;
1.71 www 13020: while ($token=$parser->get_token) {
1.339 albertel 13021: if ($token->[0] eq 'S') {
13022: if (defined($token->[2]->{'package'})) {
1.172 www 13023: #
13024: # This is a package - get package info
13025: #
1.339 albertel 13026: my $package=$token->[2]->{'package'};
13027: my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
13028: if (defined($token->[2]->{'id'})) {
13029: $keyroot.='_'.$token->[2]->{'id'};
13030: }
1.599 albertel 13031: if ($metaentry{':packages'}) {
13032: $metaentry{':packages'}.=','.$package.$keyroot;
1.339 albertel 13033: } else {
1.599 albertel 13034: $metaentry{':packages'}=$package.$keyroot;
1.339 albertel 13035: }
1.736 albertel 13036: foreach my $pack_entry (keys(%packagetab)) {
1.432 albertel 13037: my $part=$keyroot;
13038: $part=~s/^\_//;
1.736 albertel 13039: if ($pack_entry=~/^\Q$package\E\&/ ||
13040: $pack_entry=~/^\Q$package\E_0\&/) {
13041: my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395 albertel 13042: # ignore package.tab specified default values
13043: # here &package_tab_default() will fetch those
13044: if ($subp eq 'default') { next; }
1.736 albertel 13045: my $value=$packagetab{$pack_entry};
1.432 albertel 13046: my $unikey;
13047: if ($pack =~ /_0$/) {
13048: $unikey='parameter_0_'.$name;
13049: $part=0;
13050: } else {
13051: $unikey='parameter'.$keyroot.'_'.$name;
13052: }
1.339 albertel 13053: if ($subp eq 'display') {
13054: $value.=' [Part: '.$part.']';
13055: }
1.599 albertel 13056: $metaentry{':'.$unikey.'.part'}=$part;
1.395 albertel 13057: $metathesekeys{$unikey}=1;
1.599 albertel 13058: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
13059: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.339 albertel 13060: }
1.599 albertel 13061: if (defined($metaentry{':'.$unikey.'.default'})) {
13062: $metaentry{':'.$unikey}=
13063: $metaentry{':'.$unikey.'.default'};
1.356 albertel 13064: }
1.339 albertel 13065: }
13066: }
13067: } else {
1.172 www 13068: #
13069: # This is not a package - some other kind of start tag
1.339 albertel 13070: #
13071: my $entry=$token->[1];
1.1068 www 13072: my $unikey='';
1.175 www 13073:
1.339 albertel 13074: if ($entry eq 'import') {
1.175 www 13075: #
13076: # Importing a library here
1.339 albertel 13077: #
1.1067 www 13078: my $location=$parser->get_text('/import');
13079: my $dir=$filename;
13080: $dir=~s|[^/]*$||;
13081: $location=&filelocation($dir,$location);
1.1172.2.99 raeburn 13082:
13083: my $importid=$token->[2]->{'id'};
1.1068 www 13084: my $importmode=$token->[2]->{'importmode'};
1.1172.2.99 raeburn 13085: #
13086: # Check metadata for imported file to
13087: # see if it contained response items
13088: #
13089: my %currmetaentry = %metaentry;
13090: my $libresponseorder = &metadata($location,'responseorder');
13091: my $origfile;
13092: if ($libresponseorder ne '') {
13093: if ($#origfiletagids<0) {
13094: undef(%importedrespids);
13095: undef(%importedpartids);
13096: }
13097: @{$importedrespids{$importid}} = split(/\s*,\s*/,$libresponseorder);
13098: if (@{$importedrespids{$importid}} > 0) {
13099: $importedresponses = 1;
13100: # We need to get the original file and the imported file to get the response order correct
13101: # Load and inspect original file
13102: if ($#origfiletagids<0) {
13103: my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
13104: $origfile=&getfile($origfilelocation);
13105: @origfiletagids=($origfile=~/<((?:\w+)response|import|part)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
13106: }
13107: }
13108: }
13109: # Do not overwrite contents of %metaentry hash for resource itself with
13110: # hash populated for imported library file
13111: %metaentry = %currmetaentry;
13112: undef(%currmetaentry);
1.1068 www 13113: if ($importmode eq 'problem') {
1.1069 www 13114: # Import as problem/response
1.1068 www 13115: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
13116: } elsif ($importmode eq 'part') {
13117: # Import as part(s)
1.1069 www 13118: $importedparts=1;
13119: # We need to get the original file and the imported file to get the part order correct
13120: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
1.1172.2.99 raeburn 13121: # Load and inspect original file if we didn't do that already
13122: if ($#origfiletagids<0) {
13123: undef(%importedrespids);
13124: undef(%importedpartids);
13125: if ($origfile eq '') {
13126: my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
13127: $origfile=&getfile($origfilelocation);
13128: @origfiletagids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
13129: }
1.1070 www 13130: }
13131:
1.1069 www 13132: # Load and inspect imported file
13133: my $impfile=&getfile($location);
13134: my @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
13135: if ($#impfilepartids>=0) {
13136: # This problem had parts
1.1070 www 13137: $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
1.1069 www 13138: } else {
13139: # Importing by turning a single problem into a problem part
13140: # It gets the import-tags ID as part-ID
13141: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
1.1070 www 13142: $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
1.1069 www 13143: }
1.1068 www 13144: } else {
13145: # Normal import
13146: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
13147: if (defined($token->[2]->{'id'})) {
13148: $unikey.='_'.$token->[2]->{'id'};
13149: }
1.1067 www 13150: }
13151:
1.339 albertel 13152: if ($depthcount<20) {
1.736 albertel 13153: my $metadata =
1.1172.2.146. .13(raeb 13154:-23): &metadata($uri,'keys',$toolsymb,$location,$unikey,
1.736 albertel 13155: $depthcount+1);
13156: foreach my $meta (split(',',$metadata)) {
13157: $metaentry{':'.$meta}=$metaentry{':'.$meta};
13158: $metathesekeys{$meta}=1;
1.339 albertel 13159: }
1.1068 www 13160:
13161: }
1.1067 www 13162: } else {
13163: #
13164: # Not importing, some other kind of non-package, non-library start tag
13165: #
13166: $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
13167: if (defined($token->[2]->{'id'})) {
13168: $unikey.='_'.$token->[2]->{'id'};
13169: }
1.339 albertel 13170: if (defined($token->[2]->{'name'})) {
13171: $unikey.='_'.$token->[2]->{'name'};
13172: }
13173: $metathesekeys{$unikey}=1;
1.736 albertel 13174: foreach my $param (@{$token->[3]}) {
13175: $metaentry{':'.$unikey.'.'.$param} =
13176: $token->[2]->{$param};
1.339 albertel 13177: }
13178: my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599 albertel 13179: my $default=$metaentry{':'.$unikey.'.default'};
1.339 albertel 13180: if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
13181: # only ws inside the tag, and not in default, so use default
13182: # as value
1.599 albertel 13183: $metaentry{':'.$unikey}=$default;
1.908 albertel 13184: } elsif ( $internaltext =~ /\S/ ) {
13185: # something interesting inside the tag
13186: $metaentry{':'.$unikey}=$internaltext;
1.339 albertel 13187: } else {
1.908 albertel 13188: # no interesting values, don't set a default
1.339 albertel 13189: }
1.172 www 13190: # end of not-a-package not-a-library import
1.339 albertel 13191: }
1.172 www 13192: # end of not-a-package start tag
1.339 albertel 13193: }
1.172 www 13194: # the next is the end of "start tag"
1.339 albertel 13195: }
13196: }
1.483 albertel 13197: my ($extension) = ($uri =~ /\.(\w+)$/);
1.883 albertel 13198: $extension = lc($extension);
13199: if ($extension eq 'htm') { $extension='html'; }
13200:
1.737 albertel 13201: foreach my $key (keys(%packagetab)) {
1.483 albertel 13202: #no specific packages #how's our extension
13203: if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488 albertel 13204: &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483 albertel 13205: \%metathesekeys);
13206: }
1.883 albertel 13207:
13208: if (!exists($metaentry{':packages'})
13209: || $packagetab{"import_defaults&extension_$extension"}) {
1.737 albertel 13210: foreach my $key (keys(%packagetab)) {
1.483 albertel 13211: #no specific packages well let's get default then
13212: if ($key!~/^default&/) { next; }
1.488 albertel 13213: &metadata_create_package_def($uri,$key,'default',
1.483 albertel 13214: \%metathesekeys);
13215: }
13216: }
1.338 www 13217: # are there custom rights to evaluate
1.599 albertel 13218: if ($metaentry{':copyright'} eq 'custom') {
1.339 albertel 13219:
1.338 www 13220: #
13221: # Importing a rights file here
1.339 albertel 13222: #
13223: unless ($depthcount) {
1.599 albertel 13224: my $location=$metaentry{':customdistributionfile'};
1.339 albertel 13225: my $dir=$filename;
13226: $dir=~s|[^/]*$||;
13227: $location=&filelocation($dir,$location);
1.736 albertel 13228: my $rights_metadata =
1.1172.2.146. .13(raeb 13229:-23): &metadata($uri,'keys',$toolsymb,$location,'_rights',
1.736 albertel 13230: $depthcount+1);
13231: foreach my $rights (split(',',$rights_metadata)) {
13232: #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
13233: $metathesekeys{$rights}=1;
1.339 albertel 13234: }
13235: }
13236: }
1.737 albertel 13237: # uniqifiy package listing
13238: my %seen;
13239: my @uniq_packages =
13240: grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
13241: $metaentry{':packages'} = join(',',@uniq_packages);
13242:
1.1172.2.99 raeburn 13243: if (($importedresponses) || ($importedparts)) {
13244: if ($importedparts) {
1.1070 www 13245: # We had imported parts and need to rebuild partorder
1.1172.2.99 raeburn 13246: $metaentry{':partorder'}='';
13247: $metathesekeys{'partorder'}=1;
13248: }
13249: if ($importedresponses) {
13250: # We had imported responses and need to rebuild responseorder
13251: $metaentry{':responseorder'}='';
13252: $metathesekeys{'responseorder'}=1;
13253: }
13254: for (my $index=0;$index<$#origfiletagids;$index+=2) {
13255: my $origid = $origfiletagids[$index+1];
13256: if ($origfiletagids[$index] eq 'part') {
13257: # Original part, part of the problem
13258: if ($importedparts) {
13259: $metaentry{':partorder'}.=','.$origid;
13260: }
13261: } elsif ($origfiletagids[$index] eq 'import') {
13262: if ($importedparts) {
13263: # We have imported parts at this position
13264: $metaentry{':partorder'}.=','.$importedpartids{$origid};
13265: }
13266: if ($importedresponses) {
13267: # We have imported responses at this position
13268: if (ref($importedrespids{$origid}) eq 'ARRAY') {
13269: $metaentry{':responseorder'}.=','.join(',',map { $origid.'_'.$_ } @{$importedrespids{$origid}});
13270: }
13271: }
13272: } else {
13273: # Original response item, part of the problem
13274: if ($importedresponses) {
13275: $metaentry{':responseorder'}.=','.$origid;
13276: }
13277: }
13278: }
13279: if ($importedparts) {
13280: $metaentry{':partorder'}=~s/^\,//;
13281: }
13282: if ($importedresponses) {
13283: $metaentry{':responseorder'}=~s/^\,//;
13284: }
1.1070 www 13285: }
13286:
1.737 albertel 13287: $metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599 albertel 13288: &metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
1.1172.2.58 raeburn 13289: $metaentry{':allpossiblekeys'}=join(',',keys(%metathesekeys));
1.924 albertel 13290: &do_cache_new('meta',$uri,\%metaentry,$cachetime);
1.177 www 13291: # this is the end of "was not already recently cached
1.71 www 13292: }
1.599 albertel 13293: return $metaentry{':'.$what};
1.261 albertel 13294: }
13295:
1.488 albertel 13296: sub metadata_create_package_def {
1.483 albertel 13297: my ($uri,$key,$package,$metathesekeys)=@_;
13298: my ($pack,$name,$subp)=split(/\&/,$key);
13299: if ($subp eq 'default') { next; }
13300:
1.599 albertel 13301: if (defined($metaentry{':packages'})) {
13302: $metaentry{':packages'}.=','.$package;
1.483 albertel 13303: } else {
1.599 albertel 13304: $metaentry{':packages'}=$package;
1.483 albertel 13305: }
13306: my $value=$packagetab{$key};
13307: my $unikey;
13308: $unikey='parameter_0_'.$name;
1.599 albertel 13309: $metaentry{':'.$unikey.'.part'}=0;
1.483 albertel 13310: $$metathesekeys{$unikey}=1;
1.599 albertel 13311: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
13312: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.483 albertel 13313: }
1.599 albertel 13314: if (defined($metaentry{':'.$unikey.'.default'})) {
13315: $metaentry{':'.$unikey}=
13316: $metaentry{':'.$unikey.'.default'};
1.483 albertel 13317: }
13318: }
13319:
1.261 albertel 13320: sub metadata_generate_part0 {
13321: my ($metadata,$metacache,$uri) = @_;
13322: my %allnames;
1.737 albertel 13323: foreach my $metakey (keys(%$metadata)) {
1.261 albertel 13324: if ($metakey=~/^parameter\_(.*)/) {
1.428 albertel 13325: my $part=$$metacache{':'.$metakey.'.part'};
13326: my $name=$$metacache{':'.$metakey.'.name'};
1.356 albertel 13327: if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261 albertel 13328: $allnames{$name}=$part;
13329: }
13330: }
13331: }
13332: foreach my $name (keys(%allnames)) {
13333: $$metadata{"parameter_0_$name"}=1;
1.428 albertel 13334: my $key=":parameter_0_$name";
1.261 albertel 13335: $$metacache{"$key.part"}='0';
13336: $$metacache{"$key.name"}=$name;
1.428 albertel 13337: $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261 albertel 13338: $allnames{$name}.'_'.$name.
13339: '.type'};
1.428 albertel 13340: my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261 albertel 13341: '.display'};
1.644 www 13342: my $expr='[Part: '.$allnames{$name}.']';
1.479 albertel 13343: $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261 albertel 13344: $$metacache{"$key.display"}=$olddis;
13345: }
1.71 www 13346: }
13347:
1.764 albertel 13348: # ------------------------------------------------------ Devalidate title cache
13349:
13350: sub devalidate_title_cache {
13351: my ($url)=@_;
13352: if (!$env{'request.course.id'}) { return; }
13353: my $symb=&symbread($url);
13354: if (!$symb) { return; }
13355: my $key=$env{'request.course.id'}."\0".$symb;
13356: &devalidate_cache_new('title',$key);
13357: }
13358:
1.1014 droeschl 13359: # ------------------------------------------------- Get the title of a course
13360:
13361: sub current_course_title {
13362: return $env{ 'course.' . $env{'request.course.id'} . '.description' };
13363: }
1.301 www 13364: # ------------------------------------------------- Get the title of a resource
13365:
13366: sub gettitle {
13367: my $urlsymb=shift;
13368: my $symb=&symbread($urlsymb);
1.534 albertel 13369: if ($symb) {
1.620 albertel 13370: my $key=$env{'request.course.id'}."\0".$symb;
1.599 albertel 13371: my ($result,$cached)=&is_cached_new('title',$key);
1.575 albertel 13372: if (defined($cached)) {
13373: return $result;
13374: }
1.534 albertel 13375: my ($map,$resid,$url)=&decode_symb($symb);
13376: my $title='';
1.907 albertel 13377: if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
13378: $title = $env{'course.'.$env{'request.course.id'}.'.description'};
13379: } else {
13380: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13381: &GDBM_READER(),0640)) {
13382: my $mapid=$bighash{'map_pc_'.&clutter($map)};
13383: $title=$bighash{'title_'.$mapid.'.'.$resid};
13384: untie(%bighash);
13385: }
1.534 albertel 13386: }
13387: $title=~s/\&colon\;/\:/gs;
13388: if ($title) {
1.1159 www 13389: # Remember both $symb and $title for dynamic metadata
13390: $accesshash{$symb.'___crstitle'}=$title;
1.1161 www 13391: $accesshash{&declutter($map).'___'.&declutter($url).'___usage'}=time;
1.1159 www 13392: # Cache this title and then return it
1.599 albertel 13393: return &do_cache_new('title',$key,$title,600);
1.534 albertel 13394: }
13395: $urlsymb=$url;
13396: }
13397: my $title=&metadata($urlsymb,'title');
13398: if (!$title) { $title=(split('/',$urlsymb))[-1]; }
13399: return $title;
1.301 www 13400: }
1.613 albertel 13401:
1.614 albertel 13402: sub get_slot {
13403: my ($which,$cnum,$cdom)=@_;
13404: if (!$cnum || !$cdom) {
1.790 albertel 13405: (undef,my $courseid)=&whichuser();
1.620 albertel 13406: $cdom=$env{'course.'.$courseid.'.domain'};
13407: $cnum=$env{'course.'.$courseid.'.num'};
1.614 albertel 13408: }
1.703 albertel 13409: my $key=join("\0",'slots',$cdom,$cnum,$which);
13410: my %slotinfo;
13411: if (exists($remembered{$key})) {
13412: $slotinfo{$which} = $remembered{$key};
13413: } else {
13414: %slotinfo=&get('slots',[$which],$cdom,$cnum);
13415: &Apache::lonhomework::showhash(%slotinfo);
13416: my ($tmp)=keys(%slotinfo);
13417: if ($tmp=~/^error:/) { return (); }
13418: $remembered{$key} = $slotinfo{$which};
13419: }
1.616 albertel 13420: if (ref($slotinfo{$which}) eq 'HASH') {
13421: return %{$slotinfo{$which}};
13422: }
13423: return $slotinfo{$which};
1.614 albertel 13424: }
1.1150 raeburn 13425:
13426: sub get_reservable_slots {
13427: my ($cnum,$cdom,$uname,$udom) = @_;
13428: my $now = time;
13429: my $reservable_info;
13430: my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
13431: if (exists($remembered{$key})) {
13432: $reservable_info = $remembered{$key};
13433: } else {
13434: my %resv;
13435: ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
13436: &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
13437: $reservable_info = \%resv;
13438: $remembered{$key} = $reservable_info;
13439: }
13440: return $reservable_info;
13441: }
13442:
13443: sub get_course_slots {
13444: my ($cnum,$cdom) = @_;
13445: my $hashid=$cnum.':'.$cdom;
13446: my ($result,$cached) = &Apache::lonnet::is_cached_new('allslots',$hashid);
13447: if (defined($cached)) {
13448: if (ref($result) eq 'HASH') {
13449: return %{$result};
13450: }
13451: } else {
13452: my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
13453: my ($tmp) = keys(%slots);
13454: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.1172.2.23 raeburn 13455: &do_cache_new('allslots',$hashid,\%slots,600);
1.1150 raeburn 13456: return %slots;
13457: }
13458: }
13459: return;
13460: }
13461:
13462: sub devalidate_slots_cache {
13463: my ($cnum,$cdom)=@_;
13464: my $hashid=$cnum.':'.$cdom;
13465: &devalidate_cache_new('allslots',$hashid);
13466: }
13467:
1.1172.2.8 raeburn 13468: sub get_coursechange {
13469: my ($cdom,$cnum) = @_;
13470: if ($cdom eq '' || $cnum eq '') {
13471: return unless ($env{'request.course.id'});
13472: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
13473: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
13474: }
13475: my $hashid=$cdom.'_'.$cnum;
13476: my ($change,$cached)=&is_cached_new('crschange',$hashid);
13477: if ((defined($cached)) && ($change ne '')) {
13478: return $change;
13479: } else {
13480: my %crshash;
13481: %crshash = &get('environment',['internal.contentchange'],$cdom,$cnum);
13482: if ($crshash{'internal.contentchange'} eq '') {
13483: $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
13484: if ($change eq '') {
13485: %crshash = &get('environment',['internal.created'],$cdom,$cnum);
13486: $change = $crshash{'internal.created'};
13487: }
13488: } else {
13489: $change = $crshash{'internal.contentchange'};
13490: }
13491: my $cachetime = 600;
13492: &do_cache_new('crschange',$hashid,$change,$cachetime);
13493: }
13494: return $change;
13495: }
13496:
13497: sub devalidate_coursechange_cache {
1.1172.2.146. .9(raebu 13498:23): my ($cdom,$cnum)=@_;
13499:23): my $hashid=$cdom.'_'.$cnum;
1.1172.2.8 raeburn 13500: &devalidate_cache_new('crschange',$hashid);
13501: }
13502:
1.1172.2.146. .9(raebu 13503:23): sub get_suppchange {
13504:23): my ($cdom,$cnum) = @_;
13505:23): if ($cdom eq '' || $cnum eq '') {
13506:23): return unless ($env{'request.course.id'});
13507:23): $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
13508:23): $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
13509:23): }
13510:23): my $hashid=$cdom.'_'.$cnum;
13511:23): my ($change,$cached)=&is_cached_new('suppchange',$hashid);
13512:23): if ((defined($cached)) && ($change ne '')) {
13513:23): return $change;
13514:23): } else {
13515:23): my %crshash = &get('environment',['internal.supplementalchange'],$cdom,$cnum);
13516:23): if ($crshash{'internal.supplementalchange'} eq '') {
13517:23): $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
13518:23): if ($change eq '') {
13519:23): %crshash = &get('environment',['internal.created'],$cdom,$cnum);
13520:23): $change = $crshash{'internal.created'};
13521:23): }
13522:23): } else {
13523:23): $change = $crshash{'internal.supplementalchange'};
13524:23): }
13525:23): my $cachetime = 600;
13526:23): &do_cache_new('suppchange',$hashid,$change,$cachetime);
13527:23): }
13528:23): return $change;
13529:23): }
13530:23):
13531:23): sub devalidate_suppchange_cache {
13532:23): my ($cdom,$cnum)=@_;
13533:23): my $hashid=$cdom.'_'.$cnum;
13534:23): &devalidate_cache_new('suppchange',$hashid);
13535:23): }
13536:23):
13537:23): sub update_supp_caches {
13538:23): my ($cdom,$cnum) = @_;
13539:23): my %servers = &internet_dom_servers($cdom);
13540:23): my @ids=¤t_machine_ids();
13541:23): foreach my $server (keys(%servers)) {
13542:23): next if (grep(/^\Q$server\E$/,@ids));
13543:23): my $hashid=$cnum.':'.$cdom;
13544:23): my $cachekey = &escape('showsupp').':'.&escape($hashid);
13545:23): &remote_devalidate_cache($server,[$cachekey]);
13546:23): }
13547:23): &has_unhidden_suppfiles($cnum,$cdom,1,1);
13548:23): &count_supptools($cnum,$cdom,1);
13549:23): my $now = time;
13550:23): if ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
13551:23): &Apache::lonnet::appenv({'request.course.suppupdated' => $now});
13552:23): }
13553:23): &put('environment',{'internal.supplementalchange' => $now},
13554:23): $cdom,$cnum);
13555:23): &Apache::lonnet::appenv(
13556:23): {'course.'.$cdom.'_'.$cnum.'.internal.supplementalchange' => $now});
13557:23): &do_cache_new('suppchange',$cdom.'_'.$cnum,$now,600);
13558:23): }
13559:23):
1.31 www 13560: # ------------------------------------------------- Update symbolic store links
13561:
13562: sub symblist {
13563: my ($mapname,%newhash)=@_;
1.438 www 13564: $mapname=&deversion(&declutter($mapname));
1.31 www 13565: my %hash;
1.620 albertel 13566: if (($env{'request.course.fn'}) && (%newhash)) {
13567: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256 albertel 13568: &GDBM_WRCREAT(),0640)) {
1.1000 raeburn 13569: foreach my $url (keys(%newhash)) {
1.711 albertel 13570: next if ($url eq 'last_known'
13571: && $env{'form.no_update_last_known'});
13572: $hash{declutter($url)}=&encode_symb($mapname,
13573: $newhash{$url}->[1],
13574: $newhash{$url}->[0]);
1.191 harris41 13575: }
1.31 www 13576: if (untie(%hash)) {
13577: return 'ok';
13578: }
13579: }
13580: }
13581: return 'error';
1.212 www 13582: }
13583:
13584: # --------------------------------------------------------------- Verify a symb
13585:
13586: sub symbverify {
1.1172.2.11 raeburn 13587: my ($symb,$thisurl,$encstate)=@_;
1.510 www 13588: my $thisfn=$thisurl;
1.439 www 13589: $thisfn=&declutter($thisfn);
1.215 www 13590: # direct jump to resource in page or to a sequence - will construct own symbs
13591: if ($thisfn=~/\.(page|sequence)$/) { return 1; }
13592: # check URL part
1.409 www 13593: my ($map,$resid,$url)=&decode_symb($symb);
1.439 www 13594:
1.431 www 13595: unless ($url eq $thisfn) { return 0; }
1.213 www 13596:
1.216 www 13597: $symb=&symbclean($symb);
1.510 www 13598: $thisurl=&deversion($thisurl);
1.439 www 13599: $thisfn=&deversion($thisfn);
1.213 www 13600:
13601: my %bighash;
13602: my $okay=0;
1.431 www 13603:
1.620 albertel 13604: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 13605: &GDBM_READER(),0640)) {
1.1032 raeburn 13606: if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
13607: $thisurl =~ s/\?.+$//;
1.1172.2.13 raeburn 13608: if ($map =~ m{^uploaded/.+\.page$}) {
13609: $thisurl =~ s{^(/adm/wrapper|)/ext/}{http://};
13610: $thisurl =~ s{^\Qhttp://https://\E}{https://};
13611: }
13612: }
13613: my $ids;
1.1172.2.122 raeburn 13614: if ($map =~ m{^uploaded/.+\.page$}) {
13615: $ids=$bighash{'ids_'.&clutter_with_no_wrapper($thisurl)};
1.1172.2.13 raeburn 13616: } else {
13617: $ids=$bighash{'ids_'.&clutter($thisurl)};
1.1032 raeburn 13618: }
1.1102 raeburn 13619: unless ($ids) {
1.1172.2.13 raeburn 13620: my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;
1.1102 raeburn 13621: $ids=$bighash{$idkey};
1.216 www 13622: }
13623: if ($ids) {
13624: # ------------------------------------------------------------------- Has ID(s)
1.1172.2.13 raeburn 13625: if ($thisfn =~ m{^/adm/wrapper/ext/}) {
13626: $symb =~ s/\?.+$//;
13627: }
1.800 albertel 13628: foreach my $id (split(/\,/,$ids)) {
13629: my ($mapid,$resid)=split(/\./,$id);
1.216 www 13630: if (
13631: &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
1.1172.2.13 raeburn 13632: eq $symb) {
1.1172.2.11 raeburn 13633: if (ref($encstate)) {
13634: $$encstate = $bighash{'encrypted_'.$id};
13635: }
1.1172.2.13 raeburn 13636: if (($env{'request.role.adv'}) ||
13637: ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
1.1101 raeburn 13638: ($thisurl eq '/adm/navmaps')) {
1.1172.2.13 raeburn 13639: $okay=1;
13640: last;
13641: }
13642: }
13643: }
1.216 www 13644: }
1.213 www 13645: untie(%bighash);
13646: }
13647: return $okay;
1.31 www 13648: }
13649:
1.210 www 13650: # --------------------------------------------------------------- Clean-up symb
13651:
13652: sub symbclean {
13653: my $symb=shift;
1.568 albertel 13654: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210 www 13655: # remove version from map
13656: $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215 www 13657:
1.210 www 13658: # remove version from URL
13659: $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213 www 13660:
1.507 www 13661: # remove wrapper
13662:
1.510 www 13663: $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694 albertel 13664: $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210 www 13665: return $symb;
1.409 www 13666: }
13667:
13668: # ---------------------------------------------- Split symb to find map and url
1.429 albertel 13669:
13670: sub encode_symb {
13671: my ($map,$resid,$url)=@_;
13672: return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
13673: }
1.409 www 13674:
13675: sub decode_symb {
1.568 albertel 13676: my $symb=shift;
13677: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
13678: my ($map,$resid,$url)=split(/___/,$symb);
1.413 www 13679: return (&fixversion($map),$resid,&fixversion($url));
13680: }
13681:
13682: sub fixversion {
13683: my $fn=shift;
1.609 banghart 13684: if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435 www 13685: my %bighash;
13686: my $uri=&clutter($fn);
1.620 albertel 13687: my $key=$env{'request.course.id'}.'_'.$uri;
1.440 www 13688: # is this cached?
1.599 albertel 13689: my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440 www 13690: if (defined($cached)) { return $result; }
13691: # unfortunately not cached, or expired
1.620 albertel 13692: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440 www 13693: &GDBM_READER(),0640)) {
13694: if ($bighash{'version_'.$uri}) {
13695: my $version=$bighash{'version_'.$uri};
1.444 www 13696: unless (($version eq 'mostrecent') ||
13697: ($version==&getversion($uri))) {
1.440 www 13698: $uri=~s/\.(\w+)$/\.$version\.$1/;
13699: }
13700: }
13701: untie %bighash;
1.413 www 13702: }
1.599 albertel 13703: return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438 www 13704: }
13705:
13706: sub deversion {
13707: my $url=shift;
13708: $url=~s/\.\d+\.(\w+)$/\.$1/;
13709: return $url;
1.210 www 13710: }
13711:
1.31 www 13712: # ------------------------------------------------------ Return symb list entry
13713:
13714: sub symbread {
1.1172.2.126 raeburn 13715: my ($thisfn,$donotrecurse,$ignorecachednull,$checkforblock,$possibles,
1.1172.2.128 raeburn 13716: $ignoresymbdb,$noenccheck)=@_;
1.1172.2.54 raeburn 13717: my $cache_str='request.symbread.cached.'.$thisfn;
1.1172.2.128 raeburn 13718: if (defined($env{$cache_str})) {
1.1172.2.126 raeburn 13719: unless (ref($possibles) eq 'HASH') {
13720: if ($ignorecachednull) {
13721: return $env{$cache_str} unless ($env{$cache_str} eq '');
13722: } else {
13723: return $env{$cache_str};
13724: }
1.1172.2.66 raeburn 13725: }
13726: }
1.242 www 13727: # no filename provided? try from environment
1.1172.2.54 raeburn 13728: unless ($thisfn) {
1.620 albertel 13729: if ($env{'request.symb'}) {
1.1172.2.128 raeburn 13730: return $env{$cache_str}=&symbclean($env{'request.symb'});
1.1172.2.13 raeburn 13731: }
13732: $thisfn=$env{'request.filename'};
1.44 www 13733: }
1.569 albertel 13734: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242 www 13735: # is that filename actually a symb? Verify, clean, and return
13736: if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539 albertel 13737: if (&symbverify($thisfn,$1)) {
1.1172.2.128 raeburn 13738: return $env{$cache_str}=&symbclean($thisfn);
1.539 albertel 13739: }
1.242 www 13740: }
1.44 www 13741: $thisfn=declutter($thisfn);
1.31 www 13742: my %hash;
1.37 www 13743: my %bighash;
13744: my $syval='';
1.620 albertel 13745: if (($env{'request.course.fn'}) && ($thisfn)) {
1.1172.2.128 raeburn 13746: unless ($ignoresymbdb) {
1.1172.2.126 raeburn 13747: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
13748: &GDBM_READER(),0640)) {
1.1172.2.146. .7(raebu 13749:22): $syval=$hash{$thisfn};
1.1172.2.126 raeburn 13750: untie(%hash);
13751: }
1.1172.2.128 raeburn 13752: if ($syval && $checkforblock) {
13753: my @blockers = &has_comm_blocking('bre',$syval,$thisfn,$ignoresymbdb,$noenccheck);
1.1172.2.126 raeburn 13754: if (@blockers) {
13755: $syval='';
13756: }
13757: }
1.37 www 13758: }
13759: # ---------------------------------------------------------- There was an entry
13760: if ($syval) {
1.601 albertel 13761: #unless ($syval=~/\_\d+$/) {
1.620 albertel 13762: #unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.949 raeburn 13763: #&appenv({'request.ambiguous' => $thisfn});
1.620 albertel 13764: #return $env{$cache_str}='';
1.601 albertel 13765: #}
13766: #$syval.=$1;
13767: #}
1.37 www 13768: } else {
13769: # ------------------------------------------------------- Was not in symb table
1.620 albertel 13770: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 13771: &GDBM_READER(),0640)) {
1.37 www 13772: # ---------------------------------------------- Get ID(s) for current resource
1.280 www 13773: my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65 www 13774: unless ($ids) {
13775: $ids=$bighash{'ids_/'.$thisfn};
1.242 www 13776: }
13777: unless ($ids) {
13778: # alias?
13779: $ids=$bighash{'mapalias_'.$thisfn};
1.65 www 13780: }
1.37 www 13781: if ($ids) {
13782: # ------------------------------------------------------------------- Has ID(s)
13783: my @possibilities=split(/\,/,$ids);
1.39 www 13784: if ($#possibilities==0) {
13785: # ----------------------------------------------- There is only one possibility
1.37 www 13786: my ($mapid,$resid)=split(/\./,$ids);
1.626 albertel 13787: $syval=&encode_symb($bighash{'map_id_'.$mapid},
13788: $resid,$thisfn);
1.1172.2.66 raeburn 13789: if (ref($possibles) eq 'HASH') {
1.1172.2.126 raeburn 13790: unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
13791: $possibles->{$syval} = 1;
13792: }
1.1172.2.66 raeburn 13793: }
13794: if ($checkforblock) {
1.1172.2.126 raeburn 13795: unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
1.1172.2.127 raeburn 13796: my @blockers = &has_comm_blocking('bre',$syval,$bighash{'src_'.$ids},'',$noenccheck);
1.1172.2.126 raeburn 13797: if (@blockers) {
13798: $syval = '';
13799: untie(%bighash);
13800: return $env{$cache_str}='';
13801: }
1.1172.2.66 raeburn 13802: }
13803: }
13804: } elsif ((!$donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
1.39 www 13805: # ------------------------------------------ There is more than one possibility
13806: my $realpossible=0;
1.800 albertel 13807: foreach my $id (@possibilities) {
13808: my $file=$bighash{'src_'.$id};
1.1172.2.66 raeburn 13809: my $canaccess;
13810: if (($donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
13811: $canaccess = 1;
13812: } else {
13813: $canaccess = &allowed('bre',$file);
13814: }
13815: if ($canaccess) {
13816: my ($mapid,$resid)=split(/\./,$id);
13817: if ($bighash{'map_type_'.$mapid} ne 'page') {
13818: my $poss_syval=&encode_symb($bighash{'map_id_'.$mapid},
13819: $resid,$thisfn);
1.1172.2.126 raeburn 13820: next if ($bighash{'randomout_'.$id} && !$env{'request.role.adv'});
1.1172.2.127 raeburn 13821: next unless (($noenccheck) || ($bighash{'encrypted_'.$id} eq $env{'request.enc'}));
1.1172.2.66 raeburn 13822: if ($checkforblock) {
1.1172.2.127 raeburn 13823: my @blockers = &has_comm_blocking('bre',$poss_syval,$file,'',$noenccheck);
1.1172.2.126 raeburn 13824: if (@blockers > 0) {
13825: $syval = '';
13826: } else {
1.1172.2.66 raeburn 13827: $syval = $poss_syval;
13828: $realpossible++;
13829: }
13830: } else {
13831: $syval = $poss_syval;
13832: $realpossible++;
13833: }
1.1172.2.126 raeburn 13834: if ($syval) {
13835: if (ref($possibles) eq 'HASH') {
13836: $possibles->{$syval} = 1;
13837: }
13838: }
1.1172.2.66 raeburn 13839: }
1.39 www 13840: }
1.191 harris41 13841: }
1.39 www 13842: if ($realpossible!=1) { $syval=''; }
1.249 www 13843: } else {
13844: $syval='';
1.37 www 13845: }
13846: }
1.1172.2.66 raeburn 13847: untie(%bighash);
1.481 raeburn 13848: }
1.31 www 13849: }
1.62 www 13850: if ($syval) {
1.1172.2.128 raeburn 13851: return $env{$cache_str}=$syval;
1.62 www 13852: }
1.31 www 13853: }
1.949 raeburn 13854: &appenv({'request.ambiguous' => $thisfn});
1.620 albertel 13855: return $env{$cache_str}='';
1.31 www 13856: }
13857:
13858: # ---------------------------------------------------------- Return random seed
13859:
1.32 www 13860: sub numval {
13861: my $txt=shift;
13862: $txt=~tr/A-J/0-9/;
13863: $txt=~tr/a-j/0-9/;
13864: $txt=~tr/K-T/0-9/;
13865: $txt=~tr/k-t/0-9/;
13866: $txt=~tr/U-Z/0-5/;
13867: $txt=~tr/u-z/0-5/;
13868: $txt=~s/\D//g;
1.564 albertel 13869: if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32 www 13870: return int($txt);
1.368 albertel 13871: }
13872:
1.484 albertel 13873: sub numval2 {
13874: my $txt=shift;
13875: $txt=~tr/A-J/0-9/;
13876: $txt=~tr/a-j/0-9/;
13877: $txt=~tr/K-T/0-9/;
13878: $txt=~tr/k-t/0-9/;
13879: $txt=~tr/U-Z/0-5/;
13880: $txt=~tr/u-z/0-5/;
13881: $txt=~s/\D//g;
13882: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
13883: my $total;
13884: foreach my $val (@txts) { $total+=$val; }
1.564 albertel 13885: if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484 albertel 13886: return int($total);
13887: }
13888:
1.575 albertel 13889: sub numval3 {
13890: use integer;
13891: my $txt=shift;
13892: $txt=~tr/A-J/0-9/;
13893: $txt=~tr/a-j/0-9/;
13894: $txt=~tr/K-T/0-9/;
13895: $txt=~tr/k-t/0-9/;
13896: $txt=~tr/U-Z/0-5/;
13897: $txt=~tr/u-z/0-5/;
13898: $txt=~s/\D//g;
13899: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
13900: my $total;
13901: foreach my $val (@txts) { $total+=$val; }
13902: if ($_64bit) { $total=(($total<<32)>>32); }
13903: return $total;
13904: }
13905:
1.675 albertel 13906: sub digest {
13907: my ($data)=@_;
13908: my $digest=&Digest::MD5::md5($data);
13909: my ($a,$b,$c,$d)=unpack("iiii",$digest);
13910: my ($e,$f);
13911: {
13912: use integer;
13913: $e=($a+$b);
13914: $f=($c+$d);
13915: if ($_64bit) {
13916: $e=(($e<<32)>>32);
13917: $f=(($f<<32)>>32);
13918: }
13919: }
13920: if (wantarray) {
13921: return ($e,$f);
13922: } else {
13923: my $g;
13924: {
13925: use integer;
13926: $g=($e+$f);
13927: if ($_64bit) {
13928: $g=(($g<<32)>>32);
13929: }
13930: }
13931: return $g;
13932: }
13933: }
13934:
1.368 albertel 13935: sub latest_rnd_algorithm_id {
1.675 albertel 13936: return '64bit5';
1.366 albertel 13937: }
1.32 www 13938:
1.503 albertel 13939: sub get_rand_alg {
13940: my ($courseid)=@_;
1.790 albertel 13941: if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503 albertel 13942: if ($courseid) {
1.620 albertel 13943: return $env{"course.$courseid.rndseed"};
1.503 albertel 13944: }
13945: return &latest_rnd_algorithm_id();
13946: }
13947:
1.562 albertel 13948: sub validCODE {
13949: my ($CODE)=@_;
13950: if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
13951: return 0;
13952: }
13953:
1.491 albertel 13954: sub getCODE {
1.620 albertel 13955: if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618 albertel 13956: if ( (defined($Apache::lonhomework::parsing_a_problem) ||
13957: defined($Apache::lonhomework::parsing_a_task) ) &&
13958: &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491 albertel 13959: return $Apache::lonhomework::history{'resource.CODE'};
13960: }
13961: return undef;
13962: }
1.1133 foxr 13963: #
13964: # Determines the random seed for a specific context:
13965: #
13966: # parameters:
13967: # symb - in course context the symb for the seed.
13968: # course_id - The course id of the form domain_coursenum.
13969: # domain - Domain for the user.
13970: # course - Course for the user.
13971: # cenv - environment of the course.
13972: #
13973: # NOTE:
13974: # All parameters are picked out of the environment if missing
13975: # or not defined.
13976: # If a symb cannot be determined the current time is used instead.
13977: #
13978: # For a given well defined symb, courside, domain, username,
13979: # and course environment, the seed is reproducible.
13980: #
1.31 www 13981: sub rndseed {
1.1133 foxr 13982: my ($symb,$courseid,$domain,$username, $cenv)=@_;
1.790 albertel 13983: my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.896 albertel 13984: if (!defined($symb)) {
1.366 albertel 13985: unless ($symb=$wsymb) { return time; }
13986: }
1.1146 foxr 13987: if (!defined $courseid) {
13988: $courseid=$wcourseid;
13989: }
13990: if (!defined $domain) { $domain=$wdomain; }
13991: if (!defined $username) { $username=$wusername }
1.1133 foxr 13992:
13993: my $which;
13994: if (defined($cenv->{'rndseed'})) {
13995: $which = $cenv->{'rndseed'};
13996: } else {
13997: $which =&get_rand_alg($courseid);
13998: }
1.491 albertel 13999: if (defined(&getCODE())) {
1.675 albertel 14000: if ($which eq '64bit5') {
14001: return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
14002: } elsif ($which eq '64bit4') {
1.575 albertel 14003: return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
14004: } else {
14005: return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
14006: }
1.675 albertel 14007: } elsif ($which eq '64bit5') {
14008: return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575 albertel 14009: } elsif ($which eq '64bit4') {
14010: return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501 albertel 14011: } elsif ($which eq '64bit3') {
14012: return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443 albertel 14013: } elsif ($which eq '64bit2') {
14014: return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366 albertel 14015: } elsif ($which eq '64bit') {
14016: return &rndseed_64bit($symb,$courseid,$domain,$username);
14017: }
14018: return &rndseed_32bit($symb,$courseid,$domain,$username);
14019: }
14020:
14021: sub rndseed_32bit {
14022: my ($symb,$courseid,$domain,$username)=@_;
14023: {
14024: use integer;
14025: my $symbchck=unpack("%32C*",$symb) << 27;
14026: my $symbseed=numval($symb) << 22;
14027: my $namechck=unpack("%32C*",$username) << 17;
14028: my $nameseed=numval($username) << 12;
14029: my $domainseed=unpack("%32C*",$domain) << 7;
14030: my $courseseed=unpack("%32C*",$courseid);
14031: my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790 albertel 14032: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14033: #&logthis("rndseed :$num:$symb");
1.564 albertel 14034: if ($_64bit) { $num=(($num<<32)>>32); }
1.366 albertel 14035: return $num;
14036: }
14037: }
14038:
14039: sub rndseed_64bit {
14040: my ($symb,$courseid,$domain,$username)=@_;
14041: {
14042: use integer;
14043: my $symbchck=unpack("%32S*",$symb) << 21;
14044: my $symbseed=numval($symb) << 10;
14045: my $namechck=unpack("%32S*",$username);
14046:
14047: my $nameseed=numval($username) << 21;
14048: my $domainseed=unpack("%32S*",$domain) << 10;
14049: my $courseseed=unpack("%32S*",$courseid);
14050:
14051: my $num1=$symbchck+$symbseed+$namechck;
14052: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 14053: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14054: #&logthis("rndseed :$num:$symb");
1.564 albertel 14055: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366 albertel 14056: return "$num1,$num2";
1.155 albertel 14057: }
1.366 albertel 14058: }
14059:
1.443 albertel 14060: sub rndseed_64bit2 {
14061: my ($symb,$courseid,$domain,$username)=@_;
14062: {
14063: use integer;
14064: # strings need to be an even # of cahracters long, it it is odd the
14065: # last characters gets thrown away
14066: my $symbchck=unpack("%32S*",$symb.' ') << 21;
14067: my $symbseed=numval($symb) << 10;
14068: my $namechck=unpack("%32S*",$username.' ');
14069:
14070: my $nameseed=numval($username) << 21;
1.501 albertel 14071: my $domainseed=unpack("%32S*",$domain.' ') << 10;
14072: my $courseseed=unpack("%32S*",$courseid.' ');
14073:
14074: my $num1=$symbchck+$symbseed+$namechck;
14075: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 14076: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14077: #&logthis("rndseed :$num:$symb");
1.803 albertel 14078: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501 albertel 14079: return "$num1,$num2";
14080: }
14081: }
14082:
14083: sub rndseed_64bit3 {
14084: my ($symb,$courseid,$domain,$username)=@_;
14085: {
14086: use integer;
14087: # strings need to be an even # of cahracters long, it it is odd the
14088: # last characters gets thrown away
14089: my $symbchck=unpack("%32S*",$symb.' ') << 21;
14090: my $symbseed=numval2($symb) << 10;
14091: my $namechck=unpack("%32S*",$username.' ');
14092:
14093: my $nameseed=numval2($username) << 21;
1.443 albertel 14094: my $domainseed=unpack("%32S*",$domain.' ') << 10;
14095: my $courseseed=unpack("%32S*",$courseid.' ');
14096:
14097: my $num1=$symbchck+$symbseed+$namechck;
14098: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 14099: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14100: #&logthis("rndseed :$num1:$num2:$_64bit");
1.564 albertel 14101: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110 www 14102:
1.503 albertel 14103: return "$num1:$num2";
1.443 albertel 14104: }
14105: }
14106:
1.575 albertel 14107: sub rndseed_64bit4 {
14108: my ($symb,$courseid,$domain,$username)=@_;
14109: {
14110: use integer;
14111: # strings need to be an even # of cahracters long, it it is odd the
14112: # last characters gets thrown away
14113: my $symbchck=unpack("%32S*",$symb.' ') << 21;
14114: my $symbseed=numval3($symb) << 10;
14115: my $namechck=unpack("%32S*",$username.' ');
14116:
14117: my $nameseed=numval3($username) << 21;
14118: my $domainseed=unpack("%32S*",$domain.' ') << 10;
14119: my $courseseed=unpack("%32S*",$courseid.' ');
14120:
14121: my $num1=$symbchck+$symbseed+$namechck;
14122: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 14123: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14124: #&logthis("rndseed :$num1:$num2:$_64bit");
1.575 albertel 14125: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110 www 14126:
1.575 albertel 14127: return "$num1:$num2";
14128: }
14129: }
14130:
1.675 albertel 14131: sub rndseed_64bit5 {
14132: my ($symb,$courseid,$domain,$username)=@_;
14133: my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
14134: return "$num1:$num2";
14135: }
14136:
1.366 albertel 14137: sub rndseed_CODE_64bit {
14138: my ($symb,$courseid,$domain,$username)=@_;
1.155 albertel 14139: {
1.366 albertel 14140: use integer;
1.443 albertel 14141: my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484 albertel 14142: my $symbseed=numval2($symb);
1.491 albertel 14143: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
14144: my $CODEseed=numval(&getCODE());
1.443 albertel 14145: my $courseseed=unpack("%32S*",$courseid.' ');
1.484 albertel 14146: my $num1=$symbseed+$CODEchck;
14147: my $num2=$CODEseed+$courseseed+$symbchck;
1.790 albertel 14148: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
14149: #&logthis("rndseed :$num1:$num2:$symb");
1.564 albertel 14150: if ($_64bit) { $num1=(($num1<<32)>>32); }
14151: if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503 albertel 14152: return "$num1:$num2";
1.366 albertel 14153: }
14154: }
14155:
1.575 albertel 14156: sub rndseed_CODE_64bit4 {
14157: my ($symb,$courseid,$domain,$username)=@_;
14158: {
14159: use integer;
14160: my $symbchck=unpack("%32S*",$symb.' ') << 16;
14161: my $symbseed=numval3($symb);
14162: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
14163: my $CODEseed=numval3(&getCODE());
14164: my $courseseed=unpack("%32S*",$courseid.' ');
14165: my $num1=$symbseed+$CODEchck;
14166: my $num2=$CODEseed+$courseseed+$symbchck;
1.790 albertel 14167: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
14168: #&logthis("rndseed :$num1:$num2:$symb");
1.575 albertel 14169: if ($_64bit) { $num1=(($num1<<32)>>32); }
14170: if ($_64bit) { $num2=(($num2<<32)>>32); }
14171: return "$num1:$num2";
14172: }
14173: }
14174:
1.675 albertel 14175: sub rndseed_CODE_64bit5 {
14176: my ($symb,$courseid,$domain,$username)=@_;
14177: my $code = &getCODE();
14178: my ($num1,$num2)=&digest("$symb,$courseid,$code");
14179: return "$num1:$num2";
14180: }
14181:
1.366 albertel 14182: sub setup_random_from_rndseed {
14183: my ($rndseed)=@_;
1.503 albertel 14184: if ($rndseed =~/([,:])/) {
1.1172.2.51 raeburn 14185: my ($num1,$num2) = map { abs($_); } (split(/[,:]/,$rndseed));
14186: if ((!$num1) || (!$num2) || ($num1 > 2147483562) || ($num2 > 2147483398)) {
14187: &Math::Random::random_set_seed_from_phrase($rndseed);
14188: } else {
14189: &Math::Random::random_set_seed($num1,$num2);
14190: }
1.366 albertel 14191: } else {
14192: &Math::Random::random_set_seed_from_phrase($rndseed);
1.98 albertel 14193: }
1.36 albertel 14194: }
14195:
1.474 albertel 14196: sub latest_receipt_algorithm_id {
1.835 albertel 14197: return 'receipt3';
1.474 albertel 14198: }
14199:
1.480 www 14200: sub recunique {
14201: my $fucourseid=shift;
14202: my $unique;
1.835 albertel 14203: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
14204: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620 albertel 14205: $unique=$env{"course.$fucourseid.internal.encseed"};
1.480 www 14206: } else {
14207: $unique=$perlvar{'lonReceipt'};
14208: }
14209: return unpack("%32C*",$unique);
14210: }
14211:
14212: sub recprefix {
14213: my $fucourseid=shift;
14214: my $prefix;
1.835 albertel 14215: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
14216: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620 albertel 14217: $prefix=$env{"course.$fucourseid.internal.encpref"};
1.480 www 14218: } else {
14219: $prefix=$perlvar{'lonHostID'};
14220: }
14221: return unpack("%32C*",$prefix);
14222: }
14223:
1.76 www 14224: sub ireceipt {
1.474 albertel 14225: my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835 albertel 14226:
14227: my $return =&recprefix($fucourseid).'-';
14228:
14229: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
14230: $env{'request.state'} eq 'construct') {
14231: $return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
14232: return $return;
14233: }
14234:
1.76 www 14235: my $cuname=unpack("%32C*",$funame);
14236: my $cudom=unpack("%32C*",$fudom);
14237: my $cucourseid=unpack("%32C*",$fucourseid);
14238: my $cusymb=unpack("%32C*",$fusymb);
1.480 www 14239: my $cunique=&recunique($fucourseid);
1.474 albertel 14240: my $cpart=unpack("%32S*",$part);
1.835 albertel 14241: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
14242:
1.790 albertel 14243: #&logthis("doing receipt2 using parts $cpart, uname $cuname and udom $cudom gets ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474 albertel 14244:
14245: $return.= ($cunique%$cuname+
14246: $cunique%$cudom+
14247: $cusymb%$cuname+
14248: $cusymb%$cudom+
14249: $cucourseid%$cuname+
14250: $cucourseid%$cudom+
14251: $cpart%$cuname+
14252: $cpart%$cudom);
14253: } else {
14254: $return.= ($cunique%$cuname+
14255: $cunique%$cudom+
14256: $cusymb%$cuname+
14257: $cusymb%$cudom+
14258: $cucourseid%$cuname+
14259: $cucourseid%$cudom);
14260: }
14261: return $return;
1.76 www 14262: }
14263:
14264: sub receipt {
1.474 albertel 14265: my ($part)=@_;
1.790 albertel 14266: my ($symb,$courseid,$domain,$name) = &whichuser();
1.474 albertel 14267: return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76 www 14268: }
1.260 ng 14269:
1.790 albertel 14270: sub whichuser {
14271: my ($passedsymb)=@_;
14272: my ($symb,$courseid,$domain,$name,$publicuser);
14273: if (defined($env{'form.grade_symb'})) {
14274: my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
14275: my $allowed=&allowed('vgr',$tmp_courseid);
14276: if (!$allowed &&
14277: exists($env{'request.course.sec'}) &&
14278: $env{'request.course.sec'} !~ /^\s*$/) {
14279: $allowed=&allowed('vgr',$tmp_courseid.
14280: '/'.$env{'request.course.sec'});
14281: }
14282: if ($allowed) {
14283: ($symb)=&get_env_multiple('form.grade_symb');
14284: $courseid=$tmp_courseid;
14285: ($domain)=&get_env_multiple('form.grade_domain');
14286: ($name)=&get_env_multiple('form.grade_username');
14287: return ($symb,$courseid,$domain,$name,$publicuser);
14288: }
14289: }
14290: if (!$passedsymb) {
14291: $symb=&symbread();
14292: } else {
14293: $symb=$passedsymb;
14294: }
14295: $courseid=$env{'request.course.id'};
14296: $domain=$env{'user.domain'};
14297: $name=$env{'user.name'};
14298: if ($name eq 'public' && $domain eq 'public') {
14299: if (!defined($env{'form.username'})) {
14300: $env{'form.username'}.=time.rand(10000000);
14301: }
14302: $name.=$env{'form.username'};
14303: }
14304: return ($symb,$courseid,$domain,$name,$publicuser);
14305:
14306: }
14307:
1.36 albertel 14308: # ------------------------------------------------------------ Serves up a file
1.472 albertel 14309: # returns either the contents of the file or
14310: # -1 if the file doesn't exist
1.481 raeburn 14311: #
14312: # if the target is a file that was uploaded via DOCS,
14313: # a check will be made to see if a current copy exists on the local server,
14314: # if it does this will be served, otherwise a copy will be retrieved from
14315: # the home server for the course and stored in /home/httpd/html/userfiles on
14316: # the local server.
1.472 albertel 14317:
1.36 albertel 14318: sub getfile {
1.538 albertel 14319: my ($file) = @_;
1.609 banghart 14320: if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538 albertel 14321: &repcopy($file);
14322: return &readfile($file);
14323: }
14324:
14325: sub repcopy_userfile {
14326: my ($file)=@_;
1.1142 raeburn 14327: my $londocroot = $perlvar{'lonDocRoot'};
14328: if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
1.1164 raeburn 14329: if ($file =~ m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
1.538 albertel 14330: my ($cdom,$cnum,$filename) =
1.811 albertel 14331: ($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538 albertel 14332: my $uri="/uploaded/$cdom/$cnum/$filename";
14333: if (-e "$file") {
1.828 www 14334: # we already have a local copy, check it out
1.538 albertel 14335: my @fileinfo = stat($file);
1.828 www 14336: my $rtncode;
14337: my $info;
1.538 albertel 14338: my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482 albertel 14339: if ($lwpresp ne 'ok') {
1.828 www 14340: # there is no such file anymore, even though we had a local copy
1.482 albertel 14341: if ($rtncode eq '404') {
1.538 albertel 14342: unlink($file);
1.482 albertel 14343: }
14344: return -1;
14345: }
14346: if ($info < $fileinfo[9]) {
1.828 www 14347: # nice, the file we have is up-to-date, just say okay
1.607 raeburn 14348: return 'ok';
1.828 www 14349: } else {
14350: # the file is outdated, get rid of it
14351: unlink($file);
1.482 albertel 14352: }
1.828 www 14353: }
14354: # one way or the other, at this point, we don't have the file
14355: # construct the correct path for the file
14356: my @parts = ($cdom,$cnum);
14357: if ($filename =~ m|^(.+)/[^/]+$|) {
14358: push @parts, split(/\//,$1);
14359: }
14360: my $path = $perlvar{'lonDocRoot'}.'/userfiles';
14361: foreach my $part (@parts) {
14362: $path .= '/'.$part;
14363: if (!-e $path) {
14364: mkdir($path,0770);
1.482 albertel 14365: }
14366: }
1.828 www 14367: # now the path exists for sure
14368: # get a user agent
14369: my $ua=new LWP::UserAgent;
14370: my $transferfile=$file.'.in.transfer';
14371: # FIXME: this should flock
14372: if (-e $transferfile) { return 'ok'; }
14373: my $request;
14374: $uri=~s/^\///;
1.980 raeburn 14375: my $homeserver = &homeserver($cnum,$cdom);
1.1172.2.120 raeburn 14376: my $hostname = &hostname($homeserver);
1.980 raeburn 14377: my $protocol = $protocol{$homeserver};
14378: $protocol = 'http' if ($protocol ne 'https');
1.1172.2.120 raeburn 14379: $request=new HTTP::Request('GET',$protocol.'://'.$hostname.'/raw/'.$uri);
1.828 www 14380: my $response=$ua->request($request,$transferfile);
14381: # did it work?
14382: if ($response->is_error()) {
14383: unlink($transferfile);
14384: &logthis("Userfile repcopy failed for $uri");
14385: return -1;
14386: }
14387: # worked, rename the transfer file
14388: rename($transferfile,$file);
1.607 raeburn 14389: return 'ok';
1.481 raeburn 14390: }
14391:
1.517 albertel 14392: sub tokenwrapper {
14393: my $uri=shift;
1.980 raeburn 14394: $uri=~s|^https?\://([^/]+)||;
1.552 albertel 14395: $uri=~s|^/||;
1.620 albertel 14396: $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517 albertel 14397: my $token=$1;
1.552 albertel 14398: my (undef,$udom,$uname,$file)=split('/',$uri,4);
14399: if ($udom && $uname && $file) {
14400: $file=~s|(\?\.*)*$||;
1.949 raeburn 14401: &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
1.980 raeburn 14402: my $homeserver = &homeserver($uname,$udom);
1.1172.2.120 raeburn 14403: my $hostname = &hostname($homeserver);
1.980 raeburn 14404: my $protocol = $protocol{$homeserver};
14405: $protocol = 'http' if ($protocol ne 'https');
1.1172.2.120 raeburn 14406: return $protocol.'://'.$hostname.'/'.$uri.
1.517 albertel 14407: (($uri=~/\?/)?'&':'?').'token='.$token.
14408: '&tokenissued='.$perlvar{'lonHostID'};
14409: } else {
14410: return '/adm/notfound.html';
14411: }
14412: }
14413:
1.828 www 14414: # call with reqtype HEAD: get last modification time
14415: # call with reqtype GET: get the file contents
14416: # Do not call this with reqtype GET for large files! It loads everything into memory
14417: #
1.481 raeburn 14418: sub getuploaded {
14419: my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
14420: $uri=~s/^\///;
1.980 raeburn 14421: my $homeserver = &homeserver($cnum,$cdom);
1.1172.2.120 raeburn 14422: my $hostname = &hostname($homeserver);
1.980 raeburn 14423: my $protocol = $protocol{$homeserver};
14424: $protocol = 'http' if ($protocol ne 'https');
1.1172.2.120 raeburn 14425: $uri = $protocol.'://'.$hostname.'/raw/'.$uri;
1.481 raeburn 14426: my $ua=new LWP::UserAgent;
14427: my $request=new HTTP::Request($reqtype,$uri);
14428: my $response=$ua->request($request);
14429: $$rtncode = $response->code;
1.482 albertel 14430: if (! $response->is_success()) {
14431: return 'failed';
14432: }
14433: if ($reqtype eq 'HEAD') {
1.486 www 14434: $$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482 albertel 14435: } elsif ($reqtype eq 'GET') {
14436: $$info = $response->content;
1.472 albertel 14437: }
1.482 albertel 14438: return 'ok';
1.36 albertel 14439: }
14440:
1.481 raeburn 14441: sub readfile {
14442: my $file = shift;
14443: if ( (! -e $file ) || ($file eq '') ) { return -1; };
14444: my $fh;
1.1172.2.96 raeburn 14445: open($fh,"<",$file);
1.481 raeburn 14446: my $a='';
1.800 albertel 14447: while (my $line = <$fh>) { $a .= $line; }
1.481 raeburn 14448: return $a;
14449: }
14450:
1.36 albertel 14451: sub filelocation {
1.590 banghart 14452: my ($dir,$file) = @_;
14453: my $location;
14454: $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700 albertel 14455:
14456: if ($file =~ m-^/adm/-) {
14457: $file=~s-^/adm/wrapper/-/-;
14458: $file=~s-^/adm/coursedocs/showdoc/-/-;
14459: }
1.882 albertel 14460:
1.1139 www 14461: if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
1.956 raeburn 14462: $location = $file;
1.609 banghart 14463: } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590 banghart 14464: my ($udom,$uname,$filename)=
1.811 albertel 14465: ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590 banghart 14466: my $home=&homeserver($uname,$udom);
14467: my $is_me=0;
14468: my @ids=¤t_machine_ids();
14469: foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
14470: if ($is_me) {
1.1117 foxr 14471: $location=propath($udom,$uname).'/userfiles/'.$filename;
1.590 banghart 14472: } else {
14473: $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
14474: $udom.'/'.$uname.'/'.$filename;
14475: }
1.882 albertel 14476: } elsif ($file =~ m-^/adm/-) {
14477: $location = $perlvar{'lonDocRoot'}.'/'.$file;
1.590 banghart 14478: } else {
14479: $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.1139 www 14480: $file=~s:^/(res|priv)/:/:;
14481: my $space=$1;
1.590 banghart 14482: if ( !( $file =~ m:^/:) ) {
14483: $location = $dir. '/'.$file;
14484: } else {
1.1142 raeburn 14485: $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
1.590 banghart 14486: }
1.59 albertel 14487: }
1.590 banghart 14488: $location=~s://+:/:g; # remove duplicate /
1.930 albertel 14489: while ($location=~m{/\.\./}) {
14490: if ($location =~ m{/[^/]+/\.\./}) {
14491: $location=~ s{/[^/]+/\.\./}{/}g;
14492: } else {
14493: $location=~ s{/\.\./}{/}g;
14494: }
14495: } #remove dir/..
1.590 banghart 14496: while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
14497: return $location;
1.46 www 14498: }
1.36 albertel 14499:
1.46 www 14500: sub hreflocation {
14501: my ($dir,$file)=@_;
1.980 raeburn 14502: unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
1.666 albertel 14503: $file=filelocation($dir,$file);
1.700 albertel 14504: } elsif ($file=~m-^/adm/-) {
14505: $file=~s-^/adm/wrapper/-/-;
14506: $file=~s-^/adm/coursedocs/showdoc/-/-;
1.666 albertel 14507: }
14508: if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
14509: $file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
14510: } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.1143 raeburn 14511: $file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
14512: {/uploaded/$1/$2/}x;
1.46 www 14513: }
1.913 albertel 14514: if ($file=~ m{^/userfiles/}) {
14515: $file =~ s{^/userfiles/}{/uploaded/};
14516: }
1.462 albertel 14517: return $file;
1.465 albertel 14518: }
14519:
1.1139 www 14520:
14521:
14522:
14523:
1.465 albertel 14524: sub current_machine_domains {
1.853 albertel 14525: return &machine_domains(&hostname($perlvar{'lonHostID'}));
14526: }
14527:
14528: sub machine_domains {
14529: my ($hostname) = @_;
1.465 albertel 14530: my @domains;
1.838 albertel 14531: my %hostname = &all_hostnames();
1.465 albertel 14532: while( my($id, $name) = each(%hostname)) {
1.467 matthew 14533: # &logthis("-$id-$name-$hostname-");
1.465 albertel 14534: if ($hostname eq $name) {
1.844 albertel 14535: push(@domains,&host_domain($id));
1.465 albertel 14536: }
14537: }
14538: return @domains;
14539: }
14540:
14541: sub current_machine_ids {
1.853 albertel 14542: return &machine_ids(&hostname($perlvar{'lonHostID'}));
14543: }
14544:
14545: sub machine_ids {
14546: my ($hostname) = @_;
14547: $hostname ||= &hostname($perlvar{'lonHostID'});
1.465 albertel 14548: my @ids;
1.888 albertel 14549: my %name_to_host = &all_names();
1.889 albertel 14550: if (ref($name_to_host{$hostname}) eq 'ARRAY') {
14551: return @{ $name_to_host{$hostname} };
14552: }
14553: return;
1.31 www 14554: }
14555:
1.824 raeburn 14556: sub additional_machine_domains {
14557: my @domains;
1.1172.2.142 raeburn 14558: if (-e "$perlvar{'lonTabDir'}/expected_domains.tab") {
14559: if (open(my $fh,"<","$perlvar{'lonTabDir'}/expected_domains.tab")) {
14560: while( my $line = <$fh>) {
14561: chomp($line);
14562: $line =~ s/\s//g;
14563: push(@domains,$line);
14564: }
14565: close($fh);
14566: }
1.824 raeburn 14567: }
14568: return @domains;
14569: }
14570:
14571: sub default_login_domain {
14572: my $domain = $perlvar{'lonDefDomain'};
14573: my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
14574: foreach my $posdom (¤t_machine_domains(),
14575: &additional_machine_domains()) {
14576: if (lc($posdom) eq lc($testdomain)) {
14577: $domain=$posdom;
14578: last;
14579: }
14580: }
14581: return $domain;
14582: }
14583:
1.1172.2.106 raeburn 14584: sub shared_institution {
1.1172.2.136 raeburn 14585: my ($dom,$lonhost) = @_;
14586: if ($lonhost eq '') {
14587: $lonhost = $perlvar{'lonHostID'};
14588: }
1.1172.2.106 raeburn 14589: my $same_intdom;
1.1172.2.136 raeburn 14590: my $hostintdom = &internet_dom($lonhost);
1.1172.2.106 raeburn 14591: if ($hostintdom ne '') {
14592: my %iphost = &get_iphost();
14593: my $primary_id = &domain($dom,'primary');
14594: my $primary_ip = &get_host_ip($primary_id);
14595: if (ref($iphost{$primary_ip}) eq 'ARRAY') {
14596: foreach my $id (@{$iphost{$primary_ip}}) {
14597: my $intdom = &internet_dom($id);
14598: if ($intdom eq $hostintdom) {
14599: $same_intdom = 1;
14600: last;
14601: }
14602: }
14603: }
14604: }
14605: return $same_intdom;
14606: }
14607:
1.1172.2.120 raeburn 14608: sub uses_sts {
14609: my ($ignore_cache) = @_;
14610: my $lonhost = $perlvar{'lonHostID'};
14611: my $hostname = &hostname($lonhost);
14612: my $sts_on;
14613: if ($protocol{$lonhost} eq 'https') {
14614: my $cachetime = 12*3600;
14615: if (!$ignore_cache) {
14616: ($sts_on,my $cached)=&is_cached_new('stspolicy',$lonhost);
14617: if (defined($cached)) {
14618: return $sts_on;
14619: }
14620: }
1.1172.2.121 raeburn 14621: my $ua=new LWP::UserAgent;
1.1172.2.120 raeburn 14622: my $url = $protocol{$lonhost}.'://'.$hostname.'/index.html';
14623: my $request=new HTTP::Request('HEAD',$url);
1.1172.2.121 raeburn 14624: my $response=$ua->request($request);
1.1172.2.120 raeburn 14625: if ($response->is_success) {
14626: my $has_sts = $response->header('Strict-Transport-Security');
14627: if ($has_sts eq '') {
14628: $sts_on = 0;
14629: } else {
14630: if ($has_sts =~ /\Qmax-age=\E(\d+)/) {
14631: my $maxage = $1;
14632: if ($maxage) {
14633: $sts_on = 1;
14634: } else {
14635: $sts_on = 0;
14636: }
14637: } else {
14638: $sts_on = 0;
14639: }
14640: }
14641: return &do_cache_new('stspolicy',$lonhost,$sts_on,$cachetime);
14642: }
14643: }
14644: return;
14645: }
14646:
1.1172.2.142 raeburn 14647: sub waf_allssl {
14648: my ($host_name) = @_;
14649: my $alias = &get_proxy_alias();
14650: if ($host_name eq '') {
14651: $host_name = $ENV{'SERVER_NAME'};
14652: }
14653: if (($host_name ne '') && ($alias eq $host_name)) {
14654: my $serverhomedom = &host_domain($perlvar{'lonHostID'});
14655: my %defdomdefaults = &get_domain_defaults($serverhomedom);
14656: if ($defdomdefaults{'waf_sslopt'}) {
14657: return $defdomdefaults{'waf_sslopt'};
14658: }
14659: }
14660: return;
14661: }
14662:
1.1172.2.134 raeburn 14663: sub get_requestor_ip {
14664: my ($r,$nolookup,$noproxy) = @_;
14665: my $from_ip;
14666: if (ref($r)) {
1.1172.2.142 raeburn 14667: if ($r->can('useragent_ip')) {
14668: if ($noproxy && $r->can('client_ip')) {
14669: $from_ip = $r->client_ip();
14670: } else {
14671: $from_ip = $r->useragent_ip();
14672: }
14673: } elsif ($r->connection->can('remote_ip')) {
14674: $from_ip = $r->connection->remote_ip();
14675: } else {
14676: $from_ip = $r->get_remote_host($nolookup);
14677: }
1.1172.2.134 raeburn 14678: } else {
14679: $from_ip = $ENV{'REMOTE_ADDR'};
14680: }
1.1172.2.142 raeburn 14681: return $from_ip if ($noproxy);
14682: # Who controls proxy settings for server
14683: my $dom_in_use = $Apache::lonnet::perlvar{'lonDefDomain'};
14684: my $proxyinfo = &get_proxy_settings($dom_in_use);
14685: if ((ref($proxyinfo) eq 'HASH') && ($from_ip)) {
14686: if ($proxyinfo->{'vpnint'}) {
14687: if (&ip_match($from_ip,$proxyinfo->{'vpnint'})) {
14688: return $from_ip;
14689: }
14690: }
14691: if ($proxyinfo->{'trusted'}) {
14692: if (&ip_match($from_ip,$proxyinfo->{'trusted'})) {
14693: my $ipheader = $proxyinfo->{'ipheader'};
14694: my ($ip,$xfor);
14695: if (ref($r)) {
14696: if ($ipheader) {
14697: $ip = $r->headers_in->{$ipheader};
14698: }
14699: $xfor = $r->headers_in->{'X-Forwarded-For'};
14700: } else {
14701: if ($ipheader) {
14702: $ip = $ENV{'HTTP_'.uc($ipheader)};
14703: }
14704: $xfor = $ENV{'HTTP_X_FORWARDED_FOR'};
14705: }
14706: if (($ip eq '') && ($xfor ne '')) {
14707: foreach my $poss_ip (reverse(split(/\s*,\s*/,$xfor))) {
14708: unless (&ip_match($poss_ip,$proxyinfo->{'trusted'})) {
14709: $ip = $poss_ip;
14710: last;
14711: }
14712: }
14713: }
14714: if ($ip ne '') {
14715: return $ip;
14716: }
14717: }
14718: }
14719: }
1.1172.2.134 raeburn 14720: return $from_ip;
14721: }
14722:
1.1172.2.142 raeburn 14723: sub get_proxy_settings {
14724: my ($dom_in_use) = @_;
14725: my %domdefaults = &Apache::lonnet::get_domain_defaults($dom_in_use);
14726: my $proxyinfo = {
14727: ipheader => $domdefaults{'waf_ipheader'},
14728: trusted => $domdefaults{'waf_trusted'},
14729: vpnint => $domdefaults{'waf_vpnint'},
14730: vpnext => $domdefaults{'waf_vpnext'},
14731: sslopt => $domdefaults{'waf_sslopt'},
14732: };
14733: return $proxyinfo;
14734: }
14735:
14736: sub ip_match {
14737: my ($ip,$pattern_str) = @_;
14738: $ip=Net::CIDR::cidrvalidate($ip);
14739: if ($ip) {
14740: return Net::CIDR::cidrlookup($ip,split(/\s*,\s*/,$pattern_str));
14741: }
14742: return;
14743: }
14744:
14745: sub get_proxy_alias {
14746: my ($lonid) = @_;
14747: if ($lonid eq '') {
14748: $lonid = $perlvar{'lonHostID'};
14749: }
14750: if (!defined(&hostname($lonid))) {
14751: return;
14752: }
14753: if ($lonid ne '') {
14754: my ($alias,$cached) = &is_cached_new('proxyalias',$lonid);
14755: if ($cached) {
14756: return $alias;
14757: }
14758: my $dom = &Apache::lonnet::host_domain($lonid);
14759: if ($dom ne '') {
14760: my $cachetime = 60*60*24;
14761: my %domconfig =
14762: &Apache::lonnet::get_dom('configuration',['wafproxy'],$dom);
14763: if (ref($domconfig{'wafproxy'}) eq 'HASH') {
14764: if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
14765: $alias = $domconfig{'wafproxy'}{'alias'}{$lonid};
14766: }
14767: }
14768: return &do_cache_new('proxyalias',$lonid,$alias,$cachetime);
14769: }
14770: }
14771: return;
14772: }
14773:
14774: sub use_proxy_alias {
14775: my ($r,$lonid) = @_;
14776: my $alias = &get_proxy_alias($lonid);
14777: if ($alias) {
14778: my $dom = &host_domain($lonid);
14779: if ($dom ne '') {
14780: my $proxyinfo = &get_proxy_settings($dom);
14781: my ($vpnint,$remote_ip);
14782: if (ref($proxyinfo) eq 'HASH') {
14783: $vpnint = $proxyinfo->{'vpnint'};
14784: if ($vpnint) {
14785: $remote_ip = &get_requestor_ip($r,1,1);
14786: }
14787: }
14788: unless ($vpnint && &ip_match($remote_ip,$vpnint)) {
14789: return $alias;
14790: }
14791: }
14792: }
14793: return;
14794: }
14795:
14796: sub alias_sso {
14797: my ($lonid) = @_;
14798: if ($lonid eq '') {
14799: $lonid = $perlvar{'lonHostID'};
14800: }
14801: if (!defined(&hostname($lonid))) {
14802: return;
14803: }
14804: if ($lonid ne '') {
14805: my ($use_alias,$cached) = &is_cached_new('proxysaml',$lonid);
14806: if ($cached) {
14807: return $use_alias;
14808: }
14809: my $dom = &Apache::lonnet::host_domain($lonid);
14810: if ($dom ne '') {
14811: my $cachetime = 60*60*24;
14812: my %domconfig =
14813: &Apache::lonnet::get_dom('configuration',['wafproxy'],$dom);
14814: if (ref($domconfig{'wafproxy'}) eq 'HASH') {
14815: if (ref($domconfig{'wafproxy'}{'saml'}) eq 'HASH') {
14816: $use_alias = $domconfig{'wafproxy'}{'saml'}{$lonid};
14817: }
14818: }
14819: return &do_cache_new('proxysaml',$lonid,$use_alias,$cachetime);
14820: }
14821: }
14822: return;
14823: }
14824:
14825: sub get_saml_landing {
14826: my ($lonid) = @_;
14827: if ($lonid eq '') {
14828: my $defdom = &default_login_domain();
14829: my @hosts = ¤t_machine_ids();
14830: if (@hosts > 1) {
14831: foreach my $hostid (@hosts) {
14832: if (&host_domain($hostid) eq $defdom) {
14833: $lonid = $hostid;
14834: last;
14835: }
14836: }
14837: } else {
14838: $lonid = $perlvar{'lonHostID'};
14839: }
14840: if ($lonid) {
14841: unless (&Apache::lonnet::host_domain($lonid) eq $defdom) {
14842: return;
14843: }
14844: } else {
14845: return;
14846: }
14847: } elsif (!defined(&hostname($lonid))) {
14848: return;
14849: }
14850: my ($landing,$cached) = &is_cached_new('samllanding',$lonid);
14851: if ($cached) {
14852: return $landing;
14853: }
14854: my $dom = &Apache::lonnet::host_domain($lonid);
14855: if ($dom ne '') {
14856: my $cachetime = 60*60*24;
14857: my %domconfig =
14858: &Apache::lonnet::get_dom('configuration',['login'],$dom);
14859: if (ref($domconfig{'login'}) eq 'HASH') {
14860: if (ref($domconfig{'login'}{'saml'}) eq 'HASH') {
14861: if (ref($domconfig{'login'}{'saml'}{$lonid}) eq 'HASH') {
14862: $landing = 1;
14863: }
14864: }
14865: }
14866: return &do_cache_new('samllanding',$lonid,$landing,$cachetime);
14867: }
14868: return;
14869: }
14870:
1.31 www 14871: # ------------------------------------------------------------- Declutters URLs
14872:
14873: sub declutter {
14874: my $thisfn=shift;
1.569 albertel 14875: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.1172.2.49 raeburn 14876: unless ($thisfn=~m{^/home/httpd/html/priv/}) {
14877: $thisfn=~s{^/home/httpd/html}{};
14878: }
1.31 www 14879: $thisfn=~s/^\///;
1.697 albertel 14880: $thisfn=~s|^adm/wrapper/||;
14881: $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31 www 14882: $thisfn=~s/^res\///;
1.1172 bisitz 14883: $thisfn=~s/^priv\///;
1.1032 raeburn 14884: unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
14885: $thisfn=~s/\?.+$//;
14886: }
1.268 www 14887: return $thisfn;
14888: }
14889:
14890: # ------------------------------------------------------------- Clutter up URLs
14891:
14892: sub clutter {
14893: my $thisfn='/'.&declutter(shift);
1.887 albertel 14894: if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
1.884 albertel 14895: || $thisfn =~ m{^/adm/(includes|pages)} ) {
1.270 www 14896: $thisfn='/res'.$thisfn;
14897: }
1.1031 raeburn 14898: if ($thisfn !~m|^/adm|) {
14899: if ($thisfn =~ m|^/ext/|) {
1.694 albertel 14900: $thisfn='/adm/wrapper'.$thisfn;
1.695 albertel 14901: } else {
14902: my ($ext) = ($thisfn =~ /\.(\w+)$/);
14903: my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698 albertel 14904: if ($embstyle eq 'ssi'
14905: || ($embstyle eq 'hdn')
14906: || ($embstyle eq 'rat')
14907: || ($embstyle eq 'prv')
14908: || ($embstyle eq 'ign')) {
14909: #do nothing with these
14910: } elsif (($embstyle eq 'img')
1.695 albertel 14911: || ($embstyle eq 'emb')
14912: || ($embstyle eq 'wrp')) {
14913: $thisfn='/adm/wrapper'.$thisfn;
1.698 albertel 14914: } elsif ($embstyle eq 'unk'
14915: && $thisfn!~/\.(sequence|page)$/) {
1.695 albertel 14916: $thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698 albertel 14917: } else {
1.718 www 14918: # &logthis("Got a blank emb style");
1.695 albertel 14919: }
1.694 albertel 14920: }
1.1172.2.146. .1(raebu 14921:22): } elsif ($thisfn =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
14922:22): $thisfn='/adm/wrapper'.$thisfn;
1.694 albertel 14923: }
1.31 www 14924: return $thisfn;
1.12 www 14925: }
14926:
1.787 albertel 14927: sub clutter_with_no_wrapper {
14928: my $uri = &clutter(shift);
14929: if ($uri =~ m-^/adm/-) {
14930: $uri =~ s-^/adm/wrapper/-/-;
14931: $uri =~ s-^/adm/coursedocs/showdoc/-/-;
14932: }
14933: return $uri;
14934: }
14935:
1.557 albertel 14936: sub freeze_escape {
14937: my ($value)=@_;
14938: if (ref($value)) {
14939: $value=&nfreeze($value);
14940: return '__FROZEN__'.&escape($value);
14941: }
14942: return &escape($value);
14943: }
14944:
1.11 www 14945:
1.557 albertel 14946: sub thaw_unescape {
14947: my ($value)=@_;
14948: if ($value =~ /^__FROZEN__/) {
14949: substr($value,0,10,undef);
14950: $value=&unescape($value);
14951: return &thaw($value);
14952: }
14953: return &unescape($value);
14954: }
14955:
1.436 albertel 14956: sub correct_line_ends {
14957: my ($result)=@_;
14958: $$result =~s/\r\n/\n/mg;
14959: $$result =~s/\r/\n/mg;
1.415 albertel 14960: }
1.1 albertel 14961: # ================================================================ Main Program
14962:
1.184 www 14963: sub goodbye {
1.204 albertel 14964: &logthis("Starting Shut down");
1.443 albertel 14965: #not converted to using infrastruture and probably shouldn't be
1.870 albertel 14966: &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
1.443 albertel 14967: #converted
1.599 albertel 14968: # &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
1.870 albertel 14969: &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
14970: # &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
14971: # &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
1.425 albertel 14972: #1.1 only
1.870 albertel 14973: # &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
14974: # &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
14975: # &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
14976: # &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
14977: &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
1.599 albertel 14978: &logthis(sprintf("%-20s is %s",'kicks',$kicks));
14979: &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184 www 14980: &flushcourselogs();
14981: &logthis("Shutting down");
14982: }
14983:
1.852 albertel 14984: sub get_dns {
1.1172.2.17 raeburn 14985: my ($url,$func,$ignore_cache,$nocache,$hashref) = @_;
1.869 albertel 14986: if (!$ignore_cache) {
14987: my ($content,$cached)=
14988: &Apache::lonnet::is_cached_new('dns',$url);
14989: if ($cached) {
1.1172.2.17 raeburn 14990: &$func($content,$hashref);
1.869 albertel 14991: return;
14992: }
14993: }
14994:
14995: my %alldns;
1.1172.2.96 raeburn 14996: if (open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab")) {
14997: foreach my $dns (<$config>) {
14998: next if ($dns !~ /^\^(\S*)/x);
14999: my $line = $1;
15000: my ($host,$protocol) = split(/:/,$line);
15001: if ($protocol ne 'https') {
15002: $protocol = 'http';
15003: }
15004: $alldns{$host} = $protocol;
1.979 raeburn 15005: }
1.1172.2.96 raeburn 15006: close($config);
1.869 albertel 15007: }
15008: while (%alldns) {
1.1172.2.52 raeburn 15009: my ($dns) = sort { $b cmp $a } keys(%alldns);
1.1172.2.142 raeburn 15010: my @content;
15011: if ($dns eq Sys::Hostname::FQDN::fqdn()) {
15012: my $command = (split('/',$url))[3];
15013: my ($dir,$file) = &parse_getdns_url($command,$url);
15014: delete($alldns{$dns});
15015: next if (($dir eq '') || ($file eq ''));
15016: if (open(my $config,'<',"$dir/$file")) {
15017: @content = <$config>;
15018: close($config);
15019: }
15020: } else {
15021: my $ua=new LWP::UserAgent;
15022: $ua->timeout(30);
15023: my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
15024: my $response=$ua->request($request);
15025: delete($alldns{$dns});
15026: next if ($response->is_error());
15027: @content = split("\n",$response->content);
15028: }
1.1172.2.17 raeburn 15029: unless ($nocache) {
1.1172.2.23 raeburn 15030: &do_cache_new('dns',$url,\@content,30*24*60*60);
1.1172.2.17 raeburn 15031: }
15032: &$func(\@content,$hashref);
1.869 albertel 15033: return;
1.852 albertel 15034: }
1.871 albertel 15035: my $which = (split('/',$url))[3];
15036: &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
1.1172.2.105 raeburn 15037: if (open(my $config,"<","$perlvar{'lonTabDir'}/dns_$which.tab")) {
15038: my @content = <$config>;
15039: &$func(\@content,$hashref);
15040: }
1.1172.2.17 raeburn 15041: return;
15042: }
15043:
15044: # ------------------------------------------------------Get DNS checksums file
15045: sub parse_dns_checksums_tab {
15046: my ($lines,$hashref) = @_;
1.1172.2.53 raeburn 15047: my $lonhost = $perlvar{'lonHostID'};
15048: my $machine_dom = &Apache::lonnet::host_domain($lonhost);
1.1172.2.17 raeburn 15049: my $loncaparev = &get_server_loncaparev($machine_dom);
1.1172.2.53 raeburn 15050: my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
15051: my $webconfdir = '/etc/httpd/conf';
15052: if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
15053: $webconfdir = '/etc/apache2';
15054: } elsif ($distro =~ /^sles(\d+)$/) {
15055: if ($1 >= 10) {
15056: $webconfdir = '/etc/apache2';
15057: }
15058: } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
15059: if ($1 >= 10.0) {
15060: $webconfdir = '/etc/apache2';
15061: }
15062: }
1.1172.2.17 raeburn 15063: my ($release,$timestamp) = split(/\-/,$loncaparev);
15064: my (%chksum,%revnum);
15065: if (ref($lines) eq 'ARRAY') {
15066: chomp(@{$lines});
1.1172.2.34 raeburn 15067: my $version = shift(@{$lines});
15068: if ($version eq $release) {
1.1172.2.17 raeburn 15069: foreach my $line (@{$lines}) {
1.1172.2.34 raeburn 15070: my ($file,$version,$shasum) = split(/,/,$line);
1.1172.2.53 raeburn 15071: if ($file =~ m{^/etc/httpd/conf}) {
15072: if ($webconfdir eq '/etc/apache2') {
15073: $file =~ s{^\Q/etc/httpd/conf/\E}{$webconfdir/};
15074: }
15075: }
1.1172.2.34 raeburn 15076: $chksum{$file} = $shasum;
15077: $revnum{$file} = $version;
1.1172.2.17 raeburn 15078: }
15079: if (ref($hashref) eq 'HASH') {
15080: %{$hashref} = (
15081: sums => \%chksum,
15082: versions => \%revnum,
15083: );
15084: }
15085: }
15086: }
1.869 albertel 15087: return;
1.852 albertel 15088: }
1.1172.2.17 raeburn 15089:
15090: sub fetch_dns_checksums {
15091: my %checksums;
1.1172.2.34 raeburn 15092: my $machine_dom = &Apache::lonnet::host_domain($perlvar{'lonHostID'});
1.1172.2.48 raeburn 15093: my $loncaparev = &get_server_loncaparev($machine_dom,$perlvar{'lonHostID'});
1.1172.2.34 raeburn 15094: my ($release,$timestamp) = split(/\-/,$loncaparev);
15095: &get_dns("/adm/dns/checksums/$release",\&parse_dns_checksums_tab,1,1,
1.1172.2.17 raeburn 15096: \%checksums);
15097: return \%checksums;
15098: }
15099:
1.1172.2.142 raeburn 15100: sub parse_getdns_url {
15101: my ($command,$url) = @_;
15102: my $dir = $perlvar{'lonTabDir'};
15103: my $file;
15104: if ($command eq 'hosts') {
15105: $file = 'dns_hosts.tab';
15106: } elsif ($command eq 'domain') {
15107: $file = 'dns_domain.tab';
15108: } elsif ($command eq 'checksums') {
15109: my $version = (split('/',$url))[4];
15110: $file = "dns_checksums/$version.tab",
15111: }
15112: return ($dir,$file);
15113: }
15114:
1.327 albertel 15115: # ------------------------------------------------------------ Read domain file
15116: {
1.852 albertel 15117: my $loaded;
1.846 albertel 15118: my %domain;
15119:
1.852 albertel 15120: sub parse_domain_tab {
15121: my ($lines) = @_;
15122: foreach my $line (@$lines) {
15123: next if ($line =~ /^(\#|\s*$ )/x);
1.403 www 15124:
1.846 albertel 15125: chomp($line);
1.852 albertel 15126: my ($name,@elements) = split(/:/,$line,9);
1.846 albertel 15127: my %this_domain;
15128: foreach my $field ('description', 'auth_def', 'auth_arg_def',
15129: 'lang_def', 'city', 'longi', 'lati',
15130: 'primary') {
15131: $this_domain{$field} = shift(@elements);
15132: }
15133: $domain{$name} = \%this_domain;
1.852 albertel 15134: }
15135: }
1.864 albertel 15136:
15137: sub reset_domain_info {
15138: undef($loaded);
15139: undef(%domain);
15140: }
15141:
1.852 albertel 15142: sub load_domain_tab {
1.1172.2.70 raeburn 15143: my ($ignore_cache,$nocache) = @_;
15144: &get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache,$nocache);
1.852 albertel 15145: my $fh;
1.1172.2.96 raeburn 15146: if (open($fh,"<",$perlvar{'lonTabDir'}.'/domain.tab')) {
1.852 albertel 15147: my @lines = <$fh>;
15148: &parse_domain_tab(\@lines);
1.448 albertel 15149: }
1.852 albertel 15150: close($fh);
15151: $loaded = 1;
1.327 albertel 15152: }
1.846 albertel 15153:
15154: sub domain {
1.852 albertel 15155: &load_domain_tab() if (!$loaded);
15156:
1.846 albertel 15157: my ($name,$what) = @_;
15158: return if ( !exists($domain{$name}) );
15159:
15160: if (!$what) {
15161: return $domain{$name}{'description'};
15162: }
15163: return $domain{$name}{$what};
15164: }
1.974 raeburn 15165:
15166: sub domain_info {
15167: &load_domain_tab() if (!$loaded);
15168: return %domain;
15169: }
15170:
1.327 albertel 15171: }
15172:
15173:
1.1 albertel 15174: # ------------------------------------------------------------- Read hosts file
15175: {
1.838 albertel 15176: my %hostname;
1.844 albertel 15177: my %hostdom;
1.845 albertel 15178: my %libserv;
1.852 albertel 15179: my $loaded;
1.888 albertel 15180: my %name_to_host;
1.1074 raeburn 15181: my %internetdom;
1.1107 raeburn 15182: my %LC_dns_serv;
1.852 albertel 15183:
15184: sub parse_hosts_tab {
15185: my ($file) = @_;
15186: foreach my $configline (@$file) {
15187: next if ($configline =~ /^(\#|\s*$ )/x);
1.1107 raeburn 15188: chomp($configline);
15189: if ($configline =~ /^\^/) {
15190: if ($configline =~ /^\^([\w.\-]+)/) {
15191: $LC_dns_serv{$1} = 1;
15192: }
15193: next;
15194: }
1.1074 raeburn 15195: my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
1.852 albertel 15196: $name=~s/\s//g;
15197: if ($id && $domain && $role && $name) {
1.1172.2.96 raeburn 15198: if ((exists($hostname{$id})) && ($hostname{$id} ne '')) {
15199: my $curr = $hostname{$id};
15200: my $skip;
15201: if (ref($name_to_host{$curr}) eq 'ARRAY') {
15202: if (($curr eq $name) && (@{$name_to_host{$curr}} == 1)) {
15203: $skip = 1;
15204: } else {
15205: @{$name_to_host{$curr}} = grep { $_ ne $id } @{$name_to_host{$curr}};
15206: }
15207: }
15208: unless ($skip) {
15209: push(@{$name_to_host{$name}},$id);
15210: }
15211: } else {
15212: push(@{$name_to_host{$name}},$id);
15213: }
1.852 albertel 15214: $hostname{$id}=$name;
15215: $hostdom{$id}=$domain;
15216: if ($role eq 'library') { $libserv{$id}=$name; }
1.969 raeburn 15217: if (defined($protocol)) {
15218: if ($protocol eq 'https') {
15219: $protocol{$id} = $protocol;
15220: } else {
15221: $protocol{$id} = 'http';
15222: }
1.968 raeburn 15223: } else {
1.969 raeburn 15224: $protocol{$id} = 'http';
1.968 raeburn 15225: }
1.1074 raeburn 15226: if (defined($intdom)) {
15227: $internetdom{$id} = $intdom;
15228: }
1.852 albertel 15229: }
15230: }
15231: }
1.864 albertel 15232:
15233: sub reset_hosts_info {
1.897 albertel 15234: &purge_remembered();
1.864 albertel 15235: &reset_domain_info();
15236: &reset_hosts_ip_info();
1.892 albertel 15237: undef(%name_to_host);
1.864 albertel 15238: undef(%hostname);
15239: undef(%hostdom);
15240: undef(%libserv);
15241: undef($loaded);
15242: }
1.1 albertel 15243:
1.852 albertel 15244: sub load_hosts_tab {
1.1172.2.70 raeburn 15245: my ($ignore_cache,$nocache) = @_;
15246: &get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache,$nocache);
1.1172.2.96 raeburn 15247: open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab");
1.852 albertel 15248: my @config = <$config>;
15249: &parse_hosts_tab(\@config);
15250: close($config);
15251: $loaded=1;
1.1 albertel 15252: }
1.852 albertel 15253:
1.838 albertel 15254: sub hostname {
1.852 albertel 15255: &load_hosts_tab() if (!$loaded);
15256:
1.838 albertel 15257: my ($lonid) = @_;
15258: return $hostname{$lonid};
15259: }
1.845 albertel 15260:
1.838 albertel 15261: sub all_hostnames {
1.852 albertel 15262: &load_hosts_tab() if (!$loaded);
15263:
1.838 albertel 15264: return %hostname;
15265: }
1.845 albertel 15266:
1.888 albertel 15267: sub all_names {
1.1172.2.70 raeburn 15268: my ($ignore_cache,$nocache) = @_;
15269: &load_hosts_tab($ignore_cache,$nocache) if (!$loaded);
1.888 albertel 15270:
15271: return %name_to_host;
15272: }
15273:
1.974 raeburn 15274: sub all_host_domain {
15275: &load_hosts_tab() if (!$loaded);
15276: return %hostdom;
15277: }
15278:
1.845 albertel 15279: sub is_library {
1.852 albertel 15280: &load_hosts_tab() if (!$loaded);
15281:
1.845 albertel 15282: return exists($libserv{$_[0]});
15283: }
15284:
15285: sub all_library {
1.852 albertel 15286: &load_hosts_tab() if (!$loaded);
15287:
1.845 albertel 15288: return %libserv;
15289: }
15290:
1.1062 droeschl 15291: sub unique_library {
15292: #2x reverse removes all hostnames that appear more than once
15293: my %unique = reverse &all_library();
15294: return reverse %unique;
15295: }
15296:
1.841 albertel 15297: sub get_servers {
1.852 albertel 15298: &load_hosts_tab() if (!$loaded);
15299:
1.841 albertel 15300: my ($domain,$type) = @_;
15301: my %possible_hosts = ($type eq 'library') ? %libserv
15302: : %hostname;
15303: my %result;
1.842 albertel 15304: if (ref($domain) eq 'ARRAY') {
15305: while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843 albertel 15306: if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842 albertel 15307: $result{$host} = $hostname;
15308: }
15309: }
15310: } else {
15311: while ( my ($host,$hostname) = each(%possible_hosts)) {
15312: if ($hostdom{$host} eq $domain) {
15313: $result{$host} = $hostname;
15314: }
1.841 albertel 15315: }
15316: }
15317: return %result;
15318: }
1.845 albertel 15319:
1.1062 droeschl 15320: sub get_unique_servers {
15321: my %unique = reverse &get_servers(@_);
15322: return reverse %unique;
15323: }
15324:
1.844 albertel 15325: sub host_domain {
1.852 albertel 15326: &load_hosts_tab() if (!$loaded);
15327:
1.844 albertel 15328: my ($lonid) = @_;
15329: return $hostdom{$lonid};
15330: }
15331:
1.841 albertel 15332: sub all_domains {
1.852 albertel 15333: &load_hosts_tab() if (!$loaded);
15334:
1.841 albertel 15335: my %seen;
15336: my @uniq = grep(!$seen{$_}++, values(%hostdom));
15337: return @uniq;
15338: }
1.1074 raeburn 15339:
15340: sub internet_dom {
15341: &load_hosts_tab() if (!$loaded);
15342:
15343: my ($lonid) = @_;
15344: return $internetdom{$lonid};
15345: }
1.1107 raeburn 15346:
15347: sub is_LC_dns {
15348: &load_hosts_tab() if (!$loaded);
15349:
15350: my ($hostname) = @_;
15351: return exists($LC_dns_serv{$hostname});
15352: }
15353:
1.1 albertel 15354: }
15355:
1.847 albertel 15356: {
15357: my %iphost;
1.856 albertel 15358: my %name_to_ip;
15359: my %lonid_to_ip;
1.869 albertel 15360:
1.847 albertel 15361: sub get_hosts_from_ip {
15362: my ($ip) = @_;
15363: my %iphosts = &get_iphost();
15364: if (ref($iphosts{$ip})) {
15365: return @{$iphosts{$ip}};
15366: }
15367: return;
1.839 albertel 15368: }
1.864 albertel 15369:
15370: sub reset_hosts_ip_info {
15371: undef(%iphost);
15372: undef(%name_to_ip);
15373: undef(%lonid_to_ip);
15374: }
1.856 albertel 15375:
15376: sub get_host_ip {
15377: my ($lonid) = @_;
15378: if (exists($lonid_to_ip{$lonid})) {
15379: return $lonid_to_ip{$lonid};
15380: }
15381: my $name=&hostname($lonid);
15382: my $ip = gethostbyname($name);
15383: return if (!$ip || length($ip) ne 4);
15384: $ip=inet_ntoa($ip);
15385: $name_to_ip{$name} = $ip;
15386: $lonid_to_ip{$lonid} = $ip;
15387: return $ip;
15388: }
1.847 albertel 15389:
15390: sub get_iphost {
1.1172.2.70 raeburn 15391: my ($ignore_cache,$nocache) = @_;
1.894 albertel 15392:
1.869 albertel 15393: if (!$ignore_cache) {
15394: if (%iphost) {
15395: return %iphost;
15396: }
15397: my ($ip_info,$cached)=
15398: &Apache::lonnet::is_cached_new('iphost','iphost');
15399: if ($cached) {
15400: %iphost = %{$ip_info->[0]};
15401: %name_to_ip = %{$ip_info->[1]};
15402: %lonid_to_ip = %{$ip_info->[2]};
15403: return %iphost;
15404: }
15405: }
1.894 albertel 15406:
15407: # get yesterday's info for fallback
15408: my %old_name_to_ip;
15409: my ($ip_info,$cached)=
15410: &Apache::lonnet::is_cached_new('iphost','iphost');
15411: if ($cached) {
15412: %old_name_to_ip = %{$ip_info->[1]};
15413: }
15414:
1.1172.2.70 raeburn 15415: my %name_to_host = &all_names($ignore_cache,$nocache);
1.888 albertel 15416: foreach my $name (keys(%name_to_host)) {
1.847 albertel 15417: my $ip;
15418: if (!exists($name_to_ip{$name})) {
15419: $ip = gethostbyname($name);
15420: if (!$ip || length($ip) ne 4) {
1.894 albertel 15421: if (defined($old_name_to_ip{$name})) {
15422: $ip = $old_name_to_ip{$name};
15423: &logthis("Can't find $name defaulting to old $ip");
15424: } else {
15425: &logthis("Name $name no IP found");
15426: next;
15427: }
15428: } else {
15429: $ip=inet_ntoa($ip);
1.847 albertel 15430: }
15431: $name_to_ip{$name} = $ip;
15432: } else {
15433: $ip = $name_to_ip{$name};
1.653 albertel 15434: }
1.888 albertel 15435: foreach my $id (@{ $name_to_host{$name} }) {
15436: $lonid_to_ip{$id} = $ip;
15437: }
15438: push(@{$iphost{$ip}},@{$name_to_host{$name}});
1.598 albertel 15439: }
1.1172.2.70 raeburn 15440: unless ($nocache) {
15441: &do_cache_new('iphost','iphost',
15442: [\%iphost,\%name_to_ip,\%lonid_to_ip],
15443: 48*60*60);
15444: }
1.869 albertel 15445:
1.847 albertel 15446: return %iphost;
1.598 albertel 15447: }
15448:
1.992 raeburn 15449: #
15450: # Given a DNS returns the loncapa host name for that DNS
15451: #
15452: sub host_from_dns {
15453: my ($dns) = @_;
15454: my @hosts;
15455: my $ip;
15456:
1.993 raeburn 15457: if (exists($name_to_ip{$dns})) {
1.992 raeburn 15458: $ip = $name_to_ip{$dns};
15459: }
15460: if (!$ip) {
15461: $ip = gethostbyname($dns); # Initial translation to IP is in net order.
15462: if (length($ip) == 4) {
15463: $ip = &IO::Socket::inet_ntoa($ip);
15464: }
15465: }
15466: if ($ip) {
15467: @hosts = get_hosts_from_ip($ip);
15468: return $hosts[0];
15469: }
15470: return undef;
1.986 foxr 15471: }
1.992 raeburn 15472:
1.1074 raeburn 15473: sub get_internet_names {
15474: my ($lonid) = @_;
15475: return if ($lonid eq '');
15476: my ($idnref,$cached)=
15477: &Apache::lonnet::is_cached_new('internetnames',$lonid);
15478: if ($cached) {
15479: return $idnref;
15480: }
15481: my $ip = &get_host_ip($lonid);
15482: my @hosts = &get_hosts_from_ip($ip);
15483: my %iphost = &get_iphost();
15484: my (@idns,%seen);
15485: foreach my $id (@hosts) {
15486: my $dom = &host_domain($id);
15487: my $prim_id = &domain($dom,'primary');
15488: my $prim_ip = &get_host_ip($prim_id);
15489: next if ($seen{$prim_ip});
15490: if (ref($iphost{$prim_ip}) eq 'ARRAY') {
15491: foreach my $id (@{$iphost{$prim_ip}}) {
15492: my $intdom = &internet_dom($id);
15493: unless (grep(/^\Q$intdom\E$/,@idns)) {
15494: push(@idns,$intdom);
15495: }
15496: }
15497: }
15498: $seen{$prim_ip} = 1;
15499: }
1.1172.2.23 raeburn 15500: return &do_cache_new('internetnames',$lonid,\@idns,12*60*60);
1.1074 raeburn 15501: }
15502:
1.986 foxr 15503: }
15504:
1.1079 raeburn 15505: sub all_loncaparevs {
1.1172.2.39 raeburn 15506: 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 15507: }
15508:
1.1172.2.27 raeburn 15509: # ------------------------------------------------------- Read loncaparev table
15510: {
15511: sub load_loncaparevs {
15512: if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
1.1172.2.96 raeburn 15513: if (open(my $config,"<","$perlvar{'lonTabDir'}/loncaparevs.tab")) {
1.1172.2.27 raeburn 15514: while (my $configline=<$config>) {
15515: chomp($configline);
15516: my ($hostid,$loncaparev)=split(/:/,$configline);
15517: $loncaparevs{$hostid}=$loncaparev;
15518: }
15519: close($config);
15520: }
15521: }
15522: }
15523: }
15524:
15525: # ----------------------------------------------------- Read serverhostID table
15526: {
15527: sub load_serverhomeIDs {
15528: if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
1.1172.2.96 raeburn 15529: if (open(my $config,"<","$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
1.1172.2.27 raeburn 15530: while (my $configline=<$config>) {
15531: chomp($configline);
15532: my ($name,$id)=split(/:/,$configline);
15533: $serverhomeIDs{$name}=$id;
15534: }
15535: close($config);
15536: }
15537: }
15538: }
15539: }
15540:
15541:
1.862 albertel 15542: BEGIN {
15543:
15544: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
15545: unless ($readit) {
15546: {
15547: my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
15548: %perlvar = (%perlvar,%{$configvars});
15549: }
15550:
15551:
1.1 albertel 15552: # ------------------------------------------------------ Read spare server file
15553: {
1.1172.2.96 raeburn 15554: open(my $config,"<","$perlvar{'lonTabDir'}/spare.tab");
1.1 albertel 15555:
15556: while (my $configline=<$config>) {
15557: chomp($configline);
1.284 matthew 15558: if ($configline) {
1.784 albertel 15559: my ($host,$type) = split(':',$configline,2);
1.785 albertel 15560: if (!defined($type) || $type eq '') { $type = 'default' };
1.784 albertel 15561: push(@{ $spareid{$type} }, $host);
1.1 albertel 15562: }
15563: }
1.448 albertel 15564: close($config);
1.1 albertel 15565: }
1.11 www 15566: # ------------------------------------------------------------ Read permissions
15567: {
1.1172.2.96 raeburn 15568: open(my $config,"<","$perlvar{'lonTabDir'}/roles.tab");
1.11 www 15569:
15570: while (my $configline=<$config>) {
1.448 albertel 15571: chomp($configline);
15572: if ($configline) {
15573: my ($role,$perm)=split(/ /,$configline);
15574: if ($perm ne '') { $pr{$role}=$perm; }
15575: }
1.11 www 15576: }
1.448 albertel 15577: close($config);
1.11 www 15578: }
15579:
15580: # -------------------------------------------- Read plain texts for permissions
15581: {
1.1172.2.96 raeburn 15582: open(my $config,"<","$perlvar{'lonTabDir'}/rolesplain.tab");
1.11 www 15583:
15584: while (my $configline=<$config>) {
1.448 albertel 15585: chomp($configline);
15586: if ($configline) {
1.742 raeburn 15587: my ($short,@plain)=split(/:/,$configline);
15588: %{$prp{$short}} = ();
15589: if (@plain > 0) {
15590: $prp{$short}{'std'} = $plain[0];
15591: for (my $i=1; $i<@plain; $i++) {
15592: $prp{$short}{'alt'.$i} = $plain[$i];
15593: }
15594: }
1.448 albertel 15595: }
1.135 www 15596: }
1.448 albertel 15597: close($config);
1.135 www 15598: }
15599:
15600: # ---------------------------------------------------------- Read package table
15601: {
1.1172.2.96 raeburn 15602: open(my $config,"<","$perlvar{'lonTabDir'}/packages.tab");
1.135 www 15603:
15604: while (my $configline=<$config>) {
1.483 albertel 15605: if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448 albertel 15606: chomp($configline);
15607: my ($short,$plain)=split(/:/,$configline);
15608: my ($pack,$name)=split(/\&/,$short);
15609: if ($plain ne '') {
15610: $packagetab{$pack.'&'.$name.'&name'}=$name;
15611: $packagetab{$short}=$plain;
15612: }
1.11 www 15613: }
1.448 albertel 15614: close($config);
1.329 matthew 15615: }
15616:
1.1172.2.27 raeburn 15617: # --------------------------------------------------------- Read loncaparev table
1.1073 raeburn 15618:
1.1172.2.27 raeburn 15619: &load_loncaparevs();
15620:
15621: # ------------------------------------------------------- Read serverhostID table
15622:
15623: &load_serverhomeIDs();
1.1074 raeburn 15624:
1.1172.2.27 raeburn 15625: # ---------------------------------------------------------- Read releaseslist XML
1.1079 raeburn 15626: {
15627: my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
15628: if (-e $file) {
15629: my $parser = HTML::LCParser->new($file);
15630: while (my $token = $parser->get_token()) {
15631: if ($token->[0] eq 'S') {
15632: my $item = $token->[1];
15633: my $name = $token->[2]{'name'};
15634: my $value = $token->[2]{'value'};
15635: if ($item ne '' && $name ne '' && $value ne '') {
15636: my $release = $parser->get_text();
15637: $release =~ s/(^\s*|\s*$ )//gx;
15638: $needsrelease{$item.':'.$name.':'.$value} = $release;
15639: }
15640: }
15641: }
15642: }
1.1073 raeburn 15643: }
15644:
1.1138 raeburn 15645: # ---------------------------------------------------------- Read managers table
15646: {
15647: if (-e "$perlvar{'lonTabDir'}/managers.tab") {
1.1172.2.96 raeburn 15648: if (open(my $config,"<","$perlvar{'lonTabDir'}/managers.tab")) {
1.1138 raeburn 15649: while (my $configline=<$config>) {
15650: chomp($configline);
15651: next if ($configline =~ /^\#/);
15652: if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
15653: $managerstab{$configline} = 1;
15654: }
15655: }
15656: close($config);
15657: }
15658: }
15659: }
15660:
1.329 matthew 15661: # ------------- set up temporary directory
15662: {
1.1117 foxr 15663: $tmpdir = LONCAPA::tempdir();
1.329 matthew 15664:
1.11 www 15665: }
15666:
1.1172.2.104 raeburn 15667: # ------------- set default texengine (domain default overrides this)
15668: {
15669: $deftex = LONCAPA::texengine();
15670: }
15671:
1.1172.2.114 raeburn 15672: # ------------- set default minimum length for passwords for internal auth users
15673: {
15674: $passwdmin = LONCAPA::passwd_min();
15675: }
15676:
1.794 albertel 15677: $memcache=new Cache::Memcached({'servers' => ['127.0.0.1:11211'],
15678: 'compress_threshold'=> 20_000,
15679: });
1.185 www 15680:
1.281 www 15681: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186 www 15682: $dumpcount=0;
1.958 www 15683: $locknum=0;
1.22 www 15684:
1.163 harris41 15685: &logtouch();
1.672 albertel 15686: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195 www 15687: $readit=1;
1.564 albertel 15688: {
15689: use integer;
15690: my $test=(2**32)+1;
1.568 albertel 15691: if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564 albertel 15692: &logthis(" Detected 64bit platform ($_64bit)");
15693: }
1.195 www 15694: }
1.1 albertel 15695: }
1.179 www 15696:
1.1 albertel 15697: 1;
1.191 harris41 15698: __END__
15699:
1.243 albertel 15700: =pod
15701:
1.191 harris41 15702: =head1 NAME
15703:
1.243 albertel 15704: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191 harris41 15705:
15706: =head1 SYNOPSIS
15707:
1.243 albertel 15708: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191 harris41 15709:
15710: &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
15711:
1.243 albertel 15712: Common parameters:
15713:
15714: =over 4
15715:
15716: =item *
15717:
15718: $uname : an internal username (if $cname expecting a course Id specifically)
15719:
15720: =item *
15721:
15722: $udom : a domain (if $cdom expecting a course's domain specifically)
15723:
15724: =item *
15725:
15726: $symb : a resource instance identifier
15727:
15728: =item *
15729:
15730: $namespace : the name of a .db file that contains the data needed or
15731: being set.
15732:
15733: =back
15734:
1.394 bowersj2 15735: =head1 OVERVIEW
1.191 harris41 15736:
1.394 bowersj2 15737: lonnet provides subroutines which interact with the
15738: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
15739: about classes, users, and resources.
1.243 albertel 15740:
15741: For many of these objects you can also use this to store data about
15742: them or modify them in various ways.
1.191 harris41 15743:
1.394 bowersj2 15744: =head2 Symbs
1.191 harris41 15745:
1.394 bowersj2 15746: To identify a specific instance of a resource, LON-CAPA uses symbols
15747: or "symbs"X<symb>. These identifiers are built from the URL of the
15748: map, the resource number of the resource in the map, and the URL of
15749: the resource itself. The latter is somewhat redundant, but might help
15750: if maps change.
15751:
15752: An example is
15753:
15754: msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
15755:
15756: The respective map entry is
15757:
15758: <resource id="19" src="/res/msu/korte/tests/part12.problem"
15759: title="Problem 2">
15760: </resource>
15761:
15762: Symbs are used by the random number generator, as well as to store and
15763: restore data specific to a certain instance of for example a problem.
15764:
15765: =head2 Storing And Retrieving Data
15766:
15767: X<store()>X<cstore()>X<restore()>Three of the most important functions
15768: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
15769: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
15770: is is the non-critical message twin of cstore. These functions are for
15771: handlers to store a perl hash to a user's permanent data space in an
15772: easy manner, and to retrieve it again on another call. It is expected
15773: that a handler would use this once at the beginning to retrieve data,
15774: and then again once at the end to send only the new data back.
15775:
15776: The data is stored in the user's data directory on the user's
15777: homeserver under the ID of the course.
15778:
15779: The hash that is returned by restore will have all of the previous
15780: value for all of the elements of the hash.
15781:
15782: Example:
15783:
15784: #creating a hash
15785: my %hash;
15786: $hash{'foo'}='bar';
15787:
15788: #storing it
15789: &Apache::lonnet::cstore(\%hash);
15790:
15791: #changing a value
15792: $hash{'foo'}='notbar';
15793:
15794: #adding a new value
15795: $hash{'bar'}='foo';
15796: &Apache::lonnet::cstore(\%hash);
15797:
15798: #retrieving the hash
15799: my %history=&Apache::lonnet::restore();
15800:
15801: #print the hash
15802: foreach my $key (sort(keys(%history))) {
15803: print("\%history{$key} = $history{$key}");
15804: }
15805:
15806: Will print out:
1.191 harris41 15807:
1.394 bowersj2 15808: %history{1:foo} = bar
15809: %history{1:keys} = foo:timestamp
15810: %history{1:timestamp} = 990455579
15811: %history{2:bar} = foo
15812: %history{2:foo} = notbar
15813: %history{2:keys} = foo:bar:timestamp
15814: %history{2:timestamp} = 990455580
15815: %history{bar} = foo
15816: %history{foo} = notbar
15817: %history{timestamp} = 990455580
15818: %history{version} = 2
15819:
15820: Note that the special hash entries C<keys>, C<version> and
15821: C<timestamp> were added to the hash. C<version> will be equal to the
15822: total number of versions of the data that have been stored. The
15823: C<timestamp> attribute will be the UNIX time the hash was
15824: stored. C<keys> is available in every historical section to list which
15825: keys were added or changed at a specific historical revision of a
15826: hash.
15827:
15828: B<Warning>: do not store the hash that restore returns directly. This
15829: will cause a mess since it will restore the historical keys as if the
15830: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191 harris41 15831:
1.394 bowersj2 15832: Calling convention:
1.191 harris41 15833:
1.1172.2.27 raeburn 15834: my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname);
1.1172.2.64 raeburn 15835: &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$laststore);
1.191 harris41 15836:
1.394 bowersj2 15837: For more detailed information, see lonnet specific documentation.
1.191 harris41 15838:
1.394 bowersj2 15839: =head1 RETURN MESSAGES
1.191 harris41 15840:
1.394 bowersj2 15841: =over 4
1.191 harris41 15842:
1.394 bowersj2 15843: =item * B<con_lost>: unable to contact remote host
1.191 harris41 15844:
1.394 bowersj2 15845: =item * B<con_delayed>: unable to contact remote host, message will be delivered
15846: when the connection is brought back up
1.191 harris41 15847:
1.394 bowersj2 15848: =item * B<con_failed>: unable to contact remote host and unable to save message
15849: for later delivery
1.191 harris41 15850:
1.967 bisitz 15851: =item * B<error:>: an error a occurred, a description of the error follows the :
1.191 harris41 15852:
1.394 bowersj2 15853: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243 albertel 15854: that was requested
1.191 harris41 15855:
1.243 albertel 15856: =back
1.191 harris41 15857:
1.243 albertel 15858: =head1 PUBLIC SUBROUTINES
1.191 harris41 15859:
1.243 albertel 15860: =head2 Session Environment Functions
1.191 harris41 15861:
1.243 albertel 15862: =over 4
1.191 harris41 15863:
1.394 bowersj2 15864: =item *
15865: X<appenv()>
1.949 raeburn 15866: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
1.394 bowersj2 15867: the user envirnoment file, and will be restored for each access this
1.620 albertel 15868: user makes during this session, also modifies the %env for the current
1.949 raeburn 15869: process. Optional rolesarrayref - if defined contains a reference to an array
15870: of roles which are exempt from the restriction on modifying user.role entries
15871: in the user's environment.db and in %env.
1.191 harris41 15872:
15873: =item *
1.394 bowersj2 15874: X<delenv()>
1.987 raeburn 15875: B<delenv($delthis,$regexp)>: removes all items from the session
15876: environment file that begin with $delthis. If the
15877: optional second arg - $regexp - is true, $delthis is treated as a
15878: regular expression, otherwise \Q$delthis\E is used.
15879: The values are also deleted from the current processes %env.
1.191 harris41 15880:
1.795 albertel 15881: =item * get_env_multiple($name)
15882:
15883: gets $name from the %env hash, it seemlessly handles the cases where multiple
15884: values may be defined and end up as an array ref.
15885:
15886: returns an array of values
15887:
1.243 albertel 15888: =back
15889:
15890: =head2 User Information
1.191 harris41 15891:
1.243 albertel 15892: =over 4
1.191 harris41 15893:
15894: =item *
1.394 bowersj2 15895: X<queryauthenticate()>
15896: B<queryauthenticate($uname,$udom)>: try to determine user's current
1.191 harris41 15897: authentication scheme
15898:
15899: =item *
1.394 bowersj2 15900: X<authenticate()>
1.1073 raeburn 15901: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
1.394 bowersj2 15902: authenticate user from domain's lib servers (first use the current
15903: one). C<$upass> should be the users password.
1.1073 raeburn 15904: $checkdefauth is optional (value is 1 if a check should be made to
15905: authenticate user using default authentication method, and allow
15906: account creation if username does not have account in the domain).
15907: $clientcancheckhost is optional (value is 1 if checking whether the
15908: server can host will occur on the client side in lonauth.pm).
1.191 harris41 15909:
15910: =item *
1.394 bowersj2 15911: X<homeserver()>
15912: B<homeserver($uname,$udom)>: find the server which has
15913: the user's directory and files (there must be only one), this caches
15914: the answer, and also caches if there is a borken connection.
1.191 harris41 15915:
15916: =item *
1.394 bowersj2 15917: X<idget()>
15918: B<idget($udom,@ids)>: find the usernames behind a list of IDs
15919: (IDs are a unique resource in a domain, there must be only 1 ID per
15920: username, and only 1 username per ID in a specific domain) (returns
15921: hash: id=>name,id=>name)
1.191 harris41 15922:
15923: =item *
1.394 bowersj2 15924: X<idrget()>
15925: B<idrget($udom,@unames)>: find the IDs behind a list of
15926: usernames (returns hash: name=>id,name=>id)
1.191 harris41 15927:
15928: =item *
1.394 bowersj2 15929: X<idput()>
15930: B<idput($udom,%ids)>: store away a list of names and associated IDs
1.191 harris41 15931:
15932: =item *
1.394 bowersj2 15933: X<rolesinit()>
1.1169 droeschl 15934: B<rolesinit($udom,$username)>: get user privileges.
15935: returns user role, first access and timer interval hashes
1.243 albertel 15936:
15937: =item *
1.1171 droeschl 15938: X<privileged()>
15939: B<privileged($username,$domain)>: returns a true if user has a
15940: privileged and active role (i.e. su or dc), false otherwise.
15941:
15942: =item *
1.551 albertel 15943: X<getsection()>
15944: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243 albertel 15945: course $cname, return section name/number or '' for "not in course"
15946: and '-1' for "no section"
15947:
15948: =item *
1.394 bowersj2 15949: X<userenvironment()>
15950: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243 albertel 15951: passed in @what from the requested user's environment, returns a hash
15952:
1.858 raeburn 15953: =item *
15954: X<userlog_query()>
1.859 albertel 15955: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
15956: activity.log file. %filters defines filters applied when parsing the
15957: log file. These can be start or end timestamps, or the type of action
15958: - log to look for Login or Logout events, check for Checkin or
15959: Checkout, role for role selection. The response is in the form
15960: timestamp1:hostid1:event1×tamp2:hostid2:event2 where events are
15961: escaped strings of the action recorded in the activity.log file.
1.858 raeburn 15962:
1.243 albertel 15963: =back
15964:
15965: =head2 User Roles
15966:
15967: =over 4
15968:
15969: =item *
15970:
1.1172.2.65 raeburn 15971: allowed($priv,$uri,$symb,$role,$clientip,$noblockcheck) : check for a user privilege;
15972: returns codes for allowed actions.
15973:
15974: The first argument is required, all others are optional.
15975:
15976: $priv is the privilege being checked.
15977: $uri contains additional information about what is being checked for access (e.g.,
15978: URL, course ID etc.).
15979: $symb is the unique resource instance identifier in a course; if needed,
15980: but not provided, it will be retrieved via a call to &symbread().
15981: $role is the role for which a priv is being checked (only used if priv is evb).
15982: $clientip is the user's IP address (only used when checking for access to portfolio
15983: files).
15984: $noblockcheck, if true, skips calls to &has_comm_blocking() for the bre priv. This
15985: prevents recursive calls to &allowed.
15986:
1.243 albertel 15987: F: full access
15988: U,I,K: authentication modes (cxx only)
15989: '': forbidden
15990: 1: user needs to choose course
15991: 2: browse allowed
1.766 albertel 15992: A: passphrase authentication needed
1.1172.2.65 raeburn 15993: B: access temporarily blocked because of a blocking event in a course.
1.1172.2.146. .1(raebu 15994:22): D: access blocked because access is required via session initiated via deep-link
1.243 albertel 15995:
15996: =item *
15997:
1.1172.2.13 raeburn 15998: constructaccess($url,$setpriv) : check for access to construction space URL
15999:
16000: See if the owner domain and name in the URL match those in the
16001: expected environment. If so, return three element list
16002: ($ownername,$ownerdomain,$ownerhome).
16003:
16004: Otherwise return the null string.
16005:
16006: If second argument 'setpriv' is true, it assigns the privileges,
16007: and returns the same three element list, unless the owner has
16008: blocked "ad hoc" Domain Coordinator access to the Author Space,
16009: in which case the null string is returned.
16010:
16011: =item *
16012:
1.1172.2.84 raeburn 16013: definerole($rolename,$sysrole,$domrole,$courole,$uname,$udom) : define role;
16014: define a custom role rolename set privileges in format of lonTabs/roles.tab
16015: for system, domain, and course level. $uname and $udom are optional (current
16016: user's username and domain will be used when either of $uname or $udom are absent.
1.243 albertel 16017:
16018: =item *
16019:
1.988 raeburn 16020: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash
16021: (rolesplain.tab); plain text explanation of a user role term.
1.1008 raeburn 16022: $type is Course (default) or Community.
1.988 raeburn 16023: If $forcedefault evaluates to true, text returned will be default
16024: text for $type. Otherwise, if this is a course, the text returned
16025: will be a custom name for the role (if defined in the course's
16026: environment). If no custom name is defined the default is returned.
16027:
1.832 raeburn 16028: =item *
16029:
1.1172.2.23 raeburn 16030: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv) :
1.858 raeburn 16031: All arguments are optional. Returns a hash of a roles, either for
16032: co-author/assistant author roles for a user's Construction Space
1.906 albertel 16033: (default), or if $context is 'userroles', roles for the user himself,
1.933 raeburn 16034: In the hash, keys are set to colon-separated $uname,$udom,$role, and
16035: (optionally) if $withsec is true, a fourth colon-separated item - $section.
16036: For each key, value is set to colon-separated start and end times for
16037: the role. If no username and domain are specified, will default to
1.934 raeburn 16038: current user/domain. Types, roles, and roledoms are references to arrays
1.858 raeburn 16039: of role statuses (active, future or previous), roles
16040: (e.g., cc,in, st etc.) and domains of the roles which can be used
16041: to restrict the list of roles reported. If no array ref is
16042: provided for types, will default to return only active roles.
1.834 albertel 16043:
1.1172.2.13 raeburn 16044: =item *
16045:
16046: in_course($udom,$uname,$cdom,$cnum,$type,$hideprivileged) : determine if
16047: user: $uname:$udom has a role in the course: $cdom_$cnum.
16048:
16049: Additional optional arguments are: $type (if role checking is to be restricted
16050: to certain user status types -- previous (expired roles), active (currently
16051: available roles) or future (roles available in the future), and
16052: $hideprivileged -- if true will not report course roles for users who
1.1172.2.23 raeburn 16053: have active Domain Coordinator role in course's domain or in additional
16054: domains (specified in 'Domains to check for privileged users' in course
16055: environment -- set via: Course Settings -> Classlists and staff listing).
16056:
16057: =item *
16058:
16059: privileged($username,$domain,$possdomains,$possroles) : returns 1 if user
16060: $username:$domain is a privileged user (e.g., Domain Coordinator or Super User)
16061: $possdomains and $possroles are optional array refs -- to domains to check and
16062: roles to check. If $possdomains is not specified, a dump will be done of the
16063: users' roles.db to check for a dc or su role in any domain. This can be
16064: time consuming if &privileged is called repeatedly (e.g., when displaying a
16065: classlist), so in such cases, supplying a $possdomains array is preferred, as
16066: this then allows &privileged_by_domain() to be used, which caches the identity
16067: of privileged users, eliminating the need for repeated calls to &dump().
16068:
16069: =item *
16070:
16071: privileged_by_domain($possdomains,$roles) : returns a hash of a hash of a hash,
16072: where the outer hash keys are domains specified in the $possdomains array ref,
16073: next inner hash keys are privileged roles specified in the $roles array ref,
16074: and the innermost hash contains key = value pairs for username:domain = end:start
16075: for active or future "privileged" users with that role in that domain. To avoid
16076: repeated dumps of domain roles -- via &get_domain_roles() -- contents of the
16077: innerhash are cached using priv_$role and $dom as the identifiers.
1.1172.2.13 raeburn 16078:
1.243 albertel 16079: =back
16080:
16081: =head2 User Modification
16082:
16083: =over 4
16084:
16085: =item *
16086:
1.957 raeburn 16087: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
1.243 albertel 16088: user for the level given by URL. Optional start and end dates (leave empty
16089: string or zero for "no date")
1.191 harris41 16090:
16091: =item *
16092:
1.243 albertel 16093: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
16094: change a users, password, possible return values are: ok,
16095: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
16096: refused
1.191 harris41 16097:
16098: =item *
16099:
1.243 albertel 16100: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191 harris41 16101:
16102: =item *
16103:
1.1058 raeburn 16104: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
16105: $forceid,$desiredhome,$email,$inststatus,$candelete) :
16106:
16107: will update user information (firstname,middlename,lastname,generation,
16108: permanentemail), and if forceid is true, student/employee ID also.
16109: A user's institutional affiliation(s) can also be updated.
16110: User information fields will not be overwritten with empty entries
16111: unless the field is included in the $candelete array reference.
16112: This array is included when a single user is modified via "Manage Users",
16113: or when Autoupdate.pl is run by cron in a domain.
1.191 harris41 16114:
16115: =item *
16116:
1.286 matthew 16117: modifystudent
16118:
1.957 raeburn 16119: modify a student's enrollment and identification information.
1.1172.2.31 raeburn 16120: The course id is resolved based on the current user's environment.
16121: This means the invoking user must be a course coordinator or otherwise
1.286 matthew 16122: associated with a course.
16123:
1.297 matthew 16124: This call is essentially a wrapper for lonnet::modifyuser and
16125: lonnet::modify_student_enrollment
1.286 matthew 16126:
16127: Inputs:
16128:
16129: =over 4
16130:
1.957 raeburn 16131: =item B<$udom> Student's loncapa domain
1.286 matthew 16132:
1.957 raeburn 16133: =item B<$uname> Student's loncapa login name
1.286 matthew 16134:
1.964 bisitz 16135: =item B<$uid> Student/Employee ID
1.286 matthew 16136:
1.957 raeburn 16137: =item B<$umode> Student's authentication mode
1.286 matthew 16138:
1.957 raeburn 16139: =item B<$upass> Student's password
1.286 matthew 16140:
1.957 raeburn 16141: =item B<$first> Student's first name
1.286 matthew 16142:
1.957 raeburn 16143: =item B<$middle> Student's middle name
1.286 matthew 16144:
1.957 raeburn 16145: =item B<$last> Student's last name
1.286 matthew 16146:
1.957 raeburn 16147: =item B<$gene> Student's generation
1.286 matthew 16148:
1.957 raeburn 16149: =item B<$usec> Student's section in course
1.286 matthew 16150:
16151: =item B<$end> Unix time of the roles expiration
16152:
16153: =item B<$start> Unix time of the roles start date
16154:
16155: =item B<$forceid> If defined, allow $uid to be changed
16156:
16157: =item B<$desiredhome> server to use as home server for student
16158:
1.957 raeburn 16159: =item B<$email> Student's permanent e-mail address
16160:
16161: =item B<$type> Type of enrollment (auto or manual)
16162:
1.963 raeburn 16163: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto
16164:
16165: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
1.957 raeburn 16166:
1.963 raeburn 16167: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
1.957 raeburn 16168:
1.963 raeburn 16169: =item B<$context> role change context (shown in User Management Logs display in a course)
1.957 raeburn 16170:
1.1172.2.19 raeburn 16171: =item B<$inststatus> institutional status of user - : separated string of escaped status types
16172:
16173: =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 16174:
1.286 matthew 16175: =back
1.297 matthew 16176:
16177: =item *
16178:
16179: modify_student_enrollment
16180:
1.1172.2.31 raeburn 16181: Change a student's enrollment status in a class. The environment variable
1.297 matthew 16182: 'role.request.course' must be defined for this function to proceed.
16183:
16184: Inputs:
16185:
16186: =over 4
16187:
1.1172.2.31 raeburn 16188: =item $udom, student's domain
1.297 matthew 16189:
1.1172.2.31 raeburn 16190: =item $uname, student's name
1.297 matthew 16191:
1.1172.2.31 raeburn 16192: =item $uid, student's user id
1.297 matthew 16193:
1.1172.2.31 raeburn 16194: =item $first, student's first name
1.297 matthew 16195:
16196: =item $middle
16197:
16198: =item $last
16199:
16200: =item $gene
16201:
16202: =item $usec
16203:
16204: =item $end
16205:
16206: =item $start
16207:
1.957 raeburn 16208: =item $type
16209:
16210: =item $locktype
16211:
16212: =item $cid
16213:
16214: =item $selfenroll
16215:
16216: =item $context
16217:
1.1172.2.19 raeburn 16218: =item $credits, number of credits student will earn from this class
16219:
1.1172.2.76 raeburn 16220: =item $instsec, institutional course section code for student
16221:
1.297 matthew 16222: =back
16223:
1.191 harris41 16224:
16225: =item *
16226:
1.243 albertel 16227: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
16228: custom role; give a custom role to a user for the level given by URL. Specify
16229: name and domain of role author, and role name
1.191 harris41 16230:
16231: =item *
16232:
1.243 albertel 16233: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191 harris41 16234:
16235: =item *
16236:
1.243 albertel 16237: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
16238:
16239: =back
16240:
16241: =head2 Course Infomation
16242:
16243: =over 4
1.191 harris41 16244:
16245: =item *
16246:
1.1118 foxr 16247: coursedescription($courseid,$options) : returns a hash of information about the
1.631 albertel 16248: specified course id, including all environment settings for the
16249: course, the description of the course will be in the hash under the
16250: key 'description'
1.191 harris41 16251:
1.1118 foxr 16252: $options is an optional parameter that if supplied is a hash reference that controls
16253: what how this function works. It has the following key/values:
16254:
16255: =over 4
16256:
16257: =item freshen_cache
16258:
16259: If defined, and the environment cache for the course is valid, it is
16260: returned in the returned hash.
16261:
16262: =item one_time
16263:
16264: If defined, the last cache time is set to _now_
16265:
16266: =item user
16267:
16268: If defined, the supplied username is used instead of the current user.
16269:
16270:
16271: =back
16272:
1.191 harris41 16273: =item *
16274:
1.624 albertel 16275: resdata($name,$domain,$type,@which) : request for current parameter
16276: setting for a specific $type, where $type is either 'course' or 'user',
16277: @what should be a list of parameters to ask about. This routine caches
1.1172.2.31 raeburn 16278: answers for 10 minutes.
1.243 albertel 16279:
1.877 foxr 16280: =item *
16281:
16282: get_courseresdata($courseid, $domain) : dump the entire course resource
16283: data base, returning a hash that is keyed by the resource name and has
16284: values that are the resource value. I believe that the timestamps and
16285: versions are also returned.
16286:
1.243 albertel 16287: =back
16288:
16289: =head2 Course Modification
16290:
16291: =over 4
1.191 harris41 16292:
16293: =item *
16294:
1.243 albertel 16295: writecoursepref($courseid,%prefs) : write preferences (environment
16296: database) for a course
1.191 harris41 16297:
16298: =item *
16299:
1.1011 raeburn 16300: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
16301:
16302: =item *
16303:
1.1038 raeburn 16304: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
1.243 albertel 16305:
1.1167 droeschl 16306: =item *
16307:
16308: is_course($courseid), is_course($cdom, $cnum)
16309:
16310: Accepts either a combined $courseid (in the form of domain_courseid) or the
16311: two component version $cdom, $cnum. It checks if the specified course exists.
16312:
16313: Returns:
16314: undef if the course doesn't exist, otherwise
16315: in scalar context the combined courseid.
16316: in list context the two components of the course identifier, domain and
16317: courseid.
16318:
1.243 albertel 16319: =back
16320:
1.1172.2.109 raeburn 16321: =head2 Bubblesheet Configuration
16322:
16323: =over 4
16324:
16325: =item *
16326:
16327: get_scantron_config($which)
16328:
16329: $which - the name of the configuration to parse from the file.
16330:
16331: Parses and returns the bubblesheet configuration line selected as a
16332: hash of configuration file fields.
16333:
16334:
16335: Returns:
16336: If the named configuration is not in the file, an empty
16337: hash is returned.
16338:
16339: a hash with the fields
16340: name - internal name for the this configuration setup
16341: description - text to display to operator that describes this config
16342: CODElocation - if 0 or the string 'none'
16343: - no CODE exists for this config
16344: if -1 || the string 'letter'
16345: - a CODE exists for this config and is
16346: a string of letters
16347: Unsupported value (but planned for future support)
16348: if a positive integer
16349: - The CODE exists as the first n items from
16350: the question section of the form
16351: if the string 'number'
16352: - The CODE exists for this config and is
16353: a string of numbers
16354: CODEstart - (only matter if a CODE exists) column in the line where
16355: the CODE starts
16356: CODElength - length of the CODE
16357: IDstart - column where the student/employee ID starts
16358: IDlength - length of the student/employee ID info
16359: Qstart - column where the information from the bubbled
16360: 'questions' start
16361: Qlength - number of columns comprising a single bubble line from
16362: the sheet. (usually either 1 or 10)
16363: Qon - either a single character representing the character used
16364: to signal a bubble was chosen in the positional setup, or
16365: the string 'letter' if the letter of the chosen bubble is
16366: in the final, or 'number' if a number representing the
16367: chosen bubble is in the file (1->A 0->J)
16368: Qoff - the character used to represent that a bubble was
16369: left blank
16370: PaperID - if the scanning process generates a unique number for each
16371: sheet scanned the column that this ID number starts in
16372: PaperIDlength - number of columns that comprise the unique ID number
16373: for the sheet of paper
16374: FirstName - column that the first name starts in
16375: FirstNameLength - number of columns that the first name spans
16376: LastName - column that the last name starts in
16377: LastNameLength - number of columns that the last name spans
16378: BubblesPerRow - number of bubbles available in each row used to
16379: bubble an answer. (If not specified, 10 assumed).
16380:
16381:
16382: =item *
16383:
16384: get_scantronformat_file($cdom)
16385:
16386: $cdom - the course's domain (optional); if not supplied, uses
16387: domain for current $env{'request.course.id'}.
16388:
16389: Returns an array containing lines from the scantron format file for
16390: the domain of the course.
16391:
16392: If a url for a custom.tab file is listed in domain's configuration.db,
16393: lines are from this file.
16394:
16395: Otherwise, if a default.tab has been published in RES space by the
16396: domainconfig user, lines are from this file.
16397:
16398: Otherwise, fall back to getting lines from the legacy file on the
16399: local server: /home/httpd/lonTabs/default_scantronformat.tab
16400:
16401: =back
16402:
1.243 albertel 16403: =head2 Resource Subroutines
16404:
16405: =over 4
1.191 harris41 16406:
16407: =item *
16408:
1.243 albertel 16409: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191 harris41 16410:
16411: =item *
16412:
1.243 albertel 16413: repcopy($filename) : subscribes to the requested file, and attempts to
16414: replicate from the owning library server, Might return
1.607 raeburn 16415: 'unavailable', 'not_found', 'forbidden', 'ok', or
16416: 'bad_request', also attempts to grab the metadata for the
1.243 albertel 16417: resource. Expects the local filesystem pathname
16418: (/home/httpd/html/res/....)
16419:
16420: =back
16421:
16422: =head2 Resource Information
16423:
16424: =over 4
1.191 harris41 16425:
16426: =item *
16427:
1.1172.2.28 raeburn 16428: EXT($varname,$symb,$udom,$uname,$usection,$recurse,$cid) : evaluates
16429: and returns the value of a variety of different possible values,
16430: $varname should be a request string, and the other parameters can be
16431: used to specify who and what one is asking about. Ordinarily, $cid
16432: does not need to be specified, as it is retrived from
16433: $env{'request.course.id'}, but &Apache::lonnet::EXT() is called
16434: within lonuserstate::loadmap() when initializing a course, before
16435: $env{'request.course.id'} has been set, so it needs to be provided
16436: in that one case.
1.243 albertel 16437:
16438: Possible values for $varname are environment.lastname (or other item
16439: from the envirnment hash), user.name (or someother aspect about the
16440: user), resource.0.maxtries (or some other part and parameter of a
16441: resource)
1.204 albertel 16442:
16443: =item *
16444:
1.243 albertel 16445: directcondval($number) : get current value of a condition; reads from a state
16446: string
1.204 albertel 16447:
16448: =item *
16449:
1.243 albertel 16450: condval($condidx) : value of condition index based on state
1.204 albertel 16451:
16452: =item *
16453:
1.1172.2.146. .13(raeb 16454:-23): metadata($uri,$what,$toolsymb,$liburi,$prefix,$depthcount) : request a
1.243 albertel 16455: resource's metadata, $what should be either a specific key, or either
16456: 'keys' (to get a list of possible keys) or 'packages' to get a list of
1.1172.2.146. .13(raeb 16457:-23): packages that this resource currently uses, the last 3 arguments are
16458:-23): only used internally for recursive metadata.
16459:-23):
16460:-23): the toolsymb is only used where the uri is for an external tool (for which
16461:-23): the uri as well as the symb are guaranteed to be unique).
1.243 albertel 16462:
16463: this function automatically caches all requests
1.191 harris41 16464:
16465: =item *
16466:
1.243 albertel 16467: metadata_query($query,$custom,$customshow) : make a metadata query against the
16468: network of library servers; returns file handle of where SQL and regex results
16469: will be stored for query
1.191 harris41 16470:
16471: =item *
16472:
1.1172.2.66 raeburn 16473: symbread($filename,$donotrecurse,$ignorecachednull,$checkforblock,$possibles) :
16474: return symbolic list entry (all arguments optional).
16475:
16476: Args: filename is the filename (including path) for the file for which a symb
16477: is required; donotrecurse, if true will prevent calls to allowed() being made
16478: to check access status if more than one resource was found in the bighash
16479: (see rev. 1.249) to avoid an infinite loop if an ambiguous resource is part of
16480: a randompick); ignorecachednull, if true will prevent a symb of '' being
16481: returned if $env{$cache_str} is defined as ''; checkforblock if true will
16482: cause possible symbs to be checked to determine if they are subject to content
16483: blocking, if so they will not be included as possible symbs; possibles is a
16484: ref to a hash, which, as a side effect, will be populated with all possible
16485: symbs (content blocking not tested).
16486:
1.243 albertel 16487: returns the data handle
1.191 harris41 16488:
16489: =item *
16490:
1.1172.2.17 raeburn 16491: symbverify($symb,$thisfn,$encstate) : verifies that $symb actually exists
1.1172.2.11 raeburn 16492: and is a possible symb for the URL in $thisfn, and if is an encrypted
1.582 albertel 16493: resource that the user accessed using /enc/ returns a 1 on success, 0
1.1172.2.11 raeburn 16494: on failure, user must be in a course, as it assumes the existence of
16495: the course initial hash, and uses $env('request.course.id'}. The third
16496: arg is an optional reference to a scalar. If this arg is passed in the
16497: call to symbverify, it will be set to 1 if the symb has been set to be
16498: encrypted; otherwise it will be null.
1.243 albertel 16499:
1.191 harris41 16500: =item *
16501:
1.243 albertel 16502: symbclean($symb) : removes versions numbers from a symb, returns the
16503: cleaned symb
1.191 harris41 16504:
16505: =item *
16506:
1.243 albertel 16507: is_on_map($uri) : checks if the $uri is somewhere on the current
16508: course map, user must be in a course for it to work.
1.191 harris41 16509:
16510: =item *
16511:
1.243 albertel 16512: numval($salt) : return random seed value (addend for rndseed)
1.191 harris41 16513:
16514: =item *
16515:
1.243 albertel 16516: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
16517: a random seed, all arguments are optional, if they aren't sent it uses the
16518: environment to derive them. Note: if symb isn't sent and it can't get one
16519: from &symbread it will use the current time as its return value
1.191 harris41 16520:
16521: =item *
16522:
1.243 albertel 16523: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
16524: unfakeable, receipt
1.191 harris41 16525:
16526: =item *
16527:
1.620 albertel 16528: receipt() : API to ireceipt working off of env values; given out to users
1.191 harris41 16529:
16530: =item *
16531:
1.243 albertel 16532: countacc($url) : count the number of accesses to a given URL
1.191 harris41 16533:
16534: =item *
16535:
1.243 albertel 16536: 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 16537:
16538: =item *
16539:
1.243 albertel 16540: 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 16541:
16542: =item *
16543:
1.243 albertel 16544: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191 harris41 16545:
16546: =item *
16547:
1.243 albertel 16548: devalidate($symb) : devalidate temporary spreadsheet calculations,
16549: forcing spreadsheet to reevaluate the resource scores next time.
16550:
1.1172.2.13 raeburn 16551: =item *
16552:
16553: can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group) : determine if current user can edit a particular resource,
16554: when viewing in course context.
16555:
16556: input: six args -- filename (decluttered), course number, course domain,
16557: url, symb (if registered) and group (if this is a
16558: group item -- e.g., bulletin board, group page etc.).
16559:
16560: output: array of five scalars --
16561: $cfile -- url for file editing if editable on current server
16562: $home -- homeserver of resource (i.e., for author if published,
16563: or course if uploaded.).
16564: $switchserver -- 1 if server switch will be needed.
16565: $forceedit -- 1 if icon/link should be to go to edit mode
16566: $forceview -- 1 if icon/link should be to go to view mode
16567:
16568: =item *
16569:
16570: is_course_upload($file,$cnum,$cdom)
16571:
16572: Used in course context to determine if current file was uploaded to
16573: the course (i.e., would be found in /userfiles/docs on the course's
16574: homeserver.
16575:
16576: input: 3 args -- filename (decluttered), course number and course domain.
16577: output: boolean -- 1 if file was uploaded.
16578:
1.243 albertel 16579: =back
16580:
16581: =head2 Storing/Retreiving Data
16582:
16583: =over 4
1.191 harris41 16584:
16585: =item *
16586:
1.1172.2.64 raeburn 16587: store($storehash,$symb,$namespace,$udom,$uname,$laststore) : stores hash
16588: permanently for this url; hashref needs to be given and should be a \%hashname;
16589: the remaining args aren't required and if they aren't passed or are '' they will
16590: be derived from the env (with the exception of $laststore, which is an
16591: optional arg used when a user's submission is stored in grading).
16592: $laststore is $version=$timestamp, where $version is the most recent version
16593: number retrieved for the corresponding $symb in the $namespace db file, and
16594: $timestamp is the timestamp for that transaction (UNIX time).
16595: $laststore is currently only passed when cstore() is called by
16596: structuretags::finalize_storage().
1.191 harris41 16597:
16598: =item *
16599:
1.1172.2.64 raeburn 16600: cstore($storehash,$symb,$namespace,$udom,$uname,$laststore) : same as store
16601: but uses critical subroutine
1.191 harris41 16602:
16603: =item *
16604:
1.243 albertel 16605: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
16606: all args are optional
1.191 harris41 16607:
16608: =item *
16609:
1.717 albertel 16610: dumpstore($namespace,$udom,$uname,$regexp,$range) :
16611: dumps the complete (or key matching regexp) namespace into a hash
16612: ($udom, $uname, $regexp, $range are optional) for a namespace that is
16613: normally &store()ed into
16614:
16615: $range should be either an integer '100' (give me the first 100
16616: matching records)
16617: or be two integers sperated by a - with no spaces
16618: '30-50' (give me the 30th through the 50th matching
16619: records)
16620:
16621:
16622: =item *
16623:
1.1172.2.59 raeburn 16624: putstore($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog) :
1.717 albertel 16625: replaces a &store() version of data with a replacement set of data
16626: for a particular resource in a namespace passed in the $storehash hash
1.1172.2.59 raeburn 16627: reference. If $tolog is true, the transaction is logged in the courselog
16628: with an action=PUTSTORE.
1.717 albertel 16629:
16630: =item *
16631:
1.243 albertel 16632: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
16633: works very similar to store/cstore, but all data is stored in a
16634: temporary location and can be reset using tmpreset, $storehash should
16635: be a hash reference, returns nothing on success
1.191 harris41 16636:
16637: =item *
16638:
1.243 albertel 16639: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
16640: similar to restore, but all data is stored in a temporary location and
16641: can be reset using tmpreset. Returns a hash of values on success,
16642: error string otherwise.
1.191 harris41 16643:
16644: =item *
16645:
1.243 albertel 16646: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
16647: deltes all keys for $symb form the temporary storage hash.
1.191 harris41 16648:
16649: =item *
16650:
1.243 albertel 16651: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
16652: reference filled in from namesp ($udom and $uname are optional)
1.191 harris41 16653:
16654: =item *
16655:
1.243 albertel 16656: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
16657: namesp ($udom and $uname are optional)
1.191 harris41 16658:
16659: =item *
16660:
1.702 albertel 16661: dump($namespace,$udom,$uname,$regexp,$range) :
1.243 albertel 16662: dumps the complete (or key matching regexp) namespace into a hash
1.702 albertel 16663: ($udom, $uname, $regexp, $range are optional)
1.449 matthew 16664:
1.702 albertel 16665: $range should be either an integer '100' (give me the first 100
16666: matching records)
16667: or be two integers sperated by a - with no spaces
16668: '30-50' (give me the 30th through the 50th matching
16669: records)
1.449 matthew 16670: =item *
16671:
16672: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
16673: $store can be a scalar, an array reference, or if the amount to be
16674: incremented is > 1, a hash reference.
16675:
16676: ($udom and $uname are optional)
1.191 harris41 16677:
16678: =item *
16679:
1.243 albertel 16680: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
16681: ($udom and $uname are optional)
1.191 harris41 16682:
16683: =item *
16684:
1.243 albertel 16685: cput($namespace,$storehash,$udom,$uname) : critical put
16686: ($udom and $uname are optional)
1.191 harris41 16687:
16688: =item *
16689:
1.748 albertel 16690: newput($namespace,$storehash,$udom,$uname) :
16691:
16692: Attempts to store the items in the $storehash, but only if they don't
16693: currently exist, if this succeeds you can be certain that you have
16694: successfully created a new key value pair in the $namespace db.
16695:
16696:
16697: Args:
16698: $namespace: name of database to store values to
16699: $storehash: hashref to store to the db
16700: $udom: (optional) domain of user containing the db
16701: $uname: (optional) name of user caontaining the db
16702:
16703: Returns:
16704: 'ok' -> succeeded in storing all keys of $storehash
16705: 'key_exists: <key>' -> failed to anything out of $storehash, as at
16706: least <key> already existed in the db (other
16707: requested keys may also already exist)
1.967 bisitz 16708: 'error: <msg>' -> unable to tie the DB or other error occurred
1.748 albertel 16709: 'con_lost' -> unable to contact request server
16710: 'refused' -> action was not allowed by remote machine
16711:
16712:
16713: =item *
16714:
1.243 albertel 16715: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
16716: reference filled in from namesp (encrypts the return communication)
16717: ($udom and $uname are optional)
1.191 harris41 16718:
16719: =item *
16720:
1.243 albertel 16721: log($udom,$name,$home,$message) : write to permanent log for user; use
16722: critical subroutine
16723:
1.806 raeburn 16724: =item *
16725:
1.860 raeburn 16726: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
16727: array reference filled in from namespace found in domain level on either
16728: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806 raeburn 16729:
16730: =item *
16731:
1.860 raeburn 16732: put_dom($namespace,$storehash,$udom,$uhome) : stores hash in namespace at
16733: domain level either on specified domain server ($uhome) or primary domain
16734: server ($udom and $uhome are optional)
1.806 raeburn 16735:
1.943 raeburn 16736: =item *
16737:
1.1172.2.35 raeburn 16738: get_domain_defaults($target_domain,$ignore_cache) : returns hash with defaults
16739: for: authentication, language, quotas, timezone, date locale, and portal URL in
16740: the target domain.
16741:
16742: May also include additional key => value pairs for the following groups:
16743:
16744: =over
16745:
16746: =item
16747: disk quotas (MB allocated by default to portfolios and authoring spaces).
16748:
16749: =over
16750:
16751: =item defaultquota, authorquota
16752:
16753: =back
16754:
16755: =item
16756: tools (availability of aboutme page, blog, webDAV access for authoring spaces,
16757: portfolio for users).
16758:
16759: =over
16760:
16761: =item
16762: aboutme, blog, webdav, portfolio
16763:
16764: =back
16765:
16766: =item
16767: requestcourses: ability to request courses, and how requests are processed.
16768:
16769: =over
16770:
16771: =item
1.1172.2.37 raeburn 16772: official, unofficial, community, textbook
1.1172.2.35 raeburn 16773:
16774: =back
16775:
16776: =item
16777: inststatus: types of institutional affiliation, and order in which they are displayed.
16778:
16779: =over
16780:
16781: =item
1.1172.2.44 raeburn 16782: inststatustypes, inststatusorder, inststatusguest
1.1172.2.35 raeburn 16783:
16784: =back
16785:
16786: =item
16787: coursedefaults: can PDF forms can be created, default credits for courses, default quotas (MB)
16788: for course's uploaded content.
16789:
16790: =over
16791:
16792: =item
1.1172.2.68 raeburn 16793: canuse_pdfforms, officialcredits, unofficialcredits, textbookcredits, officialquota, unofficialquota,
16794: communityquota, textbookquota
1.1172.2.35 raeburn 16795:
16796: =back
16797:
16798: =item
16799: usersessions: set options for hosting of your users in other domains, and hosting of users from other domains
16800: on your servers.
16801:
16802: =over
16803:
16804: =item
16805: remotesessions, hostedsessions
16806:
16807: =back
16808:
16809: =back
16810:
16811: In cases where a domain coordinator has never used the "Set Domain Configuration"
16812: utility to create a configuration.db file on a domain's primary library server
16813: only the following domain defaults: auth_def, auth_arg_def, lang_def
16814: -- corresponding values are authentication type (internal, krb4, krb5,
16815: or localauth), initial password or a kerberos realm, language (e.g., en-us) --
16816: will be available. Values are retrieved from cache (if current), unless the
16817: optional $ignore_cache arg is true, or from domain's configuration.db (if available),
16818: or lastly from values in lonTabs/dns_domain,tab, or lonTabs/domain.tab.
16819:
16820: Typical usage:
1.943 raeburn 16821:
1.1172.2.35 raeburn 16822: %domdefaults = &get_domain_defaults($target_domain);
1.943 raeburn 16823:
1.243 albertel 16824: =back
16825:
16826: =head2 Network Status Functions
16827:
16828: =over 4
1.191 harris41 16829:
16830: =item *
16831:
1.1137 raeburn 16832: dirlist() : return directory list based on URI (first arg).
16833:
16834: Inputs: 1 required, 5 optional.
16835:
16836: =over
16837:
16838: =item
16839: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
16840:
16841: =item
16842: $userdomain - domain of user/course to be listed. Extracted from $uri if absent.
16843:
16844: =item
16845: $username - username of user/course to be listed. Extracted from $uri if absent.
16846:
16847: =item
16848: $getpropath - boolean: 1 if prepend path using &propath().
16849:
16850: =item
16851: $getuserdir - boolean: 1 if prepend path for "userfiles".
16852:
16853: =item
16854: $alternateRoot - path to prepend in place of path from $uri.
16855:
16856: =back
16857:
16858: Returns: Array of up to two items.
16859:
16860: =over
16861:
16862: a reference to an array of files/subdirectories
16863:
16864: =over
16865:
16866: Each element in the array of files/subdirectories is a & separated list of
16867: item name and the result of running stat on the item. If dirlist was requested
16868: for a file instead of a directory, the item name will be ''. For a directory
16869: listing, if the item is a metadata file, the element will end &N&M
16870: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
16871: default copyright set (1).
16872:
16873: =back
16874:
16875: a scalar containing error condition (if encountered).
16876:
16877: =over
16878:
16879: =item
16880: no_host (no homeserver identified for $username:$domain).
16881:
16882: =item
16883: no_such_host (server contacted for listing not identified as valid host).
16884:
16885: =item
16886: con_lost (connection to remote server failed).
16887:
16888: =item
16889: refused (invalid $username:$domain received on lond side).
16890:
16891: =item
16892: no_such_dir (directory at specified path on lond side does not exist).
16893:
16894: =item
16895: empty (directory at specified path on lond side is empty).
16896:
16897: =over
16898:
16899: This is currently not encountered because the &ls3, &ls2,
16900: &ls (_handler) routines on the lond side do not filter out
16901: . and .. from a directory listing.
16902:
16903: =back
16904:
16905: =back
16906:
16907: =back
1.191 harris41 16908:
16909: =item *
16910:
1.243 albertel 16911: spareserver() : find server with least workload from spare.tab
16912:
1.986 foxr 16913:
16914: =item *
16915:
16916: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
16917: if there is no corresponding loncapa host.
16918:
1.243 albertel 16919: =back
16920:
1.986 foxr 16921:
1.243 albertel 16922: =head2 Apache Request
16923:
16924: =over 4
1.191 harris41 16925:
16926: =item *
16927:
1.243 albertel 16928: ssi($url,%hash) : server side include, does a complete request cycle on url to
16929: localhost, posts hash
16930:
16931: =back
16932:
16933: =head2 Data to String to Data
16934:
16935: =over 4
1.191 harris41 16936:
16937: =item *
16938:
1.243 albertel 16939: hash2str(%hash) : convert a hash into a string complete with escaping and '='
16940: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191 harris41 16941:
16942: =item *
16943:
1.243 albertel 16944: hashref2str($hashref) : convert a hashref into a string complete with
16945: escaping and '=' and '&' separators, supports elements that are
16946: arrayrefs and hashrefs
1.191 harris41 16947:
16948: =item *
16949:
1.243 albertel 16950: arrayref2str($arrayref) : convert an arrayref into a string complete
16951: with escaping and '&' separators, supports elements that are arrayrefs
16952: and hashrefs
1.191 harris41 16953:
16954: =item *
16955:
1.243 albertel 16956: str2hash($string) : convert string to hash using unescaping and
16957: splitting on '=' and '&', supports elements that are arrayrefs and
16958: hashrefs
1.191 harris41 16959:
16960: =item *
16961:
1.243 albertel 16962: str2array($string) : convert string to hash using unescaping and
16963: splitting on '&', supports elements that are arrayrefs and hashrefs
16964:
16965: =back
16966:
16967: =head2 Logging Routines
16968:
16969:
16970: These routines allow one to make log messages in the lonnet.log and
16971: lonnet.perm logfiles.
1.191 harris41 16972:
1.1119 foxr 16973: =over 4
16974:
1.191 harris41 16975: =item *
16976:
1.243 albertel 16977: logtouch() : make sure the logfile, lonnet.log, exists
1.191 harris41 16978:
16979: =item *
16980:
1.243 albertel 16981: logthis() : append message to the normal lonnet.log file, it gets
16982: preiodically rolled over and deleted.
1.191 harris41 16983:
16984: =item *
16985:
1.243 albertel 16986: logperm() : append a permanent message to lonnet.perm.log, this log
16987: file never gets deleted by any automated portion of the system, only
16988: messages of critical importance should go in here.
16989:
1.1119 foxr 16990:
1.243 albertel 16991: =back
16992:
16993: =head2 General File Helper Routines
16994:
16995: =over 4
1.191 harris41 16996:
16997: =item *
16998:
1.481 raeburn 16999: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
17000: (a) files in /uploaded
17001: (i) If a local copy of the file exists -
17002: compares modification date of local copy with last-modified date for
17003: definitive version stored on home server for course. If local copy is
17004: stale, requests a new version from the home server and stores it.
17005: If the original has been removed from the home server, then local copy
17006: is unlinked.
17007: (ii) If local copy does not exist -
17008: requests the file from the home server and stores it.
17009:
17010: If $caller is 'uploadrep':
17011: This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
17012: for request for files originally uploaded via DOCS.
17013: - returns 'ok' if fresh local copy now available, -1 otherwise.
17014:
17015: Otherwise:
17016: This indicates a call from the content generation phase of the request.
17017: - returns the entire contents of the file or -1.
17018:
17019: (b) files in /res
17020: - returns the entire contents of a file or -1;
17021: it properly subscribes to and replicates the file if neccessary.
1.191 harris41 17022:
1.712 albertel 17023:
17024: =item *
17025:
17026: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
17027: reference
17028:
17029: returns either a stat() list of data about the file or an empty list
17030: if the file doesn't exist or couldn't find out about it (connection
17031: problems or user unknown)
17032:
1.191 harris41 17033: =item *
17034:
1.243 albertel 17035: filelocation($dir,$file) : returns file system location of a file
17036: based on URI; meant to be "fairly clean" absolute reference, $dir is a
17037: directory that relative $file lookups are to looked in ($dir of /a/dir
17038: and a file of ../bob will become /a/bob)
1.191 harris41 17039:
17040: =item *
17041:
17042: hreflocation($dir,$file) : returns file system location or a URL; same as
17043: filelocation except for hrefs
17044:
17045: =item *
17046:
1.1172.2.49 raeburn 17047: declutter() : declutters URLs -- remove beginning slashes, 'res' etc.
17048: also removes beginning /home/httpd/html unless /priv/ follows it.
1.191 harris41 17049:
1.243 albertel 17050: =back
17051:
1.608 albertel 17052: =head2 Usererfile file routines (/uploaded*)
17053:
17054: =over 4
17055:
17056: =item *
17057:
17058: userfileupload(): main rotine for putting a file in a user or course's
17059: filespace, arguments are,
17060:
1.620 albertel 17061: formname - required - this is the name of the element in $env where the
1.608 albertel 17062: filename, and the contents of the file to create/modifed exist
1.620 albertel 17063: the filename is in $env{'form.'.$formname.'.filename'} and the
17064: contents of the file is located in $env{'form.'.$formname}
1.1090 raeburn 17065: context - if coursedoc, store the file in the course of the active role
17066: of the current user;
17067: if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
17068: if 'canceloverwrite': delete file in tmp/overwrites directory
1.608 albertel 17069: subdir - required - subdirectory to put the file in under ../userfiles/
17070: if undefined, it will be placed in "unknown"
17071:
17072: (This routine calls clean_filename() to remove any dangerous
17073: characters from the filename, and then calls finuserfileupload() to
17074: complete the transaction)
17075:
17076: returns either the url of the uploaded file (/uploaded/....) if successful
17077: and /adm/notfound.html if unsuccessful
17078:
17079: =item *
17080:
17081: clean_filename(): routine for cleaing a filename up for storage in
17082: userfile space, argument is:
17083:
17084: filename - proposed filename
17085:
17086: returns: the new clean filename
17087:
17088: =item *
17089:
1.1090 raeburn 17090: finishuserfileupload(): routine that creates and sends the file to
1.608 albertel 17091: userspace, probably shouldn't be called directly
17092:
17093: docuname: username or courseid of destination for the file
17094: docudom: domain of user/course of destination for the file
17095: formname: same as for userfileupload()
1.1090 raeburn 17096: fname: filename (including subdirectories) for the file
17097: parser: if 'parse', will parse (html) file to extract references to objects, links etc.
1.1172.2.109 raeburn 17098: if hashref, and context is scantron, will convert csv format to standard format
1.1090 raeburn 17099: allfiles: reference to hash used to store objects found by parser
17100: codebase: reference to hash used for codebases of java objects found by parser
17101: thumbwidth: width (pixels) of thumbnail to be created for uploaded image
17102: thumbheight: height (pixels) of thumbnail to be created for uploaded image
17103: resizewidth: width to be used to resize image using resizeImage from ImageMagick
17104: resizeheight: height to be used to resize image using resizeImage from ImageMagick
17105: context: if 'overwrite', will move the uploaded file from its temporary location to
17106: userfiles to facilitate overwriting a previously uploaded file with same name.
1.1095 raeburn 17107: mimetype: reference to scalar to accommodate mime type determined
17108: from File::MMagic if $parser = parse.
1.608 albertel 17109:
17110: returns either the url of the uploaded file (/uploaded/....) if successful
1.1090 raeburn 17111: and /adm/notfound.html if unsuccessful (or an error message if context
17112: was 'overwrite').
17113:
1.608 albertel 17114:
17115: =item *
17116:
17117: renameuserfile(): renames an existing userfile to a new name
17118:
17119: Args:
17120: docuname: username or courseid of destination for the file
17121: docudom: domain of user/course of destination for the file
17122: old: current file name (including any subdirs under userfiles)
17123: new: desired file name (including any subdirs under userfiles)
17124:
17125: =item *
17126:
17127: mkdiruserfile(): creates a directory is a userfiles dir
17128:
17129: Args:
17130: docuname: username or courseid of destination for the file
17131: docudom: domain of user/course of destination for the file
17132: dir: dir to create (including any subdirs under userfiles)
17133:
17134: =item *
17135:
17136: removeuserfile(): removes a file that exists in userfiles
17137:
17138: Args:
17139: docuname: username or courseid of destination for the file
17140: docudom: domain of user/course of destination for the file
17141: fname: filname to delete (including any subdirs under userfiles)
17142:
17143: =item *
17144:
17145: removeuploadedurl(): convience function for removeuserfile()
17146:
17147: Args:
17148: url: a full /uploaded/... url to delete
17149:
1.747 albertel 17150: =item *
17151:
17152: get_portfile_permissions():
17153: Args:
17154: domain: domain of user or course contain the portfolio files
17155: user: name of user or num of course contain the portfolio files
17156: Returns:
17157: hashref of a dump of the proper file_permissions.db
17158:
17159:
17160: =item *
17161:
17162: get_access_controls():
17163:
17164: Args:
17165: current_permissions: the hash ref returned from get_portfile_permissions()
17166: group: (optional) the group you want the files associated with
17167: file: (optional) the file you want access info on
17168:
17169: Returns:
1.749 raeburn 17170: a hash (keys are file names) of hashes containing
17171: keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
17172: values are XML containing access control settings (see below)
1.747 albertel 17173:
17174: Internal notes:
17175:
1.749 raeburn 17176: access controls are stored in file_permissions.db as key=value pairs.
17177: key -> path to file/file_name\0uniqueID:scope_end_start
17178: where scope -> public,guest,course,group,domains or users.
17179: end -> UNIX time for end of access (0 -> no end date)
17180: start -> UNIX time for start of access
17181:
17182: value -> XML description of access control
17183: <scope type=""> (type =1 of: public,guest,course,group,domains,users">
17184: <start></start>
17185: <end></end>
17186:
17187: <password></password> for scope type = guest
17188:
17189: <domain></domain> for scope type = course or group
17190: <number></number>
17191: <roles id="">
17192: <role></role>
17193: <access></access>
17194: <section></section>
17195: <group></group>
17196: </roles>
17197:
17198: <dom></dom> for scope type = domains
17199:
17200: <users> for scope type = users
17201: <user>
17202: <uname></uname>
17203: <udom></udom>
17204: </user>
17205: </users>
17206: </scope>
17207:
17208: Access data is also aggregated for each file in an additional key=value pair:
17209: key -> path to file/file_name\0accesscontrol
17210: value -> reference to hash
17211: hash contains key = value pairs
17212: where key = uniqueID:scope_end_start
17213: value = UNIX time record was last updated
17214:
17215: Used to improve speed of look-ups of access controls for each file.
17216:
17217: Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
17218:
1.1172.2.13 raeburn 17219: =item *
17220:
1.749 raeburn 17221: modify_access_controls():
17222:
17223: Modifies access controls for a portfolio file
17224: Args
17225: 1. file name
17226: 2. reference to hash of required changes,
17227: 3. domain
17228: 4. username
17229: where domain,username are the domain of the portfolio owner
17230: (either a user or a course)
17231:
17232: Returns:
17233: 1. result of additions or updates ('ok' or 'error', with error message).
17234: 2. result of deletions ('ok' or 'error', with error message).
17235: 3. reference to hash of any new or updated access controls.
17236: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
17237: key = integer (inbound ID)
1.1172.2.13 raeburn 17238: value = uniqueID
17239:
17240: =item *
17241:
17242: get_timebased_id():
17243:
17244: Attempts to get a unique timestamp-based suffix for use with items added to a
17245: course via the Course Editor (e.g., folders, composite pages,
17246: group bulletin boards).
17247:
17248: Args: (first three required; six others optional)
17249:
17250: 1. prefix (alphanumeric): of keys in hash, e.g., suppsequence, docspage,
17251: docssequence, or name of group
17252:
17253: 2. keyid (alphanumeric): name of temporary locking key in hash,
17254: e.g., num, boardids
17255:
17256: 3. namespace: name of gdbm file used to store suffixes already assigned;
17257: file will be named nohist_namespace.db
17258:
17259: 4. cdom: domain of course; default is current course domain from %env
17260:
17261: 5. cnum: course number; default is current course number from %env
17262:
17263: 6. idtype: set to concat if an additional digit is to be appended to the
17264: unix timestamp to form the suffix, if the plain timestamp is already
17265: in use. Default is to not do this, but simply increment the unix
17266: timestamp by 1 until a unique key is obtained.
17267:
17268: 7. who: holder of locking key; defaults to user:domain for user.
17269:
17270: 8. locktries: number of attempts to obtain a lock (sleep of 1s before
17271: retrying); default is 3.
17272:
17273: 9. maxtries: number of attempts to obtain a unique suffix; default is 20.
17274:
17275: Returns:
17276:
17277: 1. suffix obtained (numeric)
17278:
17279: 2. result of deleting locking key (ok if deleted, or lock never obtained)
17280:
17281: 3. error: contains (localized) error message if an error occurred.
17282:
1.747 albertel 17283:
1.608 albertel 17284: =back
17285:
1.243 albertel 17286: =head2 HTTP Helper Routines
17287:
17288: =over 4
17289:
1.191 harris41 17290: =item *
17291:
17292: escape() : unpack non-word characters into CGI-compatible hex codes
17293:
17294: =item *
17295:
17296: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
17297:
1.243 albertel 17298: =back
17299:
17300: =head1 PRIVATE SUBROUTINES
17301:
17302: =head2 Underlying communication routines (Shouldn't call)
17303:
17304: =over 4
17305:
17306: =item *
17307:
17308: subreply() : tries to pass a message to lonc, returns con_lost if incapable
17309:
17310: =item *
17311:
17312: reply() : uses subreply to send a message to remote machine, logs all failures
17313:
17314: =item *
17315:
17316: critical() : passes a critical message to another server; if cannot
17317: get through then place message in connection buffer directory and
17318: returns con_delayed, if incapable of saving message, returns
17319: con_failed
17320:
17321: =item *
17322:
17323: reconlonc() : tries to reconnect lonc client processes.
17324:
17325: =back
17326:
17327: =head2 Resource Access Logging
17328:
17329: =over 4
17330:
17331: =item *
17332:
17333: flushcourselogs() : flush (save) buffer logs and access logs
17334:
17335: =item *
17336:
17337: courselog($what) : save message for course in hash
17338:
17339: =item *
17340:
17341: courseacclog($what) : save message for course using &courselog(). Perform
17342: special processing for specific resource types (problems, exams, quizzes, etc).
17343:
1.191 harris41 17344: =item *
17345:
17346: goodbye() : flush course logs and log shutting down; it is called in srm.conf
17347: as a PerlChildExitHandler
1.243 albertel 17348:
17349: =back
17350:
17351: =head2 Other
17352:
17353: =over 4
17354:
17355: =item *
17356:
17357: symblist($mapname,%newhash) : update symbolic storage links
1.191 harris41 17358:
17359: =back
17360:
17361: =cut
1.877 foxr 17362:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>